From 7dccdf34286567339b8ef851ac13a438e7216e1d Mon Sep 17 00:00:00 2001 From: Khan Winter <35942988+thecoolwinter@users.noreply.github.com> Date: Tue, 2 Sep 2025 22:11:24 -0500 Subject: [PATCH] Make it work on Linux --- Sources/CBOR/Decoder/CBORDecoder.swift | 4 ++++ .../Containers/DecodingContextContainer.swift | 4 ++++ .../Containers/KeyedCBORDecodingContainer.swift | 4 ++++ .../SingleValueCBORDecodingContainer.swift | 10 ++++++++++ .../Containers/UnkeyedCBORDecodingContainer.swift | 4 ++++ Sources/CBOR/Decoder/DecodingContext.swift | 4 ++++ Sources/CBOR/Decoder/Scanner/CBORScanner.swift | 4 ++++ Sources/CBOR/Decoder/Scanner/DataReader.swift | 4 ++++ .../CBOR/Encoder/Optimizers/DateOptimizer.swift | 4 ++++ Sources/Fuzzing/AnyDecodable.swift | 14 +++++--------- Sources/Fuzzing/main.swift | 4 ++++ Tests/CBORTests/DecodableTests.swift | 4 ++++ Tests/CBORTests/EncodableTests.swift | 4 ++++ .../Data+ExpressibleByArrayLiteral.swift | 4 ++++ Tests/CBORTests/Extensions/Data+hexString.swift | 4 ++++ Tests/CBORTests/Extensions/String+asHexData.swift | 4 ++++ Tests/CBORTests/Mock.swift | 4 ++++ Tests/CBORTests/RoundTripTests.swift | 4 ++++ Tests/CBORTests/ScannerTests.swift | 4 ++++ 19 files changed, 83 insertions(+), 9 deletions(-) diff --git a/Sources/CBOR/Decoder/CBORDecoder.swift b/Sources/CBOR/Decoder/CBORDecoder.swift index 78fbcd9..d77db10 100644 --- a/Sources/CBOR/Decoder/CBORDecoder.swift +++ b/Sources/CBOR/Decoder/CBORDecoder.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/20/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif /// Decodes ``Decodable`` objects from CBOR data. /// diff --git a/Sources/CBOR/Decoder/Containers/DecodingContextContainer.swift b/Sources/CBOR/Decoder/Containers/DecodingContextContainer.swift index 5781f14..98a761a 100644 --- a/Sources/CBOR/Decoder/Containers/DecodingContextContainer.swift +++ b/Sources/CBOR/Decoder/Containers/DecodingContextContainer.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/20/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif protocol DecodingContextContainer { var context: DecodingContext { get } diff --git a/Sources/CBOR/Decoder/Containers/KeyedCBORDecodingContainer.swift b/Sources/CBOR/Decoder/Containers/KeyedCBORDecodingContainer.swift index 05379d4..1dba6f4 100644 --- a/Sources/CBOR/Decoder/Containers/KeyedCBORDecodingContainer.swift +++ b/Sources/CBOR/Decoder/Containers/KeyedCBORDecodingContainer.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/20/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif struct KeyedCBORDecodingContainer: DecodingContextContainer, KeyedDecodingContainerProtocol { enum AnyKey: Hashable { diff --git a/Sources/CBOR/Decoder/Containers/SingleValueCBORDecodingContainer.swift b/Sources/CBOR/Decoder/Containers/SingleValueCBORDecodingContainer.swift index 3279c0b..6eff065 100644 --- a/Sources/CBOR/Decoder/Containers/SingleValueCBORDecodingContainer.swift +++ b/Sources/CBOR/Decoder/Containers/SingleValueCBORDecodingContainer.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/20/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif struct SingleValueCBORDecodingContainer: DecodingContextContainer { let context: DecodingContext @@ -138,9 +142,15 @@ extension SingleValueCBORDecodingContainer: SingleValueDecodingContainer { private func decodeStringDate() throws -> Date { let taggedData = context.results.loadTagData(tagMapIndex: data.mapOffset) let string = try SingleValueCBORDecodingContainer(context: context, data: taggedData).decode(String.self) +#if canImport(FoundationEssentials) + guard let date = try? Date.ISO8601FormatStyle().parse(string) else { + throw DecodingError.dataCorrupted(context.error("Failed to decode date from \"\(string)\"")) + } +#else guard let date = ISO8601DateFormatter().date(from: string) else { throw DecodingError.dataCorrupted(context.error("Failed to decode date from \"\(string)\"")) } +#endif return date } diff --git a/Sources/CBOR/Decoder/Containers/UnkeyedCBORDecodingContainer.swift b/Sources/CBOR/Decoder/Containers/UnkeyedCBORDecodingContainer.swift index 32256b3..5c54aa8 100644 --- a/Sources/CBOR/Decoder/Containers/UnkeyedCBORDecodingContainer.swift +++ b/Sources/CBOR/Decoder/Containers/UnkeyedCBORDecodingContainer.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/20/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif struct UnkeyedCBORDecodingContainer: DecodingContextContainer, UnkeyedDecodingContainer { let context: DecodingContext diff --git a/Sources/CBOR/Decoder/DecodingContext.swift b/Sources/CBOR/Decoder/DecodingContext.swift index 017af77..a4bd605 100644 --- a/Sources/CBOR/Decoder/DecodingContext.swift +++ b/Sources/CBOR/Decoder/DecodingContext.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/20/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif struct DecodingContext { let options: DecodingOptions diff --git a/Sources/CBOR/Decoder/Scanner/CBORScanner.swift b/Sources/CBOR/Decoder/Scanner/CBORScanner.swift index 6208fc0..b012ba4 100644 --- a/Sources/CBOR/Decoder/Scanner/CBORScanner.swift +++ b/Sources/CBOR/Decoder/Scanner/CBORScanner.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/24/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif private let taggedScanMap: [UInt: Set] = [ 0: [.string], // Date (string) diff --git a/Sources/CBOR/Decoder/Scanner/DataReader.swift b/Sources/CBOR/Decoder/Scanner/DataReader.swift index 1ed54d6..68aec55 100644 --- a/Sources/CBOR/Decoder/Scanner/DataReader.swift +++ b/Sources/CBOR/Decoder/Scanner/DataReader.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/20/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif /// A mutable struct used by the `CBORScanner` to iteratively scan a CBOR blob. /// Since this isn't passed by reference, this represents the *entire* blob instead of a single value diff --git a/Sources/CBOR/Encoder/Optimizers/DateOptimizer.swift b/Sources/CBOR/Encoder/Optimizers/DateOptimizer.swift index d9dff9c..83c48bf 100644 --- a/Sources/CBOR/Encoder/Optimizers/DateOptimizer.swift +++ b/Sources/CBOR/Encoder/Optimizers/DateOptimizer.swift @@ -19,7 +19,11 @@ struct StringDateOptimizer: EncodingOptimizer { var contentSize: Int { optimizer.size } init(value: Date) { + #if canImport(FoundationEssentials) + optimizer = SmallStringOptimizer(value: value.ISO8601Format().utf8) + #else optimizer = SmallStringOptimizer(value: ISO8601DateFormatter().string(from: value).utf8) + #endif } mutating func writePayload(to data: inout Slice) { diff --git a/Sources/Fuzzing/AnyDecodable.swift b/Sources/Fuzzing/AnyDecodable.swift index 0c9c2dc..d1cb33b 100644 --- a/Sources/Fuzzing/AnyDecodable.swift +++ b/Sources/Fuzzing/AnyDecodable.swift @@ -4,7 +4,9 @@ // swiftlint:disable line_length // swiftlint:disable type_name -#if canImport(Foundation) +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation #endif @@ -58,11 +60,7 @@ extension _AnyDecodable { let container = try decoder.singleValueContainer() if container.decodeNil() { - #if canImport(Foundation) - self.init(NSNull()) - #else - self.init(Optional.none) - #endif + self.init(Optional.none) } else if let bool = try? container.decode(Bool.self) { self.init(bool) } else if let int = try? container.decode(Int.self) { @@ -86,10 +84,8 @@ extension _AnyDecodable { extension AnyDecodable: Equatable { static func == (lhs: AnyDecodable, rhs: AnyDecodable) -> Bool { switch (lhs.value, rhs.value) { -#if canImport(Foundation) - case is (NSNull, NSNull), is (Void, Void): + case (Optional.none, Optional.none), is (Void, Void): return true -#endif case let (lhs as Bool, rhs as Bool): return lhs == rhs case let (lhs as Int, rhs as Int): diff --git a/Sources/Fuzzing/main.swift b/Sources/Fuzzing/main.swift index 60c2f68..5da6163 100644 --- a/Sources/Fuzzing/main.swift +++ b/Sources/Fuzzing/main.swift @@ -6,7 +6,11 @@ // import CBOR +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif @_optimize(none) func blackhole(_ val: some Any) { } diff --git a/Tests/CBORTests/DecodableTests.swift b/Tests/CBORTests/DecodableTests.swift index 6d427b2..f22a8be 100644 --- a/Tests/CBORTests/DecodableTests.swift +++ b/Tests/CBORTests/DecodableTests.swift @@ -6,7 +6,11 @@ // import Testing +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif @testable import CBOR @Suite diff --git a/Tests/CBORTests/EncodableTests.swift b/Tests/CBORTests/EncodableTests.swift index 1ece21f..73984d7 100644 --- a/Tests/CBORTests/EncodableTests.swift +++ b/Tests/CBORTests/EncodableTests.swift @@ -6,7 +6,11 @@ // import Testing +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif @testable import CBOR @Suite("EncodableTests") diff --git a/Tests/CBORTests/Extensions/Data+ExpressibleByArrayLiteral.swift b/Tests/CBORTests/Extensions/Data+ExpressibleByArrayLiteral.swift index c35e652..daa00b9 100644 --- a/Tests/CBORTests/Extensions/Data+ExpressibleByArrayLiteral.swift +++ b/Tests/CBORTests/Extensions/Data+ExpressibleByArrayLiteral.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/24/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif @testable import CBOR extension Data: @retroactive ExpressibleByArrayLiteral { diff --git a/Tests/CBORTests/Extensions/Data+hexString.swift b/Tests/CBORTests/Extensions/Data+hexString.swift index 5edf9bd..af21869 100644 --- a/Tests/CBORTests/Extensions/Data+hexString.swift +++ b/Tests/CBORTests/Extensions/Data+hexString.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 9/1/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif extension Data { func hexString() -> String { diff --git a/Tests/CBORTests/Extensions/String+asHexData.swift b/Tests/CBORTests/Extensions/String+asHexData.swift index 35be3b7..7b7e55c 100644 --- a/Tests/CBORTests/Extensions/String+asHexData.swift +++ b/Tests/CBORTests/Extensions/String+asHexData.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 9/1/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif extension String { func asHexData() -> Data { diff --git a/Tests/CBORTests/Mock.swift b/Tests/CBORTests/Mock.swift index a906ff5..f9104a2 100644 --- a/Tests/CBORTests/Mock.swift +++ b/Tests/CBORTests/Mock.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/27/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif struct Person: Codable { let name: String diff --git a/Tests/CBORTests/RoundTripTests.swift b/Tests/CBORTests/RoundTripTests.swift index a5a3ae5..4ae0028 100644 --- a/Tests/CBORTests/RoundTripTests.swift +++ b/Tests/CBORTests/RoundTripTests.swift @@ -6,7 +6,11 @@ // import Testing +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif @testable import CBOR @Suite diff --git a/Tests/CBORTests/ScannerTests.swift b/Tests/CBORTests/ScannerTests.swift index 2cf8b1f..4c076fd 100644 --- a/Tests/CBORTests/ScannerTests.swift +++ b/Tests/CBORTests/ScannerTests.swift @@ -5,7 +5,11 @@ // Created by Khan Winter on 8/24/25. // +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif import Testing @testable import CBOR -- 2.51.2