You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add a throwing initialiser for creating a version from a string
This new initialiser throws a `VersionError` instance when initialisation fails. This gives the user more information and control over error handling. `Version`'s conformance to `LosslessStringConvertible` is preserved by having `init?(_ versionString: String)` call this new initialiser, and return `nil` when an error is thrown.
return"non-numerical characters in version core identifier\(nonNumericalIdentifiers.count >1?"s":"")\(nonNumericalIdentifiers.map{"'\($0)'"}.joined(separator:", "))"
return"characters other than alpha-numerics and hyphens in pre-release identifier\(nonAlhpaNumericalIdentifiers.count >1?"s":"")\(nonAlhpaNumericalIdentifiers.map{"'\($0)'"}.joined(separator:", "))"
return"characters other than alpha-numerics and hyphens in build metadata identifier\(nonAlhpaNumericalIdentifiers.count >1?"s":"")\(nonAlhpaNumericalIdentifiers.map{"'\($0)'"}.joined(separator:", "))"
83
+
}
84
+
}
85
+
}
86
+
87
+
extensionVersion{
88
+
// TODO: Rename this function to `init(string: String) trhows`, after `init?(string: String)` is removed.
89
+
// TODO: Find a better error-checking order.
90
+
// Currently, if a version string is "fourty-two", this initializer throws an error that says "fourty" is only 1 version core identifier, which is not enough.
91
+
// But this is misleading the user to consider "fourty" as a valid version core identifier.
92
+
// We should find a way to check for (or throw) "wrong characters used" errors first, but without overly-complicating the logic.
93
+
/// Creates a version from the given string.
94
+
/// - Parameter versionString: The string to create the version from.
95
+
/// - Throws: A `VersionError` instance if the `versionString` doesn't follow [SemVer 2.0.0](https://semver.org).
96
+
publicinit(versionString:String)throws{
97
+
// SemVer 2.0.0 allows only ASCII alphanumerical characters and "-" in the version string, except for "." and "+" as delimiters. ("-" is used as a delimiter between the version core and pre-release identifiers, but it's allowed within pre-release and metadata identifiers as well.)
98
+
// Alphanumerics check will come later, after each identifier is split out (i.e. after the delimiters are removed).
/// Initializes a version struct with the provided version string.
123
227
/// - Parameter version: A version string to use for creating a new version struct.
124
228
publicinit?(_ versionString:String){
125
-
// SemVer 2.0.0 allows only ASCII alphanumerical characters and "-" in the version string, except for "." and "+" as delimiters. ("-" is used as a delimiter between the version core and pre-release identifiers, but it's allowed within pre-release and metadata identifiers as well.)
126
-
// Alphanumerics check will come later, after each identifier is split out (i.e. after the delimiters are removed).
0 commit comments