Skip to content

Commit c908cc9

Browse files
Implement CodableWithConfiguration
1 parent 979ac27 commit c908cc9

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ The `Delta` type also conforms to all standard protocols (depending on the confo
2525
- `Hashable`
2626
- `CustomDebugStringConvertible`
2727
- `Encodable`
28+
- `EncodableWithConfiguration`
2829
- `Decodable`
30+
- `DecodableWithConfiguration`
2931
- `Sendable`
3032
- `BitwiseCopyable`
3133
- `~Copyable`

Sources/LightTableDelta/Delta.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,30 @@ extension Delta: Encodable where Element: Encodable {
298298
}
299299
}
300300

301+
#if canImport(Foundation)
302+
import Foundation
303+
304+
@available(macOS 12, *)
305+
extension Delta: EncodableWithConfiguration where Element: EncodableWithConfiguration {
306+
public typealias EncodingConfiguration = Element.EncodingConfiguration
307+
308+
public func encode(to encoder: any Encoder, configuration: Element.EncodingConfiguration) throws {
309+
switch self {
310+
case .source(let source):
311+
var container = encoder.container(keyedBy: CodingKeys.self)
312+
try container.encode(source, forKey: .source, configuration: configuration)
313+
case .target(let target):
314+
var container = encoder.container(keyedBy: CodingKeys.self)
315+
try container.encode(target, forKey: .target, configuration: configuration)
316+
case .transition(let source, let target):
317+
var container = encoder.container(keyedBy: CodingKeys.self)
318+
try container.encode(source, forKey: .source, configuration: configuration)
319+
try container.encode(target, forKey: .target, configuration: configuration)
320+
}
321+
}
322+
}
323+
#endif
324+
301325
extension Delta: Decodable where Element: Decodable {
302326
public init(from decoder: any Decoder) throws {
303327
let container = try decoder.container(keyedBy: CodingKeys.self)
@@ -319,6 +343,34 @@ extension Delta: Decodable where Element: Decodable {
319343
}
320344
}
321345

346+
#if canImport(Foundation)
347+
import Foundation
348+
349+
@available(macOS 12, *)
350+
extension Delta: DecodableWithConfiguration where Element: DecodableWithConfiguration {
351+
public typealias DecodingConfiguration = Element.DecodingConfiguration
352+
353+
public init(from decoder: any Decoder, configuration: Element.DecodingConfiguration) throws {
354+
let container = try decoder.container(keyedBy: CodingKeys.self)
355+
let source = try container.decodeIfPresent(Element.self, forKey: .source, configuration: configuration)
356+
let target = try container.decodeIfPresent(Element.self, forKey: .target, configuration: configuration)
357+
358+
if let source, let target {
359+
self = .transition(source: source, target: target)
360+
}
361+
else if let source {
362+
self = .source(source)
363+
}
364+
else if let target {
365+
self = .target(target)
366+
}
367+
else {
368+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "No source or target value."))
369+
}
370+
}
371+
}
372+
#endif
373+
322374
extension Delta: Sendable where Element: Sendable {}
323375

324376
extension Delta: BitwiseCopyable where Element: BitwiseCopyable {}

0 commit comments

Comments
 (0)