@@ -145,56 +145,84 @@ public struct Enum: CodeBlock {
145145/// A Swift `case` declaration inside an `enum`.
146146public struct EnumCase : CodeBlock {
147147 private let name : String
148- private var value : String ?
149- private var intValue : Int ?
148+ private var literalValue : Literal ?
150149
151150 /// Creates a `case` declaration.
152151 /// - Parameter name: The name of the case.
153152 public init ( _ name: String ) {
154153 self . name = name
154+ self . literalValue = nil
155155 }
156156
157- /// Sets the raw value of the case to a string .
158- /// - Parameter value: The string value.
157+ /// Sets the raw value of the case to a Literal .
158+ /// - Parameter value: The literal value.
159159 /// - Returns: A copy of the case with the raw value set.
160- public func equals( _ value: String ) -> Self {
160+ public func equals( _ value: Literal ) -> Self {
161161 var copy = self
162- copy. value = value
163- copy. intValue = nil
162+ copy. literalValue = value
164163 return copy
165164 }
166165
167- /// Sets the raw value of the case to an integer.
166+ /// Sets the raw value of the case to a string (for backward compatibility).
167+ /// - Parameter value: The string value.
168+ /// - Returns: A copy of the case with the raw value set.
169+ public func equals( _ value: String ) -> Self {
170+ self . equals ( . string( value) )
171+ }
172+
173+ /// Sets the raw value of the case to an integer (for backward compatibility).
168174 /// - Parameter value: The integer value.
169175 /// - Returns: A copy of the case with the raw value set.
170176 public func equals( _ value: Int ) -> Self {
171- var copy = self
172- copy. value = nil
173- copy. intValue = value
174- return copy
177+ self . equals ( . integer( value) )
178+ }
179+
180+ /// Sets the raw value of the case to a float (for backward compatibility).
181+ /// - Parameter value: The float value.
182+ /// - Returns: A copy of the case with the raw value set.
183+ public func equals( _ value: Double ) -> Self {
184+ self . equals ( . float( value) )
175185 }
176186
177187 public var syntax : SyntaxProtocol {
178188 let caseKeyword = TokenSyntax . keyword ( . case, trailingTrivia: . space)
179189 let identifier = TokenSyntax . identifier ( name, trailingTrivia: . space)
180190
181191 var initializer : InitializerClauseSyntax ?
182- if let value = value {
183- initializer = InitializerClauseSyntax (
184- equal: . equalToken( leadingTrivia: . space, trailingTrivia: . space) ,
185- value: StringLiteralExprSyntax (
186- openingQuote: . stringQuoteToken( ) ,
187- segments: StringLiteralSegmentListSyntax ( [
188- . stringSegment( StringSegmentSyntax ( content: . stringSegment( value) ) )
189- ] ) ,
190- closingQuote: . stringQuoteToken( )
192+ if let literal = literalValue {
193+ switch literal {
194+ case . string( let value) :
195+ initializer = InitializerClauseSyntax (
196+ equal: . equalToken( leadingTrivia: . space, trailingTrivia: . space) ,
197+ value: StringLiteralExprSyntax (
198+ openingQuote: . stringQuoteToken( ) ,
199+ segments: StringLiteralSegmentListSyntax ( [
200+ . stringSegment( StringSegmentSyntax ( content: . stringSegment( value) ) )
201+ ] ) ,
202+ closingQuote: . stringQuoteToken( )
203+ )
191204 )
192- )
193- } else if let intValue = intValue {
194- initializer = InitializerClauseSyntax (
195- equal: . equalToken( leadingTrivia: . space, trailingTrivia: . space) ,
196- value: IntegerLiteralExprSyntax ( digits: . integerLiteral( String ( intValue) ) )
197- )
205+ case . float( let value) :
206+ initializer = InitializerClauseSyntax (
207+ equal: . equalToken( leadingTrivia: . space, trailingTrivia: . space) ,
208+ value: FloatLiteralExprSyntax ( literal: . floatLiteral( String ( value) ) )
209+ )
210+ case . integer( let value) :
211+ initializer = InitializerClauseSyntax (
212+ equal: . equalToken( leadingTrivia: . space, trailingTrivia: . space) ,
213+ value: IntegerLiteralExprSyntax ( digits: . integerLiteral( String ( value) ) )
214+ )
215+ case . nil :
216+ initializer = InitializerClauseSyntax (
217+ equal: . equalToken( leadingTrivia: . space, trailingTrivia: . space) ,
218+ value: NilLiteralExprSyntax ( nilKeyword: . keyword( . nil ) )
219+ )
220+ case . boolean( let value) :
221+ initializer = InitializerClauseSyntax (
222+ equal: . equalToken( leadingTrivia: . space, trailingTrivia: . space) ,
223+ value: BooleanLiteralExprSyntax ( literal: value ? . keyword( . true ) : . keyword( . false ) )
224+ )
225+ }
198226 }
199227
200228 return EnumCaseDeclSyntax (
0 commit comments