|
| 1 | +@_implementationOnly import SwiftSyntax |
| 2 | + |
| 3 | +extension Registrar.Options { |
| 4 | + /// A where clause generator for `Codable` conformance. |
| 5 | + /// |
| 6 | + /// This generator keeps track of generic type arguments, |
| 7 | + /// and generates where clause based on whether these |
| 8 | + /// types need to conform to `Decodable` or `Encodable` |
| 9 | + /// for `Codable` conformance. |
| 10 | + struct ConstraintGenerator { |
| 11 | + /// List of generic type arguments. |
| 12 | + /// |
| 13 | + /// Contains all the type requirement arguments |
| 14 | + /// of generic declaration. |
| 15 | + var typeArguments: [TokenSyntax] = [] |
| 16 | + |
| 17 | + /// Creates a new generator with provided declaration group. |
| 18 | + /// |
| 19 | + /// - Parameters: |
| 20 | + /// - decl: The declaration group generator picks |
| 21 | + /// generic type arguments from. |
| 22 | + /// |
| 23 | + /// - Returns: The newly created generator. |
| 24 | + init(decl: DeclGroupSyntax) { |
| 25 | + guard |
| 26 | + let decl = decl as? GenericTypeDeclSyntax, |
| 27 | + let paramClause = decl.genericParameterClause |
| 28 | + else { return } |
| 29 | + |
| 30 | + typeArguments.reserveCapacity(paramClause.parameters.count) |
| 31 | + for param in paramClause.parameters { |
| 32 | + typeArguments.append(param.name.trimmed) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Provides where clause for the `Codable` extension declaration. |
| 37 | + /// |
| 38 | + /// The where clause contains conformance requirement for generic |
| 39 | + /// arguments necessary for `Codable` conformance. |
| 40 | + /// |
| 41 | + /// - Parameters: |
| 42 | + /// - path: The requirement check path to be used for `Variable`. |
| 43 | + /// - variables: List of all the variables registered in `Registrar`. |
| 44 | + /// - protocol: The`Codable` protocol type syntax. |
| 45 | + /// |
| 46 | + /// - Returns: The generated where clause. |
| 47 | + @inlinable |
| 48 | + func codingClause( |
| 49 | + forRequirementPath path: KeyPath<any Variable, Bool?>, |
| 50 | + withVariables variables: [any Variable], |
| 51 | + conformingTo protocol: TypeSyntax |
| 52 | + ) -> GenericWhereClauseSyntax? { |
| 53 | + let allTypes = variables.filter { $0[keyPath: path] ?? true } |
| 54 | + .map(\.type.trimmed.description) |
| 55 | + let typeArguments = self.typeArguments.filter { type in |
| 56 | + return allTypes.contains(type.description) |
| 57 | + } |
| 58 | + guard !typeArguments.isEmpty else { return nil } |
| 59 | + return GenericWhereClauseSyntax { |
| 60 | + for argument in typeArguments { |
| 61 | + GenericRequirementSyntax( |
| 62 | + requirement: .conformanceRequirement( |
| 63 | + .init( |
| 64 | + leftType: IdentifierTypeSyntax(name: argument), |
| 65 | + rightType: `protocol` |
| 66 | + ) |
| 67 | + ) |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// Provides where clause for the `Decodable` extension declaration. |
| 74 | + /// |
| 75 | + /// The where clause contains conformance requirement for generic |
| 76 | + /// arguments necessary for `Decodable` conformance. |
| 77 | + /// |
| 78 | + /// - Parameters: |
| 79 | + /// - variables: List of all the variables registered in `Registrar`. |
| 80 | + /// - protocol: The`Decodable` protocol type syntax. |
| 81 | + /// |
| 82 | + /// - Returns: The generated where clause. |
| 83 | + func decodingClause( |
| 84 | + withVariables variables: [any Variable], |
| 85 | + conformingTo protocol: TypeSyntax |
| 86 | + ) -> GenericWhereClauseSyntax? { |
| 87 | + return codingClause( |
| 88 | + forRequirementPath: \.requireDecodable, |
| 89 | + withVariables: variables, conformingTo: `protocol` |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + /// Provides where clause for the `Encodable` extension declaration. |
| 94 | + /// |
| 95 | + /// The where clause contains conformance requirement for generic |
| 96 | + /// arguments necessary for `Encodable` conformance. |
| 97 | + /// |
| 98 | + /// - Parameters: |
| 99 | + /// - variables: List of all the variables registered in `Registrar`. |
| 100 | + /// - protocol: The`Encodable` protocol type syntax. |
| 101 | + /// |
| 102 | + /// - Returns: The generated where clause. |
| 103 | + func encodingClause( |
| 104 | + withVariables variables: [any Variable], |
| 105 | + conformingTo protocol: TypeSyntax |
| 106 | + ) -> GenericWhereClauseSyntax? { |
| 107 | + return codingClause( |
| 108 | + forRequirementPath: \.requireEncodable, |
| 109 | + withVariables: variables, conformingTo: `protocol` |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// A declaration group syntax type that accepts generic parameter clause. |
| 116 | +/// |
| 117 | +/// This type has optional `GenericParameterClauseSyntax` that |
| 118 | +/// can be used for where clause generation for `Codable` conformance. |
| 119 | +protocol GenericTypeDeclSyntax: DeclGroupSyntax { |
| 120 | + /// A where clause that places additional constraints on generic parameters |
| 121 | + /// like `where Element: Hashable`. |
| 122 | + var genericParameterClause: GenericParameterClauseSyntax? { get } |
| 123 | +} |
| 124 | + |
| 125 | +extension StructDeclSyntax: GenericTypeDeclSyntax {} |
| 126 | +extension ClassDeclSyntax: GenericTypeDeclSyntax {} |
| 127 | +extension EnumDeclSyntax: GenericTypeDeclSyntax {} |
0 commit comments