-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathAutoStateMachineHashable.stencil
39 lines (35 loc) · 1.3 KB
/
AutoStateMachineHashable.stencil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{% for enum in types.enums where enum.implements.AutoStateMachineHashable or enum|annotated:"AutoStateMachineHashable" %}
extension {{ enum.name }}: StateMachineHashable {
{{ enum.accessLevel }} enum HashableIdentifier {
{% for case in enum.cases %}
case {{ case.name }}
{% endfor %}
}
{{ enum.accessLevel }} var hashableIdentifier: HashableIdentifier {
switch self {
{% for case in enum.cases %}
case .{{ case.name }}:
return .{{ case.name }}
{% endfor %}
}
}
{{ enum.accessLevel }} var associatedValue: Any {
switch self {
{% for case in enum.cases %}
{% if case.associatedValues.count == 0 %}
case .{{ case.name }}:
return ()
{% elif case.associatedValues.count == 1 %}
case let .{{ case.name }}(value):
return value
{% else %}
{% map case.associatedValues into values using associated %}value_{{ forloop.counter0 }}{% endmap %}
case let .{{ case.name }}({% for value in case.associatedValues %}value_{{ forloop.counter0 }}{% if not forloop.last %}, {% endif %}{% endfor %}):
return ({% for value in case.associatedValues %}value_{{ forloop.counter0 }}{% if not forloop.last %}, {% endif %}{% endfor %})
{% endif %}
{% endfor %}
}
}
}{% if not forloop.last %}
{% endif %}
{% endfor %}