-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatch.stencil
45 lines (37 loc) · 3.18 KB
/
Match.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
40
41
42
43
44
45
{% macro getTypeName type %}{% if type.actualTypeName %}{{ type.actualTypeName.name }}{% else %}{{ type.typeName.name }}{% endif %}{% endmacro %}
{% for type in types.all where type.name == "SourceryImports_Match" %}
{% for variable in type.variables %}
{% if variable|annotated:"testable" %}@testable {% else %}{% endif %}{% if variable|annotated:"preconcurrency" %}@preconcurrency {% else %}{% endif %}import {{ variable.name }}
{% endfor %}
{% endfor %}
{% for type in types.enums|annotated:"match" where type.cases.count > 0 %}
{{ type.accessLevel }} extension {{ type.name }} {
func match<ReturnedType>(
{% for case in type.cases %}
{{ case.name }}: {% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}{% call getTypeName associated %}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}(){% endif %} -> ReturnedType{% if forloop.last %}{% else %}, {% endif %}
{% endfor %}) -> ReturnedType {
switch self {
{% for case in type.cases %}
case .{{ case.name }}{% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}let x{{ forloop.counter }}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}{% endif %}:
return {{ case.name }}{% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}x{{ forloop.counter }}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}(){% endif %}
{% endfor %}
}
}
{% for case in type.cases %}
func match_{{ case.name }}<ReturnedType>(or fallback: @autoclosure () -> ReturnedType, _ matchFunction: {% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}{% call getTypeName associated %}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}(){% endif %} -> ReturnedType) -> ReturnedType {
if case .{{ case.name }}{% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}let x{{ forloop.counter }}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}{% endif %} = self {
return matchFunction{% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}x{{ forloop.counter }}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}(){% endif %}
} else {
return fallback()
}
}
func isCase_{{ case.name }}(_ isCaseFunction: {% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}{% call getTypeName associated %}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}(){% endif %} -> Bool = { {% if case.associatedValues.count > 0 %}{% for associated in case.associatedValues %}_{% if forloop.last %}{% else %}, {% endif %}{% endfor %} in{% else %}{% endif %} true }) -> Bool {
if case .{{ case.name }}{% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}let x{{ forloop.counter }}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}{% endif %} = self {
return isCaseFunction{% if case.associatedValues.count > 0 %}({% for associated in case.associatedValues %}x{{ forloop.counter }}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}(){% endif %}
} else {
return false
}
}
{% endfor %}
}
{% endfor %}