Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed May 24, 2022
1 parent 9b4426d commit 857ddd6
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions Sources/SemanticVersion/SemanticVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,95 @@
import Foundation


/// `SemanticVersion` is a struct representing a software or project version according to ["Semantic Versioning"](https://semver.org).
///
/// Given a version number MAJOR.MINOR.PATCH, increment the:
/// 1. MAJOR version when you make incompatible API changes,
/// 2. MINOR version when you add functionality in a backwards compatible manner, and
/// PATCH version when you make backwards compatible bug fixes.
/// Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
///
/// Once instantiated, you can query a `SemanticVersion` about its components:
///
/// ```swift
/// let v123 = SemanticVersion(1, 2, 3)
/// v123.isStable // true
/// v123.isPreRelease // false
/// v123.isMajorRelease // false
/// v123.isMinorRelease // false
/// v123.isPatchRelease // true
/// ```
///
/// You can also instantiate a `SemanticVersion` from a string
///
/// ```swift
/// let v200 = SemanticVersion("2.0.0")!
/// v200.isStable // true
/// v200.isPreRelease // false
/// v200.isMajorRelease // true
/// v200.isMinorRelease // false
/// v200.isPatchRelease // false
/// ```
///
/// `SemanticVersion` supports beta versions:
///
/// ```swift
/// let v300rc1 = SemanticVersion("3.0.0-rc1-test")!
/// v300rc1.isStable // false
/// v300rc1.isPreRelease // true
/// v300rc1.isMajorRelease // false
/// v300rc1.isMinorRelease // false
/// v300rc1.isPatchRelease // false
/// v300rc1.major // 3
/// v300rc1.minor // 0
/// v300rc1.patch // 0
/// v300rc1.preRelease // "rc1-test"
/// ```
///
/// `SemanticVersion` is `Comparable` and `Equatable`:
///
/// ```swift
/// v123 < v200 // true
/// SemanticVersion("2.0.0")! < SemanticVersion("2.0.1")! // true
/// // NB: beta versions come before their releases
/// SemanticVersion("2.0.0")! > SemanticVersion("2.0.0-b1")! // true
/// v123 == SemanticVersion("1.2.3") // true
/// SemanticVersion("v1.2.3-beta1+build5")
/// == SemanticVersion(1, 2, 3, "beta1", "build5") // true
/// ```
///
/// `SemanticVersion` is `Hashable`:
///
/// ```swift
/// let dict = [ // [{major 3, minor 0, patch 0,...
/// v123: 1,
/// v200: 2,
/// v300rc1: 3
/// ]
/// ```
///
/// `SemanticVersion` is `Codable`:
///
/// ```swift
/// let data = try JSONEncoder().encode(v123) // 58 bytes
/// let decoded = try JSONDecoder().decode(SemanticVersion.self, from: data) // 1.2.3
/// decoded == v123 // true
/// ```

public struct SemanticVersion: Codable, Equatable, Hashable {
public var major: Int
public var minor: Int
public var patch: Int
public var preRelease: String
public var build: String

/// Initialize a version.
/// - Parameters:
/// - major: Major version number
/// - minor: Minor version number
/// - patch: Patch version number
/// - preRelease: Pre-release version number or details
/// - build: Build version number or details
public init(_ major: Int, _ minor: Int, _ patch: Int, _ preRelease: String = "", _ build: String = "") {
self.major = major
self.minor = minor
Expand All @@ -33,6 +115,9 @@ public struct SemanticVersion: Codable, Equatable, Hashable {


extension SemanticVersion: LosslessStringConvertible {

/// Initialize a version from a string. Returns `nil` if the string is not a semantic version.
/// - Parameter string: Version string.
public init?(_ string: String) {
let groups = semVerRegex.matchGroups(string)
guard
Expand Down

0 comments on commit 857ddd6

Please sign in to comment.