@@ -31,6 +31,32 @@ import Foundation
3131
3232public struct SwiftVersion : Sendable , Hashable , ExpressibleByStringLiteral , CustomStringConvertible
3333{
34+ internal struct ParsingError : OptionSet , Error {
35+ internal let rawValue : Int
36+
37+ internal init ( rawValue: Int ) {
38+ self . rawValue = rawValue
39+ }
40+ fileprivate init ? ( major: Int ? , minor: Int ? ) {
41+ var error = ParsingError ( )
42+
43+ if major == nil {
44+ error. insert ( . major)
45+ }
46+
47+ if minor == nil {
48+ error. insert ( . minor)
49+ }
50+
51+ guard error. rawValue > 0 else {
52+ return nil
53+ }
54+ self = error
55+ }
56+
57+ internal static let major : ParsingError = . init( rawValue: 1 << 0 )
58+ internal static let minor : ParsingError = . init( rawValue: 1 << 1 )
59+ }
3460 public let major : Int
3561 public let minor : Int
3662
@@ -42,11 +68,28 @@ public struct SwiftVersion: Sendable, Hashable, ExpressibleByStringLiteral, Cust
4268 self . minor = minor
4369 }
4470
45- public init ( stringLiteral value: String ) {
71+ internal init ( throwing value: String ) throws ( ParsingError ) {
4672 let components = value. components ( separatedBy: " . " )
47- let major : Int = . init( components [ 0 ] ) !
48- let minor : Int = . init( components [ 1 ] ) !
49- self . init ( major: major, minor: minor)
73+ let major : Int ? = . init( components [ 0 ] )
74+ let minor : Int ? = . init( components [ 1 ] )
75+ try self . init ( major: major, minor: minor)
76+ }
77+ internal init ( major: Int ? , minor: Int ? ) throws ( ParsingError) {
78+ if let major = major, let minor = minor {
79+ self . init ( major: major, minor: minor)
80+ } else if let error = ParsingError ( major: major, minor: minor) {
81+ throw error
82+ } else {
83+ assertionFailure ( " Should never reach here " )
84+ throw . init( rawValue: 0 )
85+ }
86+ }
87+ public init ( stringLiteral value: String ) {
88+ do {
89+ try self . init ( throwing: value)
90+ } catch {
91+ fatalError ( " Invalid String Literal: \( value) " )
92+ }
5093 }
5194}
5295
0 commit comments