-
Notifications
You must be signed in to change notification settings - Fork 69
/
Enum.swift
76 lines (71 loc) · 2.02 KB
/
Enum.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import CasePaths
extension Conversion {
/// Converts the associated values of an enum case into the case, and an enum case into its
/// associated values.
///
/// Useful for transforming the output of a ``ParserPrinter`` into an enum:
///
/// ```swift
/// @CasePathable
/// enum Expression {
/// case add(Int, Int)
/// ...
/// }
///
/// struct Add: ParserPrinter{
/// var body: some ParserPrinter<Substring, Expression> {
/// ParsePrint(.case(\Expression.Cases.add)) {
/// Int.parser()
/// "+"
/// Int.parser()
/// }
/// }
/// }
///
/// try Add().parse("1+2") // Expression.add(1, 2)
/// ```
///
/// To transform the output of a ``ParserPrinter`` into a struct, see ``memberwise(_:)``.
///
/// - Parameter embed: An embed function where `Values` directly maps to the memory
/// layout of `Enum`, for example the internal, default initializer that is automatically
/// synthesized for structs.
/// - Returns: A conversion that can embed the associated values of an enum case into the case,
/// and extract the associated values from the case.
@inlinable
public static func `case`<Values, Enum: CasePathable>(
_ keyPath: CaseKeyPath<Enum, Values>
) -> Self where Self == AnyCasePath<Enum, Values> {
AnyCasePath(keyPath)
}
@inlinable
public static func `case`<Values, Enum>(
_ initializer: @escaping (Values) -> Enum
) -> Self where Self == AnyCasePath<Enum, Values> {
/initializer
}
@inlinable
public static func `case`<Enum>(
_ initializer: Enum
) -> Self where Self == AnyCasePath<Enum, Void> {
/initializer
}
}
extension AnyCasePath: Conversion {
@inlinable
public func apply(_ input: Value) -> Root {
self.embed(input)
}
@inlinable
public func unapply(_ output: Root) throws -> Value {
guard let value = self.extract(from: output)
else {
throw ConvertingError(
"""
case: Failed to extract \(Value.self) from \(output).
"""
)
}
return value
}
}