Skip to content

Commit

Permalink
Add Version as a public API
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jul 16, 2024
1 parent 6ed7746 commit a048a4e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 61 deletions.
24 changes: 12 additions & 12 deletions Sources/Pulse/Helpers/Version.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -47,21 +47,21 @@ 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")
}
self = version
}

func encode(to encoder: Encoder) throws {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.description)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulse/LoggerStore/LoggerStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class LoggerStore: @unchecked Sendable, Identifiable {
public let events = PassthroughSubject<Event, Never>()

/// 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")
Expand Down
2 changes: 1 addition & 1 deletion Sources/PulseUI/Features/Sessions/SessionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 0 additions & 47 deletions Sources/PulseUI/Helpers/Version.swift

This file was deleted.

0 comments on commit a048a4e

Please sign in to comment.