1515import Foundation
1616
1717
18+ /// `SemanticVersion` is a struct representing a software or project version according to ["Semantic Versioning"](https://semver.org).
19+ ///
20+ /// Given a version number MAJOR.MINOR.PATCH, increment the:
21+ /// 1. MAJOR version when you make incompatible API changes,
22+ /// 2. MINOR version when you add functionality in a backwards compatible manner, and
23+ /// PATCH version when you make backwards compatible bug fixes.
24+ /// Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
25+ ///
26+ /// Once instantiated, you can query a `SemanticVersion` about its components:
27+ ///
28+ /// ```swift
29+ /// let v123 = SemanticVersion(1, 2, 3)
30+ /// v123.isStable // true
31+ /// v123.isPreRelease // false
32+ /// v123.isMajorRelease // false
33+ /// v123.isMinorRelease // false
34+ /// v123.isPatchRelease // true
35+ /// ```
36+ ///
37+ /// You can also instantiate a `SemanticVersion` from a string
38+ ///
39+ /// ```swift
40+ /// let v200 = SemanticVersion("2.0.0")!
41+ /// v200.isStable // true
42+ /// v200.isPreRelease // false
43+ /// v200.isMajorRelease // true
44+ /// v200.isMinorRelease // false
45+ /// v200.isPatchRelease // false
46+ /// ```
47+ ///
48+ /// `SemanticVersion` supports beta versions:
49+ ///
50+ /// ```swift
51+ /// let v300rc1 = SemanticVersion("3.0.0-rc1-test")!
52+ /// v300rc1.isStable // false
53+ /// v300rc1.isPreRelease // true
54+ /// v300rc1.isMajorRelease // false
55+ /// v300rc1.isMinorRelease // false
56+ /// v300rc1.isPatchRelease // false
57+ /// v300rc1.major // 3
58+ /// v300rc1.minor // 0
59+ /// v300rc1.patch // 0
60+ /// v300rc1.preRelease // "rc1-test"
61+ /// ```
62+ ///
63+ /// `SemanticVersion` is `Comparable` and `Equatable`:
64+ ///
65+ /// ```swift
66+ /// v123 < v200 // true
67+ /// SemanticVersion("2.0.0")! < SemanticVersion("2.0.1")! // true
68+ /// // NB: beta versions come before their releases
69+ /// SemanticVersion("2.0.0")! > SemanticVersion("2.0.0-b1")! // true
70+ /// v123 == SemanticVersion("1.2.3") // true
71+ /// SemanticVersion("v1.2.3-beta1+build5")
72+ /// == SemanticVersion(1, 2, 3, "beta1", "build5") // true
73+ /// ```
74+ ///
75+ /// `SemanticVersion` is `Hashable`:
76+ ///
77+ /// ```swift
78+ /// let dict = [ // [{major 3, minor 0, patch 0,...
79+ /// v123: 1,
80+ /// v200: 2,
81+ /// v300rc1: 3
82+ /// ]
83+ /// ```
84+ ///
85+ /// `SemanticVersion` is `Codable`:
86+ ///
87+ /// ```swift
88+ /// let data = try JSONEncoder().encode(v123) // 58 bytes
89+ /// let decoded = try JSONDecoder().decode(SemanticVersion.self, from: data) // 1.2.3
90+ /// decoded == v123 // true
91+ /// ```
92+
1893public struct SemanticVersion : Codable , Equatable , Hashable {
1994 public var major : Int
2095 public var minor : Int
2196 public var patch : Int
2297 public var preRelease : String
2398 public var build : String
2499
100+ /// Initialize a version.
101+ /// - Parameters:
102+ /// - major: Major version number
103+ /// - minor: Minor version number
104+ /// - patch: Patch version number
105+ /// - preRelease: Pre-release version number or details
106+ /// - build: Build version number or details
25107 public init ( _ major: Int , _ minor: Int , _ patch: Int , _ preRelease: String = " " , _ build: String = " " ) {
26108 self . major = major
27109 self . minor = minor
@@ -33,6 +115,9 @@ public struct SemanticVersion: Codable, Equatable, Hashable {
33115
34116
35117extension SemanticVersion : LosslessStringConvertible {
118+
119+ /// Initialize a version from a string. Returns `nil` if the string is not a semantic version.
120+ /// - Parameter string: Version string.
36121 public init ? ( _ string: String ) {
37122 let groups = semVerRegex. matchGroups ( string)
38123 guard
0 commit comments