-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathLeafDataRepresentable.swift
More file actions
79 lines (64 loc) 路 2.37 KB
/
Copy pathLeafDataRepresentable.swift
File metadata and controls
79 lines (64 loc) 路 2.37 KB
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
77
78
79
import Foundation
/// Capable of being encoded as `LeafData`.
public protocol LeafDataRepresentable {
/// Converts `self` to `LeafData`, returning `nil` if the conversion is not possible.
var leafData: LeafData { get }
}
// MARK: Default Conformances
extension String: LeafDataRepresentable {
public var leafData: LeafData { .string(self) }
}
extension Substring: LeafDataRepresentable {
public var leafData: LeafData { .string(String(self)) }
}
extension FixedWidthInteger {
public var leafData: LeafData {
guard let valid = Int(exactly: self) else { return .int(nil) }
return .int(Int(valid))
}
}
extension Int8: LeafDataRepresentable {}
extension Int16: LeafDataRepresentable {}
extension Int32: LeafDataRepresentable {}
extension Int64: LeafDataRepresentable {}
extension Int: LeafDataRepresentable {}
extension UInt8: LeafDataRepresentable {}
extension UInt16: LeafDataRepresentable {}
extension UInt32: LeafDataRepresentable {}
extension UInt64: LeafDataRepresentable {}
extension UInt: LeafDataRepresentable {}
extension BinaryFloatingPoint {
public var leafData: LeafData {
guard let valid = Double(exactly: self) else { return .double(nil) }
return .double(Double(valid))
}
}
extension Float: LeafDataRepresentable {}
extension Double: LeafDataRepresentable {}
#if arch(i386) || arch(x86_64)
extension Float80: LeafDataRepresentable {}
#endif
extension Bool: LeafDataRepresentable {
public var leafData: LeafData { .bool(self) }
}
extension UUID: LeafDataRepresentable {
public var leafData: LeafData { .string(LeafConfiguration.stringFormatter(description)) }
}
extension Date: LeafDataRepresentable {
public var leafData: LeafData { .double(timeIntervalSince1970) }
}
extension Array where Element == LeafData {
public var leafData: LeafData { .array(self.map { $0 }) }
}
extension Dictionary where Key == String, Value == LeafData {
public var leafData: LeafData { .dictionary(self.mapValues { $0 }) }
}
extension Set where Element: LeafDataRepresentable {
public var leafData: LeafData { .array(self.map { $0.leafData }) }
}
extension Array where Element: LeafDataRepresentable {
public var leafData: LeafData { .array(self.map { $0.leafData }) }
}
extension Dictionary where Key == String, Value: LeafDataRepresentable {
public var leafData: LeafData { .dictionary(self.mapValues { $0.leafData }) }
}