Skip to content

Commit

Permalink
feat: Get rid of the Codextended dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrenskers committed Feb 18, 2025
1 parent 813081e commit c763b47
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
6 changes: 1 addition & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/kylef/PathKit", from: "1.0.1"),
.package(url: "https://github.com/JohnSundell/Codextended.git", from: "0.1.0"),
//.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.1.0"),
],
targets: [
.target(
name: "Saga",
dependencies: [
"PathKit",
"Codextended",
]
dependencies: ["PathKit"]
),
.executableTarget(
name: "SagaCLI",
Expand Down
50 changes: 49 additions & 1 deletion Sources/Saga/MetadataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Foundation

public final class MetadataDecoder: Decoder {
internal final class MetadataDecoder: Decoder {
public var userInfo: [CodingUserInfoKey : Any] { [:] }
public let codingPath: [CodingKey]

Expand Down Expand Up @@ -690,3 +690,51 @@ private extension Array {
return array
}
}

internal func makeMetadataDecoder(for metadata: [String: String]) -> MetadataDecoder {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = .current

return MetadataDecoder(
metadata: metadata,
dateFormatter: dateFormatter
)
}

internal func resolveDate(from decoder: MetadataDecoder) throws -> Date? {
return try decoder.decodeIfPresent("date", as: Date.self)
}

private struct AnyCodingKey: CodingKey {
var stringValue: String
var intValue: Int?

init(_ string: String) {
stringValue = string
}

init?(stringValue: String) {
self.stringValue = stringValue
}

init?(intValue: Int) {
self.intValue = intValue
self.stringValue = String(intValue)
}
}

private extension Decoder {
/// Decode an optional value for a given key, specified as a string. Throws an error if the
/// specified key exists but is not able to be decoded as the inferred type.
func decodeIfPresent<T: Decodable>(_ key: String, as type: T.Type = T.self) throws -> T? {
return try decodeIfPresent(AnyCodingKey(key), as: type)
}

/// Decode an optional value for a given key, specified as a `CodingKey`. Throws an error if the
/// specified key exists but is not able to be decoded as the inferred type.
func decodeIfPresent<T: Decodable, K: CodingKey>(_ key: K, as type: T.Type = T.self) throws -> T? {
let container = try self.container(keyedBy: K.self)
return try container.decodeIfPresent(type, forKey: key)
}
}
20 changes: 0 additions & 20 deletions Sources/Saga/Reader+Decoder.swift

This file was deleted.

0 comments on commit c763b47

Please sign in to comment.