-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prism.stencil
62 lines (54 loc) · 3.93 KB
/
Prism.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{% macro getTypeName type %}{% if type.actualTypeName %}{{ type.actualTypeName.name }}{% else %}{{ type.typeName.name }}{% endif %}{% endmacro %}
{% for type in types.all where type.name == "SourceryImports_Prism" %}
{% 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:"prism" %}
extension {{ type.name }} {
{{ type.accessLevel }} enum prism {
{% for case in type.cases %}
{% if type.isGeneric %}
{% if case.hasAssociatedValue %}
{{ type.accessLevel }} static var {{ case.name }}: Prism<{{ type.name }},{% if case.associatedValues.count > 1 %}({% for associated in case.associatedValues %}{% call getTypeName associated %}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}{% call getTypeName case.associatedValues.first %}{% endif %}> {
return .init(
tryGet: { if case .{{ case.name }}({% for associated in case.associatedValues %}let x{{ forloop.counter}}{% if not forloop.last%}, {% endif %}{% endfor %}) = $0 { return ({% for associated in case.associatedValues %}x{{ forloop.counter}}{% if not forloop.last%}, {% endif %}{% endfor %}) } else { return nil } },
inject: { ({% for associated in case.associatedValues %}x{{ forloop.counter}}{% if not forloop.last%}, {% endif %}{% endfor %}) in .{{ case.name }}({% for associated in case.associatedValues %}{% if associated.localName != nil %}{{ associated.localName }}:{% endif %}x{{ forloop.counter }}{% if not forloop.last%}, {% endif %}{% endfor %}) })
}
{% else %}
{{ type.accessLevel }} static var {{ case.name }}: Prism<{{ type.name }}, ()> {
return .init(
tryGet: { if case .{{ case.name }} = $0 { return () } else { return nil } },
inject: { .{{ case.name }} })
}
{% endif %}
{% else %}
{% if case.hasAssociatedValue %}
{{ type.accessLevel }} static let {{ case.name }} = Prism<{{ type.name }},{% if case.associatedValues.count > 1 %}({% for associated in case.associatedValues %}{% call getTypeName associated %}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}{% call getTypeName case.associatedValues.first %}{% endif %}>(
tryGet: { if case .{{ case.name }}({% for associated in case.associatedValues %}let x{{ forloop.counter}}{% if not forloop.last%}, {% endif %}{% endfor %}) = $0 { return ({% for associated in case.associatedValues %}x{{ forloop.counter}}{% if not forloop.last%}, {% endif %}{% endfor %}) } else { return nil } },
inject: { ({% for associated in case.associatedValues %}x{{ forloop.counter}}{% if not forloop.last%}, {% endif %}{% endfor %}) in .{{ case.name }}({% for associated in case.associatedValues %}{% if associated.localName != nil %}{{ associated.localName }}:{% endif %}x{{ forloop.counter }}{% if not forloop.last%}, {% endif %}{% endfor %}) })
{% else %}
{{ type.accessLevel }} static let {{ case.name }} = Prism<{{ type.name }}, ()>(
tryGet: { if case .{{ case.name }} = $0 { return () } else { return nil } },
inject: { .{{ case.name }} })
{% endif %}
{% endif %}
{% endfor %}
}
}
{% if type.annotations.prism == "chain" %}
{{ type.accessLevel }} extension PrismType where SType == TType, AType == BType, BType == {{ type.name }} {
{% for case in type.cases %}
{% if case.hasAssociatedValue %}
var {{ case.name }}: Prism<SType,{% if case.associatedValues.count > 1 %}({% for associated in case.associatedValues %}{% call getTypeName associated %}{% if forloop.last %}{% else %}, {% endif %}{% endfor %}){% else %}{% call getTypeName case.associatedValues.first %}{% endif %}> {
return self >>> {{ type.name }}.prism.{{ case.name }}
}
{% else %}
var {{ case.name }}: Prism<SType,()> {
return self >>> {{ type.name }}.prism.{{ case.name }}
}
{% endif %}
{% endfor %}
}
{% endif %}
{% endfor %}