Skip to content

Commit aa63210

Browse files
Rename cases
1 parent 50eb415 commit aa63210

File tree

2 files changed

+89
-97
lines changed

2 files changed

+89
-97
lines changed

Sources/LightTableDelta/Delta.swift

Lines changed: 83 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -25,71 +25,63 @@ public enum Delta<Element>: ~Copyable where Element: ~Copyable {
2525

2626
/// A source element.
2727
///
28-
/// Conceptually, this is a value that was deleted and thus no target element is available.
29-
case deleted(source: Element)
28+
/// Conceptually, this case represents a value where the element was deleted and thus no target element is available.
29+
case source(Element)
3030
/// A target element.
3131
///
32-
/// Conceptually, this is a value that was added and thus no source element is available.
33-
case added(target: Element)
34-
/// A source element and a target element.
32+
/// Conceptually, this case represents a value where the element was added and thus no source element is available.
33+
case target(Element)
34+
/// The combination of a source element and a target element.
3535
///
36-
/// Conceptually, this is a value that was modified and both the source and the target element are available.
37-
/// The source and target elements can be different or equal.
38-
case modified(source: Element, target: Element)
36+
/// Conceptually, this case represents a value where an element was modified or kept the same and thus both a source and a target element are available.
37+
case transition(source: Element, target: Element)
3938
}
4039

4140
public extension Delta where Element: ~Copyable {
42-
/// Creates a modified delta from a source and a target element.
41+
/// Creates a transition delta.
4342
@inlinable @inline(__always)
4443
init(source: consuming Element, target: consuming Element) {
4544
self = .transition(source: source, target: target)
4645
}
4746

48-
/// Creates a delta from a source and a target element.
49-
///
50-
/// If the source element is `nil`, the delta is `.added(target:)`.
51-
/// Otherwise, the delta is `.modified(source:target:)`.
47+
/// Creates a target delta if `source` is `nil`; otherwise, creates a transition delta.
5248
@inlinable
5349
init(source: consuming Element?, target: consuming Element) {
5450
if let source {
55-
self = .modified(source: source, target: target)
51+
self = .transition(source: source, target: target)
5652
}
5753
else {
58-
self = .added(target: target)
54+
self = .target(target)
5955
}
6056
}
6157

62-
/// Creates a delta from a source and a target element.
63-
///
64-
/// If the target element is `nil`, the delta is `.deleted(source:)`.
65-
/// Otherwise, the delta is `.modified(source:target:)`.
58+
/// Creates a source delta if `target` is `nil`; otherwise, creates a transition delta.
6659
@inlinable
6760
init(source: consuming Element, target: consuming Element?) {
6861
if let target {
69-
self = .modified(source: source, target: target)
62+
self = .transition(source: source, target: target)
7063
}
7164
else {
72-
self = .deleted(source: source)
65+
self = .source(source)
7366
}
7467
}
7568

76-
/// Creates a delta from a source and a target element.
69+
/// Creates a delta when one or both elements are non-`nil`; otherwise, returns `nil`.
7770
///
78-
/// If both the source and the target element are `nil`, the delta is `nil`.
79-
/// If the source element is `nil`, the delta is `.added(target:)`.
80-
/// If the target element is `nil`, the delta is `.deleted(source:)`.
81-
/// Otherwise, the delta is `.modified(source:target:)`.
71+
/// - If both the source and target are non-`nil`, creates a transition delta.
72+
/// - Else, if the source is non-`nil`, creates a source delta.
73+
/// - Else, if the target is non-`nil`, creates a target delta.
74+
/// - Otherwise, returns `nil`.
8275
@inlinable
8376
init?(source: consuming Element?, target: consuming Element?) {
84-
if source != nil && target != nil {
85-
// `if let source, let target` does not work with non-copyable types here
86-
self = .modified(source: source!, target: target!)
77+
if let source = source.take(), let target = target.take() {
78+
self = .transition(source: source, target: target)
8779
}
8880
else if let source {
89-
self = .deleted(source: source)
81+
self = .source(source)
9082
}
9183
else if let target {
92-
self = .added(target: target)
84+
self = .target(target)
9385
}
9486
else {
9587
return nil
@@ -104,12 +96,12 @@ public extension Delta where Element: ~Copyable {
10496
_ transform: (consuming Element) throws(E) -> T
10597
) throws(E) -> Delta<T> {
10698
switch consume self {
107-
case .deleted(let source):
108-
.deleted(source: try transform(source))
109-
case .added(let target):
110-
.added(target: try transform(target))
111-
case .modified(let source, let target):
112-
.modified(source: try transform(source), target: try transform(target))
99+
case .source(let source):
100+
.source(try transform(source))
101+
case .target(let target):
102+
.target(try transform(target))
103+
case .transition(let source, let target):
104+
.transition(source: try transform(source), target: try transform(target))
113105
}
114106
}
115107

@@ -119,22 +111,22 @@ public extension Delta where Element: ~Copyable {
119111
_ transform: (consuming Element) throws(E) -> T?
120112
) throws(E) -> Delta<T>? {
121113
switch consume self {
122-
case .deleted(let source):
114+
case .source(let source):
123115
guard let source = try transform(source) else {
124116
return nil
125117
}
126-
return .deleted(source: source)
127-
case .added(let target):
118+
return .source(source)
119+
case .target(let target):
128120
guard let target = try transform(target) else {
129121
return nil
130122
}
131-
return .added(target: target)
132-
case .modified(let source, let target):
123+
return .target(target)
124+
case .transition(let source, let target):
133125
guard let source = try transform(source),
134126
let target = try transform(target) else {
135127
return nil
136128
}
137-
return .modified(source: source, target: target)
129+
return .transition(source: source, target: target)
138130
}
139131
}
140132

@@ -147,15 +139,15 @@ public extension Delta where Element: ~Copyable {
147139
switch side {
148140
case .source:
149141
switch consume self {
150-
case .deleted(let source): source
151-
case .added(let target): target
152-
case .modified(let source, _): source
142+
case .source(let source): source
143+
case .target(let target): target
144+
case .transition(let source, _): source
153145
}
154146
case .target:
155147
switch consume self {
156-
case .deleted(let source): source
157-
case .added(let target): target
158-
case .modified(_, let target): target
148+
case .source(let source): source
149+
case .target(let target): target
150+
case .transition(_, let target): target
159151
}
160152
}
161153
}
@@ -166,93 +158,93 @@ public extension Delta where Element: ~Copyable {
166158
combine: (consuming Element, consuming Element) throws(E) -> Element
167159
) throws(E) -> Element {
168160
switch consume self {
169-
case .deleted(let source):
161+
case .source(let source):
170162
source
171-
case .added(let target):
163+
case .target(let target):
172164
target
173-
case .modified(let source, let target):
165+
case .transition(let source, let target):
174166
try combine(source, target)
175167
}
176168
}
177169
}
178170

179171
extension Delta: Copyable where Element: Copyable {
180-
/// Returns a modified delta where both the source and target share the same element.
172+
/// Returns a transition delta where both the source and target share the same element.
181173
@inlinable @inline(__always)
182-
public static func equal(_ element: Element) -> Self {
183-
.modified(source: element, target: element)
174+
public static func transition(_ element: Element) -> Self {
175+
.transition(source: element, target: element)
184176
}
185177

186-
/// The source element, if the delta value is not of type `.added`.
178+
/// The source element, if available; otherwise, `nil`.
187179
@inlinable @inline(__always)
188180
public var source: Element? {
189181
switch self {
190-
case .deleted(let source): source
191-
case .added(_): nil
192-
case .modified(let source, _): source
182+
case .source(let source): source
183+
case .target(_): nil
184+
case .transition(let source, _): source
193185
}
194186
}
195187

196-
/// The target element, if the delta value is not of type `.deleted`.
188+
/// The target element, if available; otherwise, `nil`.
197189
@inlinable @inline(__always)
198190
public var target: Element? {
199191
switch self {
200-
case .deleted(_): nil
201-
case .added(let target): target
202-
case .modified(_, let target): target
192+
case .source(_): nil
193+
case .target(let target): target
194+
case .transition(_, let target): target
203195
}
204196
}
205197

206198
/// Returns a delta containing the results of mapping the given closure over the delta’s elements.
207199
///
208-
/// In the `.modified` case, `transform` is applied concurrently to both sides.
200+
/// In the transition case, both elements are transformed concurrently.
209201
@available(macOS 10.15, iOS 13, tvOS 13, visionOS 1, watchOS 6, *)
210202
@inlinable
211203
public func asyncMap<T>(
212-
_ transform: @Sendable (consuming Element) async -> T
204+
_ transform: @Sendable (Element) async -> T
213205
) async -> Delta<T> where Element: Sendable {
214206
switch self {
215-
case .deleted(let source):
216-
return .deleted(source: await transform(source))
217-
case .added(let target):
218-
return .added(target: await transform(target))
219-
case .modified(let source, let target):
207+
case .source(let source):
208+
return .source(await transform(source))
209+
case .target(let target):
210+
return .target(await transform(target))
211+
case .transition(let source, let target):
220212
async let transformedSource = transform(source)
221213
async let transformedTarget = transform(target)
222-
return await .modified(source: transformedSource, target: transformedTarget)
214+
return await .transition(source: transformedSource, target: transformedTarget)
223215
}
224216
}
225217

226218
/// Returns a delta containing the results of mapping the given closure over the delta’s elements.
227219
///
228-
/// In the `.modified` case, `transform` is applied concurrently to both sides.
220+
/// In the transition case, both elements are transformed concurrently.
229221
@available(macOS 10.15, iOS 13, tvOS 13, visionOS 1, watchOS 6, *)
230222
@inlinable
231223
public func asyncMap<T>(
232-
_ transform: @Sendable (consuming Element) async throws -> T
224+
_ transform: @Sendable (Element) async throws -> T
233225
) async throws -> Delta<T> where Element: Sendable {
234226
switch self {
235-
case .deleted(let source):
236-
return .deleted(source: try await transform(source))
237-
case .added(let target):
238-
return .added(target: try await transform(target))
239-
case .modified(let source, let target):
227+
case .source(let source):
228+
return .source(try await transform(source))
229+
case .target(let target):
230+
return .target(try await transform(target))
231+
case .transition(let source, let target):
240232
async let transformedSource = transform(source)
241233
async let transformedTarget = transform(target)
242-
return try await .modified(source: transformedSource, target: transformedTarget)
234+
return try await .transition(source: transformedSource, target: transformedTarget)
243235
}
244236
}
245237
}
246238

247239
extension Delta: CustomDebugStringConvertible {
248240
public var debugDescription: String {
249241
switch self {
250-
case .deleted(let source):
251-
"Delta.deleted(\(source))"
252-
case .added(let target):
253-
"Delta.added(\(target))"
254-
case .modified(let source, let target):
255-
"Delta.modified(\(source), \(target))"
242+
case .source(let source):
243+
"Delta(source: \(source))"
244+
case .target(let target):
245+
"Delta(target: \(target))"
246+
case .transition(let source, let target):
247+
"Delta(source: \(source), target: \(target))"
256248
}
257249
}
258250
}
@@ -271,13 +263,13 @@ public extension Delta where Element: ~Copyable {
271263
extension Delta: Encodable where Element: Encodable {
272264
public func encode(to encoder: any Encoder) throws {
273265
switch self {
274-
case .deleted(let source):
266+
case .source(let source):
275267
var container = encoder.container(keyedBy: CodingKeys.self)
276268
try container.encode(source, forKey: .source)
277-
case .added(let target):
269+
case .target(let target):
278270
var container = encoder.container(keyedBy: CodingKeys.self)
279271
try container.encode(target, forKey: .target)
280-
case .modified(let source, let target):
272+
case .transition(let source, let target):
281273
var container = encoder.container(keyedBy: CodingKeys.self)
282274
try container.encode(source, forKey: .source)
283275
try container.encode(target, forKey: .target)
@@ -292,13 +284,13 @@ extension Delta: Decodable where Element: Decodable {
292284
let target = try container.decodeIfPresent(Element.self, forKey: .target)
293285

294286
if let source, let target {
295-
self = .modified(source: source, target: target)
287+
self = .transition(source: source, target: target)
296288
}
297289
else if let source {
298-
self = .deleted(source: source)
290+
self = .source(source)
299291
}
300292
else if let target {
301-
self = .added(target: target)
293+
self = .target(target)
302294
}
303295
else {
304296
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "No source or target value."))

Tests/LightTableDeltaTests/LightTableDeltaTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import LightTableDelta
66
let encoder = JSONEncoder()
77
encoder.outputFormatting = .sortedKeys
88

9-
let jsonDataDeleted = try encoder.encode(Delta.deleted(source: 3))
9+
let jsonDataDeleted = try encoder.encode(Delta.source(3))
1010
let jsonDeleted = String(decoding: jsonDataDeleted, as: UTF8.self)
1111
#expect(jsonDeleted == #"{"A":3}"#)
1212

13-
let jsonDataAdded = try encoder.encode(Delta.added(target: 5))
13+
let jsonDataAdded = try encoder.encode(Delta.target(5))
1414
let jsonAdded = String(decoding: jsonDataAdded, as: UTF8.self)
1515
#expect(jsonAdded == #"{"B":5}"#)
1616

17-
let jsonDataModified = try encoder.encode(Delta.modified(source: 3, target: 5))
17+
let jsonDataModified = try encoder.encode(Delta.transition(source: 3, target: 5))
1818
let jsonModified = String(decoding: jsonDataModified, as: UTF8.self)
1919
#expect(jsonModified == #"{"A":3,"B":5}"#)
2020
}
@@ -24,15 +24,15 @@ import LightTableDelta
2424

2525
let jsonDataDeleted = Data(#"{"A":3}"#.utf8)
2626
let deltaDeleted = try decoder.decode(Delta<Int>.self, from: jsonDataDeleted)
27-
#expect(deltaDeleted == .deleted(source: 3))
27+
#expect(deltaDeleted == .source(3))
2828

2929
let jsonDataAdded = Data( #"{"B":5}"#.utf8)
3030
let deltaAdded = try decoder.decode(Delta<Int>.self, from: jsonDataAdded)
31-
#expect(deltaAdded == .added(target: 5))
31+
#expect(deltaAdded == .target(5))
3232

3333
let jsonDataModified = Data(#"{"A":3,"B":5}"#.utf8)
3434
let deltaModified = try decoder.decode(Delta<Int>.self, from: jsonDataModified)
35-
#expect(deltaModified == .modified(source: 3, target: 5))
35+
#expect(deltaModified == .transition(source: 3, target: 5))
3636

3737
let jsonDataEmpty = Data("{}".utf8)
3838
#expect(throws: DecodingError.self, performing: { try decoder.decode(Delta<Int>.self, from: jsonDataEmpty) })

0 commit comments

Comments
 (0)