@@ -11,9 +11,18 @@ struct CodedAs: PropertyAttribute {
11
11
let node : AttributeSyntax
12
12
13
13
/// The alternate value expression provided.
14
- var expr : ExprSyntax {
15
- return node. arguments!
16
- . as ( LabeledExprListSyntax . self) !. first!. expression
14
+ var expr : ExprSyntax ? {
15
+ return node. arguments?
16
+ . as ( LabeledExprListSyntax . self) ? . first? . expression
17
+ }
18
+
19
+ /// The type to which to be decoded/encoded.
20
+ ///
21
+ /// Used for enums with internal/adjacent tagging to decode
22
+ /// the identifier to this type.
23
+ var type : TypeSyntax ? {
24
+ return node. attributeName. as ( IdentifierTypeSyntax . self) ?
25
+ . genericArgumentClause? . arguments. first? . argument
17
26
}
18
27
19
28
/// Creates a new instance with the provided node.
@@ -26,7 +35,7 @@ struct CodedAs: PropertyAttribute {
26
35
init ? ( from node: AttributeSyntax ) {
27
36
guard
28
37
node. attributeName. as ( IdentifierTypeSyntax . self) !
29
- . description == Self . name
38
+ . name . text == Self . name
30
39
else { return nil }
31
40
self . node = node
32
41
}
@@ -36,17 +45,35 @@ struct CodedAs: PropertyAttribute {
36
45
///
37
46
/// The following conditions are checked by the
38
47
/// built diagnoser:
39
- /// * Attached declaration is an enum-case declaration.
40
48
/// * Macro usage is not duplicated for the same declaration.
41
- /// * This attribute isn't used combined with `IgnoreCoding`
49
+ /// * If macro has zero arguments provided:
50
+ /// * Attached declaration is an enum declaration.
51
+ /// * This attribute must be combined with `Codable`
52
+ /// and `TaggedAt` attribute.
53
+ /// * This attribute mustn't be combined with `CodedBy`
54
+ /// attribute.
55
+ /// * If macro has one argument provided:
56
+ /// * Attached declaration is an enum-case declaration.
57
+ /// * This attribute isn't used combined with `IgnoreCoding`
42
58
/// attribute.
43
59
///
44
60
/// - Returns: The built diagnoser instance.
45
61
func diagnoser( ) -> DiagnosticProducer {
46
62
return AggregatedDiagnosticProducer {
47
- expect ( syntaxes: EnumCaseDeclSyntax . self)
48
63
cantDuplicate ( )
49
- cantBeCombined ( with: IgnoreCoding . self)
64
+ `if` (
65
+ has ( arguments: 1 ) ,
66
+ AggregatedDiagnosticProducer {
67
+ expect ( syntaxes: EnumCaseDeclSyntax . self)
68
+ cantBeCombined ( with: IgnoreCoding . self)
69
+ } ,
70
+ else: AggregatedDiagnosticProducer {
71
+ expect ( syntaxes: EnumDeclSyntax . self)
72
+ mustBeCombined ( with: Codable . self)
73
+ mustBeCombined ( with: TaggedAt . self)
74
+ cantBeCombined ( with: CodedBy . self)
75
+ }
76
+ )
50
77
}
51
78
}
52
79
}
0 commit comments