@@ -11,9 +11,18 @@ struct CodedAs: PropertyAttribute {
1111 let node : AttributeSyntax
1212
1313 /// 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
1726 }
1827
1928 /// Creates a new instance with the provided node.
@@ -26,7 +35,7 @@ struct CodedAs: PropertyAttribute {
2635 init ? ( from node: AttributeSyntax ) {
2736 guard
2837 node. attributeName. as ( IdentifierTypeSyntax . self) !
29- . description == Self . name
38+ . name . text == Self . name
3039 else { return nil }
3140 self . node = node
3241 }
@@ -36,17 +45,35 @@ struct CodedAs: PropertyAttribute {
3645 ///
3746 /// The following conditions are checked by the
3847 /// built diagnoser:
39- /// * Attached declaration is an enum-case declaration.
4048 /// * 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`
4258 /// attribute.
4359 ///
4460 /// - Returns: The built diagnoser instance.
4561 func diagnoser( ) -> DiagnosticProducer {
4662 return AggregatedDiagnosticProducer {
47- expect ( syntaxes: EnumCaseDeclSyntax . self)
4863 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+ )
5077 }
5178 }
5279}
0 commit comments