diff --git a/Sources/SwiftUtils/AppVersion.swift b/Sources/SwiftUtils/AppVersion.swift deleted file mode 100644 index d5f647d..0000000 --- a/Sources/SwiftUtils/AppVersion.swift +++ /dev/null @@ -1,69 +0,0 @@ -// -// AppVersion.swift -// azooKey -// -// Created by ensan on 2022/07/02. -// Copyright © 2022 ensan. All rights reserved. -// - -import Foundation - -/// AppVersion is a struct that represents a version of an app. -/// It is a wrapper of String that conforms to Codable, Equatable, Comparable, Hashable, LosslessStringConvertible, CustomStringConvertible. -/// It is initialized with a string that represents a version of an app. -/// The string must be in the format of "major.minor.patch". -/// The string must not contain any other characters than numbers and dots. -public struct AppVersion: Codable, Equatable, Comparable, Hashable, LosslessStringConvertible, CustomStringConvertible, Sendable { - - /// ParseError is an enum that represents an error that occurs when parsing a string to an AppVersion. - private enum ParseError: Error { - case nonIntegerValue - } - - /// Initializes an AppVersion with a string that represents a version of an app. - public init?(_ description: String) { - if let versionSequence = try? description.split(separator: ".").map({ (value: Substring) throws -> Int in - guard let value = Int(value) else { throw ParseError.nonIntegerValue } - return value - }) { - if versionSequence.count < 1 { - self.majorVersion = 0 - } else { - self.majorVersion = versionSequence[0] - } - - if versionSequence.count < 2 { - self.minorVersion = 0 - } else { - self.minorVersion = versionSequence[1] - } - - if versionSequence.count < 3 { - self.patchVersion = 0 - } else { - self.patchVersion = versionSequence[2] - } - } else { - return nil - } - } - - /// Compares two AppVersions. - public static func < (lhs: AppVersion, rhs: AppVersion) -> Bool { - for (l, r) in zip([lhs.majorVersion, lhs.minorVersion, lhs.patchVersion], [rhs.majorVersion, rhs.minorVersion, rhs.patchVersion]) { - if l == r { - continue - } - return l < r - } - return false - } - - public var majorVersion: Int - public var minorVersion: Int - public var patchVersion: Int - - public var description: String { - "\(majorVersion).\(minorVersion).\(patchVersion)" - } -} diff --git a/Sources/SwiftUtils/ArrayUtils.swift b/Sources/SwiftUtils/ArrayUtils.swift index 459cee3..d4a8973 100644 --- a/Sources/SwiftUtils/ArrayUtils.swift +++ b/Sources/SwiftUtils/ArrayUtils.swift @@ -8,13 +8,6 @@ import Algorithms import Foundation -@resultBuilder -public struct ArrayBuilder { - public static func buildBlock(_ values: T...) -> [T] { - values - } -} - public extension Sequence { /// Returns a sequence that contains the elements of this sequence followed by the elements of the given sequence. /// - Parameters: diff --git a/Sources/SwiftUtils/CodableSupport.swift b/Sources/SwiftUtils/CodableSupport.swift deleted file mode 100644 index 8f0afa8..0000000 --- a/Sources/SwiftUtils/CodableSupport.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// CodableSupport.swift -// azooKey -// -// Created by ensan on 2021/03/17. -// Copyright © 2021 ensan. All rights reserved. -// - -import Foundation - -public extension Encodable { - /// Encodes this value into the given container. - /// - Parameters: - /// - container: The container to encode this value into. - func containerEncode(container: inout KeyedEncodingContainer, key: CodingKeys) throws { - try container.encode(self, forKey: key) - } -} diff --git a/Sources/SwiftUtils/StringUtils.swift b/Sources/SwiftUtils/StringUtils.swift index f33f232..b70893c 100644 --- a/Sources/SwiftUtils/StringUtils.swift +++ b/Sources/SwiftUtils/StringUtils.swift @@ -74,15 +74,6 @@ public extension StringProtocol { return String(utf16CodeUnits: result, count: result.count) } - /// Returns an Index value that is the specified distance from the start index. - /// - Parameter: - /// - offset: The distance to offset from the start index. - /// - Returns: An Index value that is the specified distance from the start index. - @inlinable - func indexFromStart(_ offset: Int) -> Index { - self.index(self.startIndex, offsetBy: offset) - } - // FIXME: レガシーな実装なのでどうにかしたい。Migrationする……? // エスケープが必要なのは次の文字: /* diff --git a/Sources/SwiftUtils/UserDefaultsManager.swift b/Sources/SwiftUtils/UserDefaultsManager.swift deleted file mode 100644 index c63efc3..0000000 --- a/Sources/SwiftUtils/UserDefaultsManager.swift +++ /dev/null @@ -1,64 +0,0 @@ -// -// UserDefaultsManager.swift -// -// -// Created by ensan on 2023/07/21. -// - -import Foundation - -public protocol UserDefaultsKeys: RawRepresentable where RawValue == String { - associatedtype Manager: UserDefaultsManager - init(keyPath: PartialKeyPath) -} - -public protocol UserDefaultsManager { - associatedtype Keys: UserDefaultsKeys where Keys.Manager == Self - var userDefaults: UserDefaults { get } - mutating func update(_ value: KeyPath, newValue: T) - mutating func update(_ value: KeyPath, process: (inout T) -> Void) -} - -public extension UserDefaultsManager { - mutating func update(_ value: KeyPath, newValue: T) { - if let value = value as? WritableKeyPath { - self[keyPath: value] = newValue - update(value: value) - } - } - - mutating func update(_ value: KeyPath, process: (inout T) -> Void) { - if let value = value as? WritableKeyPath { - process(&self[keyPath: value]) - update(value: value) - } - } -} - -public extension UserDefaultsManager { - mutating func update(value: WritableKeyPath) { - do { - let data = try JSONEncoder().encode(self[keyPath: value]) - let key = Keys(keyPath: value) - userDefaults.set(data, forKey: key.rawValue) - } catch { - debug(error) - } - } - - static func load(key: Keys, userDefaults: UserDefaults) -> T { - if let value = userDefaults.data(forKey: key.rawValue) { - do { - let value = try JSONDecoder().decode(T.self, from: value) - return value - } catch { - debug(error) - } - } - return T.initialValue - } -} - -public protocol StaticInitialValueAvailable: Codable { - static var initialValue: Self {get} -} diff --git a/Sources/SwiftUtils/Modify.swift b/Sources/SwiftUtils/WithMutableValue.swift similarity index 94% rename from Sources/SwiftUtils/Modify.swift rename to Sources/SwiftUtils/WithMutableValue.swift index 240d301..a8ddc65 100644 --- a/Sources/SwiftUtils/Modify.swift +++ b/Sources/SwiftUtils/WithMutableValue.swift @@ -1,5 +1,5 @@ // -// Modify.swift +// WithMutableValue.swift // azooKey // // Created by ensan on 2022/10/10. diff --git a/Tests/SwiftUtilsTests/AppVersionTests.swift b/Tests/SwiftUtilsTests/AppVersionTests.swift deleted file mode 100644 index b8374ca..0000000 --- a/Tests/SwiftUtilsTests/AppVersionTests.swift +++ /dev/null @@ -1,57 +0,0 @@ -// -// AppVersionTests.swift -// azooKeyTests -// -// Created by ensan on 2022/12/19. -// Copyright © 2022 ensan. All rights reserved. -// - -@testable import SwiftUtils -import XCTest - -final class AppVersionTests: XCTestCase { - - func testInitFromString() throws { - do { - let version = AppVersion("1.9.3") - XCTAssertNotNil(version) - XCTAssertEqual(version!.majorVersion, 1) - XCTAssertEqual(version!.minorVersion, 9) - XCTAssertEqual(version!.patchVersion, 3) - } - do { - let version = AppVersion("1.10") - XCTAssertNotNil(version) - XCTAssertEqual(version!.majorVersion, 1) - XCTAssertEqual(version!.minorVersion, 10) - XCTAssertEqual(version!.patchVersion, 0) - } - do { - let version = AppVersion("1") - XCTAssertNotNil(version) - XCTAssertEqual(version!.majorVersion, 1) - XCTAssertEqual(version!.minorVersion, 0) - XCTAssertEqual(version!.patchVersion, 0) - } - do { - let version = AppVersion("X") - XCTAssertNil(version) - } - } - - func testComparable() throws { - XCTAssertTrue(AppVersion("1.9.1")! < AppVersion("1.10")!) - XCTAssertTrue(AppVersion("1.7")! < AppVersion("1.7.1")!) - XCTAssertTrue(AppVersion("1.10")! < AppVersion("2.1")!) - XCTAssertFalse(AppVersion("1.9.1")! < AppVersion("1.9.1")!) - XCTAssertFalse(AppVersion("2.5")! < AppVersion("2.4.9")!) - - XCTAssertTrue(AppVersion("2.5")! > AppVersion("2.4.9")!) - XCTAssertTrue(AppVersion("3.1")! > AppVersion("1.4.9")!) - XCTAssertFalse(AppVersion("1.9.1")! > AppVersion("1.9.1")!) - XCTAssertFalse(AppVersion("1.9.1")! > AppVersion("1.10")!) - XCTAssertFalse(AppVersion("1.7")! > AppVersion("1.7.1")!) - XCTAssertFalse(AppVersion("1.10")! > AppVersion("2.1")!) - } - -} diff --git a/Tests/SwiftUtilsTests/StringExtensionTests.swift b/Tests/SwiftUtilsTests/StringExtensionTests.swift index 728c780..62bc38f 100644 --- a/Tests/SwiftUtilsTests/StringExtensionTests.swift +++ b/Tests/SwiftUtilsTests/StringExtensionTests.swift @@ -22,13 +22,4 @@ final class StringExtensionTests: XCTestCase { XCTAssertEqual("".toHiragana(), "") XCTAssertEqual("これはろん".toHiragana(), "これはろん") } - - func testIndexFromStart() throws { - do { - let string = "ア❤️‍🔥ウ😇オ" - XCTAssertEqual(string[string.indexFromStart(3)], "😇") - XCTAssertEqual(string[string.indexFromStart(4)], "オ") - } - } - }