|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024–2025 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +extension ABI { |
| 12 | + /// A type describing an ABI version number. |
| 13 | + /// |
| 14 | + /// This type implements a subset of the [semantic versioning](https://semver.org) |
| 15 | + /// specification (specifically parsing, displaying, and comparing |
| 16 | + /// `<version core>` values we expect that Swift will need for the foreseeable |
| 17 | + /// future.) |
| 18 | + struct VersionNumber: Sendable { |
| 19 | + /// The major version. |
| 20 | + var majorComponent: Int8 |
| 21 | + |
| 22 | + /// The minor version. |
| 23 | + var minorComponent: Int8 |
| 24 | + |
| 25 | + /// The patch, revision, or bug fix version. |
| 26 | + var patchComponent: Int8 = 0 |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +extension ABI.VersionNumber { |
| 31 | + init(_ majorComponent: _const Int8, _ minorComponent: _const Int8, _ patchComponent: _const Int8 = 0) { |
| 32 | + self.init(majorComponent: majorComponent, minorComponent: minorComponent, patchComponent: patchComponent) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// MARK: - CustomStringConvertible |
| 37 | + |
| 38 | +extension ABI.VersionNumber: CustomStringConvertible { |
| 39 | + /// Initialize an instance of this type by parsing the given string. |
| 40 | + /// |
| 41 | + /// - Parameters: |
| 42 | + /// - string: The string to parse, such as `"0"` or `"6.3.0"`. |
| 43 | + /// |
| 44 | + /// @Comment { |
| 45 | + /// - Bug: We are not able to reuse the logic from swift-syntax's |
| 46 | + /// `VersionTupleSyntax` type here because we cannot link to swift-syntax |
| 47 | + /// in this target. |
| 48 | + /// } |
| 49 | + /// |
| 50 | + /// If `string` contains fewer than 3 numeric components, the missing |
| 51 | + /// components are inferred to be `0` (for example, `"1.2"` is equivalent to |
| 52 | + /// `"1.2.0"`.) If `string` contains more than 3 numeric components, the |
| 53 | + /// additional components are ignored. |
| 54 | + init?(_ string: String) { |
| 55 | + // Split the string on "." (assuming it is of the form "1", "1.2", or |
| 56 | + // "1.2.3") and parse the individual components as integers. |
| 57 | + let components = string.split(separator: ".", omittingEmptySubsequences: false) |
| 58 | + func componentValue(_ index: Int) -> Int8? { |
| 59 | + components.count > index ? Int8(components[index]) : 0 |
| 60 | + } |
| 61 | + |
| 62 | + guard let majorComponent = componentValue(0), |
| 63 | + let minorComponent = componentValue(1), |
| 64 | + let patchComponent = componentValue(2) else { |
| 65 | + return nil |
| 66 | + } |
| 67 | + self.init(majorComponent: majorComponent, minorComponent: minorComponent, patchComponent: patchComponent) |
| 68 | + } |
| 69 | + |
| 70 | + var description: String { |
| 71 | + if majorComponent <= 0 && minorComponent == 0 && patchComponent == 0 { |
| 72 | + // Version 0 and earlier are described as integers for compatibility with |
| 73 | + // Swift 6.2 and earlier. |
| 74 | + return String(describing: majorComponent) |
| 75 | + } else if patchComponent == 0 { |
| 76 | + return "\(majorComponent).\(minorComponent)" |
| 77 | + } |
| 78 | + return "\(majorComponent).\(minorComponent).\(patchComponent)" |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// MARK: - Equatable, Comparable |
| 83 | + |
| 84 | +extension ABI.VersionNumber: Equatable, Comparable { |
| 85 | + static func <(lhs: Self, rhs: Self) -> Bool { |
| 86 | + if lhs.majorComponent != rhs.majorComponent { |
| 87 | + return lhs.majorComponent < rhs.majorComponent |
| 88 | + } else if lhs.minorComponent != rhs.minorComponent { |
| 89 | + return lhs.minorComponent < rhs.minorComponent |
| 90 | + } else if lhs.patchComponent != rhs.patchComponent { |
| 91 | + return lhs.patchComponent < rhs.patchComponent |
| 92 | + } |
| 93 | + return false |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// MARK: - Codable |
| 98 | + |
| 99 | +extension ABI.VersionNumber: Codable { |
| 100 | + init(from decoder: any Decoder) throws { |
| 101 | + let container = try decoder.singleValueContainer() |
| 102 | + if let number = try? container.decode(Int8.self) { |
| 103 | + // Allow for version numbers encoded as integers for compatibility with |
| 104 | + // Swift 6.2 and earlier. |
| 105 | + self.init(majorComponent: number, minorComponent: 0) |
| 106 | + } else { |
| 107 | + let string = try container.decode(String.self) |
| 108 | + guard let result = Self(string) else { |
| 109 | + throw DecodingError.dataCorrupted( |
| 110 | + .init( |
| 111 | + codingPath: decoder.codingPath, |
| 112 | + debugDescription: "Unexpected string '\(string)' (expected an integer or a string of the form '1.2.3')" |
| 113 | + ) |
| 114 | + ) |
| 115 | + } |
| 116 | + self = result |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + func encode(to encoder: any Encoder) throws { |
| 121 | + var container = encoder.singleValueContainer() |
| 122 | + if majorComponent <= 0 && minorComponent == 0 && patchComponent == 0 { |
| 123 | + // Version 0 and earlier are encoded as integers for compatibility with |
| 124 | + // Swift 6.2 and earlier. |
| 125 | + try container.encode(majorComponent) |
| 126 | + } else { |
| 127 | + try container.encode("\(majorComponent).\(minorComponent).\(patchComponent)") |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments