Skip to content

Commit 4b93ee7

Browse files
committed
working through issues
1 parent 4dd2167 commit 4b93ee7

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

.swift-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"prioritizeKeepingFunctionOutputTogether" : false,
2323
"respectsExistingLineBreaks" : true,
2424
"rules" : {
25-
"AllPublicDeclarationsHaveDocumentation" : false,
25+
"AllPublicDeclarationsHaveDocumentation" : true,
2626
"AlwaysUseLiteralForEmptyCollectionInit" : false,
2727
"AlwaysUseLowerCamelCase" : true,
2828
"AmbiguousTrailingClosureOverload" : true,

Sources/PackageDSLKit/SwiftVersion.swift

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ import Foundation
3131

3232
public 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

Sources/package/SwiftVersion.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ import PackageDSLKit
3232

3333
extension SwiftVersion: ExpressibleByArgument {
3434
public init(argument value: String) {
35-
let components = value.components(separatedBy: ".")
36-
let major: Int = .init(components[0])!
37-
let minor: Int = .init(components[1])!
38-
self.init(major: major, minor: minor)
35+
self.init(stringLiteral: value)
3936
}
4037
}

0 commit comments

Comments
 (0)