Skip to content

Commit 19b7263

Browse files
authored
Underscore 'internal' methods on Case. (#215)
* Underscore 'internal' methods on Case. * clean up * wip * wip * wip * wip
1 parent 29e0379 commit 19b7263

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

Sources/CasePathsCore/CasePathable.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,30 @@ extension Case {
7575
let keyPath = keyPath.unsafeSendable()
7676
return Case<AppendedValue>(
7777
embed: {
78-
embed(Value.allCasePaths[keyPath: keyPath].embed($0))
78+
_embed(Value.allCasePaths[keyPath: keyPath].embed($0))
7979
},
8080
extract: {
81-
extract(from: $0).flatMap(Value.allCasePaths[keyPath: keyPath].extract)
81+
_extract(from: $0).flatMap(Value.allCasePaths[keyPath: keyPath].extract)
8282
}
8383
)
8484
}
8585

86-
public func embed(_ value: Value) -> Any {
86+
public func _embed(_ value: Value) -> Any {
8787
self._embed(value)
8888
}
8989

90-
public func extract(from root: Any) -> Value? {
90+
public func _extract(from root: Any) -> Value? {
9191
self._extract(root)
9292
}
9393
}
9494

9595
private protocol _AnyCase {
96-
func _extract(from root: Any) -> Any?
96+
func extractAny(from root: Any) -> Any?
9797
}
9898

9999
extension Case: _AnyCase {
100-
fileprivate func _extract(from root: Any) -> Any? {
101-
self.extract(from: root)
100+
fileprivate func extractAny(from root: Any) -> Any? {
101+
self._extract(from: root)
102102
}
103103
}
104104

@@ -202,7 +202,7 @@ extension CaseKeyPath {
202202
/// - Returns: An enum for the case of this key path that holds the given value.
203203
public func callAsFunction<Enum, AssociatedValue>(_ value: AssociatedValue) -> Enum
204204
where Root == Case<Enum>, Value == Case<AssociatedValue> {
205-
Case(self).embed(value) as! Enum
205+
Case(self)._embed(value) as! Enum
206206
}
207207

208208
/// Returns an enum for this case key path's case.
@@ -227,7 +227,7 @@ extension CaseKeyPath {
227227
/// - Returns: An enum for the case of this key path.
228228
public func callAsFunction<Enum>() -> Enum
229229
where Root == Case<Enum>, Value == Case<Void> {
230-
Case(self).embed(()) as! Enum
230+
Case(self)._embed(()) as! Enum
231231
}
232232

233233
/// Whether an argument matches the case key path's case.
@@ -280,8 +280,8 @@ extension _AppendKeyPath {
280280
) -> Enum?
281281
where Self == PartialCaseKeyPath<Enum> {
282282
func open<AnyAssociatedValue>(_ value: AnyAssociatedValue) -> Enum? {
283-
(Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue>)?.embed(value) as? Enum
284-
?? (Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue?>)?.embed(value) as? Enum
283+
(Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue>)?._embed(value) as? Enum
284+
?? (Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue?>)?._embed(value) as? Enum
285285
}
286286
return _openExistential(value, do: open)
287287
}
@@ -324,13 +324,13 @@ extension CasePathable {
324324
/// enum, and see ``Swift/KeyPath/callAsFunction(_:)`` for embedding an associated value in a
325325
/// brand new root enum.
326326
public subscript<Value>(case keyPath: CaseKeyPath<Self, Value>) -> Value? {
327-
Case(keyPath).extract(from: self)
327+
Case(keyPath)._extract(from: self)
328328
}
329329

330330
/// Attempts to extract the associated value from a root enum using a partial case key path.
331331
@_disfavoredOverload
332332
public subscript(case keyPath: PartialCaseKeyPath<Self>) -> Any? {
333-
(Case<Self>()[keyPath: keyPath] as? any _AnyCase)?._extract(from: self)
333+
(Case<Self>()[keyPath: keyPath] as? any _AnyCase)?.extractAny(from: self)
334334
}
335335

336336
/// Replaces the associated value of a root enum at a case key path when the case matches.
@@ -362,8 +362,8 @@ extension CasePathable {
362362
get { fatalError() }
363363
set {
364364
let `case` = Case(keyPath)
365-
guard `case`.extract(from: self) != nil else { return }
366-
self = `case`.embed(newValue) as! Self
365+
guard `case`._extract(from: self) != nil else { return }
366+
self = `case`._embed(newValue) as! Self
367367
}
368368
}
369369

@@ -475,7 +475,7 @@ extension CasePathable {
475475
column: UInt = #column
476476
) {
477477
let `case` = Case(keyPath)
478-
guard var value = `case`.extract(from: self) else {
478+
guard var value = `case`._extract(from: self) else {
479479
reportIssue(
480480
"""
481481
Can't modify '\(String(describing: self))' via 'CaseKeyPath<\(Self.self), \(Value.self)>' \
@@ -489,7 +489,7 @@ extension CasePathable {
489489
return
490490
}
491491
yield(&value)
492-
self = `case`.embed(value) as! Self
492+
self = `case`._embed(value) as! Self
493493
}
494494
}
495495

@@ -500,8 +500,8 @@ extension AnyCasePath {
500500
public init(_ keyPath: CaseKeyPath<Root, Value>) {
501501
let `case` = Case(keyPath)
502502
self.init(
503-
embed: { `case`.embed($0) as! Root },
504-
extract: { `case`.extract(from: $0) }
503+
embed: { `case`._embed($0) as! Root },
504+
extract: { `case`._extract(from: $0) }
505505
)
506506
}
507507
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import CasePaths
2+
3+
@CasePathable
4+
private enum EnumWithExtractAndEmbedCase {
5+
case embed
6+
case extract
7+
}

0 commit comments

Comments
 (0)