Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/build-embeded.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Embedded
on:
push:
branches:
- main
- 'release/**'
pull_request:
branches:
- main
- 'release/**'
jobs:
embedded-build:
name: Build
runs-on: ubuntu-24.04
steps:
- name: Checkout source
uses: actions/checkout@v4
- uses: SwiftyLab/setup-swift@latest
with:
swift-version: "6.2.3"
- name: Embedded
run: swift build -c release --target TOMLDecoder --disable-default-traits -Xswiftc -target -Xswiftc x86_64-unknown-linux-gnu -Xswiftc -enable-experimental-feature -Xswiftc Embedded -Xswiftc -wmo
4 changes: 3 additions & 1 deletion .github/workflows/build-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ jobs:
uses: actions/checkout@v4
- name: Set up Swift for WASM
run: swift sdk install https://download.swift.org/swift-6.2.3-release/wasm-sdk/swift-6.2.3-RELEASE/swift-6.2.3-RELEASE_wasm.artifactbundle.tar.gz --checksum 394040ecd5260e68bb02f6c20aeede733b9b90702c2204e178f3e42413edad2a
- name: Run tests
- name: Build
run: swift build --swift-sdk swift-6.2.3-RELEASE_wasm
- name: Build without Codable
run: swift build --swift-sdk swift-6.2.3-RELEASE_wasm --disable-default-traits
6 changes: 6 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
uses: actions/checkout@v4
- name: Run tests
run: swift test
- name: Run tests without Codable
if: matrix.swift_version != '6.0'
run: swift test --disable-default-traits

swift-test-macos:
name: ${{ matrix.os_name }} (Swift ${{ matrix.swift_version }})
Expand All @@ -50,6 +53,9 @@ jobs:
uses: actions/checkout@v4
- name: Run tests
run: swift test
- name: Run tests without Codable
if: matrix.swift_version != '6.0'
run: swift test --disable-default-traits

swift-test-simulator:
name: ${{ matrix.platform }} ${{ matrix.os_version }} (Swift 6.2)
Expand Down
17 changes: 16 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
SHELL = /bin/bash
export LANG = en_US.UTF-8
export LC_CTYPE = en_US.UTF-8
EMBED_SWIFT ?= $(shell \
if command -v swiftly >/dev/null 2>&1; then \
toolchain="$$(swiftly use --print-location 2>/dev/null | head -n1)"; \
if [ -x "$$toolchain/usr/bin/swift" ]; then \
echo "$$toolchain/usr/bin/swift"; \
else \
command -v swift; \
fi; \
else \
command -v swift; \
fi \
)

.DEFAULT_GOAL := format

.PHONY: build test generate-code generate-tests benchmark format docs
.PHONY: build test embedded generate-code generate-tests benchmark format docs

docs:
@Scripts/generate-docs.sh /
Expand All @@ -18,6 +30,9 @@ generate-tests:
build: generate-code
@swift build -c release -Xswiftc -warnings-as-errors > /dev/null

embedded: generate-code
@$(EMBED_SWIFT) build -c release --target TOMLDecoder --disable-default-traits -Xswiftc -target -Xswiftc arm64-apple-macos14.0 -Xswiftc -enable-experimental-feature -Xswiftc Embedded -Xswiftc -wmo

test: generate-tests
@swift test -Xswiftc -warnings-as-errors

Expand Down
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 6.0
// swift-tools-version: 6.1

import Foundation
import PackageDescription
Expand Down Expand Up @@ -89,6 +89,10 @@ let package = Package(
.executable(name: "compliance", targets: ["compliance"]),
.library(name: "TOMLDecoder", targets: ["TOMLDecoder"]),
],
traits: [
"CodableSupport",
.default(enabledTraits: ["CodableSupport"]),
],
dependencies: benchmarksDeps + docsDeps + formattingDeps,
targets: targets + testTargets + benchmarkTargets,
cxxLanguageStandard: .cxx20
Expand Down
52 changes: 52 additions & 0 deletions Package@swift-6.0.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// swift-tools-version: 6.0

import PackageDescription

let codableSupportEnabled: [SwiftSetting] = [.define("CodableSupport")]

let package = Package(
name: "TOMLDecoder",
platforms: [.iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macOS(.v10_15), .visionOS(.v1)],
products: [
.executable(name: "compliance", targets: ["compliance"]),
.library(name: "TOMLDecoder", targets: ["TOMLDecoder"]),
],
targets: [
.executableTarget(
name: "compliance",
dependencies: ["TOMLDecoder"]
),
.target(
name: "TOMLDecoder",
exclude: ["gyb"],
swiftSettings: codableSupportEnabled + [
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("InternalImportsByDefault"),
.enableUpcomingFeature("MemberImportVisibility"),
]
),
.target(
name: "Resources",
exclude: ["fixtures"]
),
.target(
name: "ProlepticGregorianTestHelpers",
publicHeadersPath: "include"
),
.testTarget(
name: "TOMLDecoderTests",
dependencies: [
"ProlepticGregorianTestHelpers",
"Resources",
"TOMLDecoder",
],
exclude: [
"gyb",
"invalid_fixtures",
"valid_fixtures",
],
swiftSettings: codableSupportEnabled
),
],
cxxLanguageStandard: .cxx20
)
4 changes: 4 additions & 0 deletions Scripts/generate-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def _generate_valid_test_file(fixtures: Iterable[str], commit: str, spec_version
"""// Generated by Scripts/generate-tests.py
// Source: toml-test commit __COMMIT__ (spec __SPEC_VERSION__)

#if CodableSupport
import Foundation
import Testing
import TOMLDecoder
Expand All @@ -233,6 +234,7 @@ def _generate_valid_test_file(fixtures: Iterable[str], commit: str, spec_version

__TESTS__
}
#endif
"""
)

Expand Down Expand Up @@ -271,6 +273,7 @@ def _generate_invalid_test_file(fixtures: Iterable[str], commit: str, spec_versi
"""// Generated by Scripts/generate-tests.py
// Source: toml-test commit __COMMIT__ (spec __SPEC_VERSION__)

#if CodableSupport
import Foundation
import Testing
import TOMLDecoder
Expand All @@ -291,6 +294,7 @@ def _generate_invalid_test_file(fixtures: Iterable[str], commit: str, spec_versi

__TESTS__
}
#endif
"""
)

Expand Down
40 changes: 30 additions & 10 deletions Sources/TOMLDecoder/DateTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// > This means for ancient dates,
/// > ``OffsetDateTime`` may disagree with `Foundation.Date` on how much time has passed since a reference date.
/// > For modern dates, there's no difference between the two.
public struct OffsetDateTime: Equatable, Hashable, Sendable, Codable, CustomStringConvertible {
public struct OffsetDateTime: Equatable, Hashable, Sendable, CustomStringConvertible {
/// The date component of the offset date-time.
public var date: LocalDate
/// The time component of the offset date-time.
Expand Down Expand Up @@ -48,13 +48,20 @@ public struct OffsetDateTime: Equatable, Hashable, Sendable, Codable, CustomStri
self.features = features
}

#if CodableSupport
public init(from decoder: any Decoder) throws {
if let decoder = decoder as? _TOMLDecoder {
self = try decoder.decode(OffsetDateTime.self)
} else {
try self.init(from: decoder)
return
}

let container = try decoder.container(keyedBy: CodingKeys.self)
date = try container.decode(LocalDate.self, forKey: .date)
time = try container.decode(LocalTime.self, forKey: .time)
offset = try container.decode(Int16.self, forKey: .offset)
features = try container.decode(Features.self, forKey: .features)
}
#endif

/// Create a new offset date-time from it's members.
///
Expand Down Expand Up @@ -183,7 +190,7 @@ public struct OffsetDateTime: Equatable, Hashable, Sendable, Codable, CustomStri
///
/// A parser can preserve features of a offset date-time string with this type.
/// If neither lowercase nor uppercase 'T' is present, the date-time seprator is a space, which is allowed by TOML.
public struct Features: OptionSet, Hashable, Sendable, Codable {
public struct Features: OptionSet, Hashable, Sendable {
/// The raw value of the features.
public let rawValue: UInt8

Expand Down Expand Up @@ -213,7 +220,7 @@ public struct OffsetDateTime: Equatable, Hashable, Sendable, Codable, CustomStri
/// A local date-time as defined by TOML.
///
/// ``LocalDateTime`` stores fractional seconds to the nanosecond precision.
public struct LocalDateTime: Equatable, Hashable, Sendable, Codable, CustomStringConvertible {
public struct LocalDateTime: Equatable, Hashable, Sendable, CustomStringConvertible {
/// The date component of the local date-time.
public var date: LocalDate
/// The time component of the local date-time.
Expand All @@ -231,13 +238,18 @@ public struct LocalDateTime: Equatable, Hashable, Sendable, Codable, CustomStrin
self.time = time
}

#if CodableSupport
public init(from decoder: any Decoder) throws {
if let decoder = decoder as? _TOMLDecoder {
self = try decoder.decode(LocalDateTime.self)
} else {
try self.init(from: decoder)
return
}

let container = try decoder.container(keyedBy: CodingKeys.self)
date = try container.decode(LocalDate.self, forKey: .date)
time = try container.decode(LocalTime.self, forKey: .time)
}
#endif

/// Create a new local date-time from it's members.
///
Expand Down Expand Up @@ -271,7 +283,7 @@ public struct LocalDateTime: Equatable, Hashable, Sendable, Codable, CustomStrin
/// A local time as defined by TOML.
///
/// ``LocalTime`` stores fractional seconds to the nanosecond precision.
public struct LocalTime: Equatable, Hashable, Sendable, Codable, CustomStringConvertible {
public struct LocalTime: Equatable, Hashable, Sendable, CustomStringConvertible {
/// The hour component of the local time.
public var hour: UInt8
/// The minute component of the local time.
Expand Down Expand Up @@ -340,7 +352,7 @@ public struct LocalTime: Equatable, Hashable, Sendable, Codable, CustomStringCon
/// A local date as defined by TOML.
///
/// ``LocalDate`` stores the year, month, and day components of a date.
public struct LocalDate: Equatable, Hashable, Sendable, Codable, CustomStringConvertible {
public struct LocalDate: Equatable, Hashable, Sendable, CustomStringConvertible {
/// The year component of the local date.
/// Valid range is [1, 9999].
public var year: UInt16
Expand Down Expand Up @@ -431,7 +443,15 @@ extension String {
}
}

#if canImport(Foundation)
#if CodableSupport
extension OffsetDateTime: Codable {}
extension OffsetDateTime.Features: Codable {}
extension LocalDateTime: Codable {}
extension LocalTime: Codable {}
extension LocalDate: Codable {}
#endif

#if CodableSupport
public import Foundation

extension DateComponents {
Expand Down
Loading
Loading