This commit is contained in:
41
Sources/CodersClient/Coders.swift
Normal file
41
Sources/CodersClient/Coders.swift
Normal file
@@ -0,0 +1,41 @@
|
||||
import Dependencies
|
||||
import DependenciesMacros
|
||||
import Foundation
|
||||
import TOMLKit
|
||||
|
||||
public extension DependencyValues {
|
||||
|
||||
/// Holds onto decoders and encoders for json and toml files.
|
||||
var coders: Coders {
|
||||
get { self[Coders.self] }
|
||||
set { self[Coders.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
@DependencyClient
|
||||
public struct Coders: Sendable {
|
||||
public var jsonDecoder: @Sendable () -> JSONDecoder = { .init() }
|
||||
public var jsonEncoder: @Sendable () -> JSONEncoder = { .init() }
|
||||
public var tomlDecoder: @Sendable () -> TOMLDecoder = { .init() }
|
||||
public var tomlEncoder: @Sendable () -> TOMLEncoder = { .init() }
|
||||
}
|
||||
|
||||
extension Coders: DependencyKey {
|
||||
|
||||
public static var testValue: Self { Self() }
|
||||
|
||||
public static var liveValue: Self {
|
||||
.init(
|
||||
jsonDecoder: { JSONDecoder() },
|
||||
jsonEncoder: { defaultJsonEncoder },
|
||||
tomlDecoder: { TOMLDecoder() },
|
||||
tomlEncoder: { TOMLEncoder() }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private let defaultJsonEncoder: JSONEncoder = {
|
||||
var encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.prettyPrinted, .withoutEscapingSlashes]
|
||||
return encoder
|
||||
}()
|
||||
Reference in New Issue
Block a user