import Foundation /// A comparable Ollama version such as `0.40.18`. public struct OllamaVersion: Comparable, Sendable, CustomStringConvertible { /// The numeric components, e.g. `[0, 40, 18]`. public let components: [Int] /// The minimum supported server version (ARCHITECTURE ยง2). public static let minimumSupported = OllamaVersion(components: [0, 40, 0]) /// Parses a dotted numeric version. Returns nil for non-numeric or empty input. public init?(_ string: String) { let withoutBuild = string.split(separator: "+", maxSplits: 1).first.map(String.init) ?? string let core = withoutBuild.split(separator: "-", maxSplits: 1).first.map(String.init) ?? withoutBuild let parts = core.split(separator: ".").map { Int($0) } guard !parts.isEmpty, parts.allSatisfy({ $0 != nil }) else { return nil } components = parts.compactMap(\.self) } private init(components: [Int]) { self.components = components } /// The dotted representation. public var description: String { components.map(String.init).joined(separator: ".") } /// Component-wise comparison, padding the shorter version with zeros. public static func < (lhs: OllamaVersion, rhs: OllamaVersion) -> Bool { let count = max(lhs.components.count, rhs.components.count) for index in 0 ..< count { let left = index < lhs.components.count ? lhs.components[index] : 0 let right = index < rhs.components.count ? rhs.components[index] : 0 if left != right { return left < right } } return false } /// Equality treats `0.40` and `0.40.0` as the same version. public static func == (lhs: OllamaVersion, rhs: OllamaVersion) -> Bool { !(lhs < rhs) && !(rhs < lhs) } }