From a048a4e16f216e2e4a6350912d84457df873c522 Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 16 Jul 2024 18:43:01 -0400 Subject: [PATCH] Add Version as a public API --- Sources/Pulse/Helpers/Version.swift | 24 +++++----- Sources/Pulse/LoggerStore/LoggerStore.swift | 2 +- .../Features/Sessions/SessionsView.swift | 2 +- Sources/PulseUI/Helpers/Version.swift | 47 ------------------- 4 files changed, 14 insertions(+), 61 deletions(-) delete mode 100644 Sources/PulseUI/Helpers/Version.swift diff --git a/Sources/Pulse/Helpers/Version.swift b/Sources/Pulse/Helpers/Version.swift index 0cb60f95d..3b9920d8b 100644 --- a/Sources/Pulse/Helpers/Version.swift +++ b/Sources/Pulse/Helpers/Version.swift @@ -2,12 +2,12 @@ // // Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean). -struct Version: Comparable, LosslessStringConvertible, Codable, Sendable { - let major: Int - let minor: Int - let patch: Int +public struct Version: Comparable, LosslessStringConvertible, Codable, Sendable { + public let major: Int + public let minor: Int + public let patch: Int - init(_ major: Int, _ minor: Int, _ patch: Int) { + public init(_ major: Int, _ minor: Int, _ patch: Int) { precondition(major >= 0 && minor >= 0 && patch >= 0, "Negative versioning is invalid.") self.major = major self.minor = minor @@ -16,15 +16,15 @@ struct Version: Comparable, LosslessStringConvertible, Codable, Sendable { // MARK: Comparable - static func == (lhs: Version, rhs: Version) -> Bool { + public static func == (lhs: Version, rhs: Version) -> Bool { !(lhs < rhs) && !(lhs > rhs) } - static func < (lhs: Version, rhs: Version) -> Bool { + public static func < (lhs: Version, rhs: Version) -> Bool { (lhs.major, lhs.minor, lhs.patch) < (rhs.major, rhs.minor, rhs.patch) } - init(string: String) throws { + public init(string: String) throws { guard let version = Version(string) else { throw LoggerStore.Error.unknownError // Should never happen } @@ -33,7 +33,7 @@ struct Version: Comparable, LosslessStringConvertible, Codable, Sendable { // MARK: LosslessStringConvertible - init?(_ string: String) { + public init?(_ string: String) { guard string.allSatisfy(\.isASCII) else { return nil } let components = string.split(separator: ".", omittingEmptySubsequences: false) guard components.count == 3, @@ -47,13 +47,13 @@ struct Version: Comparable, LosslessStringConvertible, Codable, Sendable { self.patch = patch } - var description: String { + public var description: String { "\(major).\(minor).\(patch)" } // MARK: Codable - init(from decoder: Decoder) throws { + public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() guard let version = Version(try container.decode(String.self)) else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid version number format") @@ -61,7 +61,7 @@ struct Version: Comparable, LosslessStringConvertible, Codable, Sendable { self = version } - func encode(to encoder: Encoder) throws { + public func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() try container.encode(self.description) } diff --git a/Sources/Pulse/LoggerStore/LoggerStore.swift b/Sources/Pulse/LoggerStore/LoggerStore.swift index eca062172..93a998fb6 100644 --- a/Sources/Pulse/LoggerStore/LoggerStore.swift +++ b/Sources/Pulse/LoggerStore/LoggerStore.swift @@ -38,7 +38,7 @@ public final class LoggerStore: @unchecked Sendable, Identifiable { public let events = PassthroughSubject() /// The store version. - public var version: String { manifest.version.description } + public var version: Version { manifest.version } private var isSaveScheduled = false private let queue = DispatchQueue(label: "com.github.kean.pulse.logger-store") diff --git a/Sources/PulseUI/Features/Sessions/SessionsView.swift b/Sources/PulseUI/Features/Sessions/SessionsView.swift index 69c0e445e..e43807d04 100644 --- a/Sources/PulseUI/Features/Sessions/SessionsView.swift +++ b/Sources/PulseUI/Features/Sessions/SessionsView.swift @@ -25,7 +25,7 @@ struct SessionsView: View { @Environment(\.router) private var router var body: some View { - if let version = Version(store.version), version < Version(3, 6, 0) { + if store.version < Version(3, 6, 0) { PlaceholderView(imageName: "questionmark.app", title: "Unsupported", subtitle: "This feature requires a store created by Pulse version 3.6.0 or higher").padding() } else { content diff --git a/Sources/PulseUI/Helpers/Version.swift b/Sources/PulseUI/Helpers/Version.swift deleted file mode 100644 index 6efd9e134..000000000 --- a/Sources/PulseUI/Helpers/Version.swift +++ /dev/null @@ -1,47 +0,0 @@ -// The MIT License (MIT) -// -// Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean). - -import Foundation -import Pulse - -struct Version: Comparable, Sendable { - let major: Int - let minor: Int - let patch: Int - - init(_ major: Int, _ minor: Int, _ patch: Int) { - precondition(major >= 0 && minor >= 0 && patch >= 0, "Negative versioning is invalid.") - self.major = major - self.minor = minor - self.patch = patch - } - - var description: String { - "\(major).\(minor).\(patch)" - } - - // MARK: Comparable - - static func == (lhs: Version, rhs: Version) -> Bool { - !(lhs < rhs) && !(lhs > rhs) - } - - static func < (lhs: Version, rhs: Version) -> Bool { - (lhs.major, lhs.minor, lhs.patch) < (rhs.major, rhs.minor, rhs.patch) - } - - init?(_ string: String) { - guard string.allSatisfy(\.isASCII) else { return nil } - let components = string.split(separator: ".", omittingEmptySubsequences: false) - guard components.count == 3, - let major = Int(components[0]), - let minor = Int(components[1]), - let patch = Int(components[2]) else { - return nil - } - self.major = major - self.minor = minor - self.patch = patch - } -}