-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPositionalArgument.swift
351 lines (304 loc) · 11.3 KB
/
PositionalArgument.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// PositionalArgument.swift
// ArgumentEncoding
//
// This source code is licensed under the MIT License (MIT) found in the
// LICENSE file in the root directory of this source tree.
import Foundation
/// A value only argument type that is not a command or sub-command.
///
/// Because positional argumnents do not have a key, they encode to only their value..
///
/// ```swift
/// struct Container: ArgumentGroup, FormatterNode {
/// let flagFormatter: FlagFormatter = .doubleDashPrefix
/// let optionFormatter: OptionFormatter = OptionFormatter(prefix: .doubleDash)
///
/// @Positional var name: String = "value"
/// }
///
/// Container().arguments() == ["value"]
/// ```
@propertyWrapper
public struct Positional<Value>: PositionalProtocol {
public var wrappedValue: Value
// Different Value types will encode to arguments differently.
// Using unwrap, this can be handled individually per type or collectively by protocol
private let unwrap: @Sendable (Value) -> [String]
var unwrapped: [String] {
unwrap(wrappedValue)
}
func encoding() -> [Command] {
unwrapped.map(Command.init(rawValue:))
}
/// Get the Positional's argument encoding.
/// - Returns: The argument encoding which is an array of strings
public func arguments() -> [String] {
encoding().flatMap { $0.arguments() }
}
/// Initializes a new positional when not used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
/// - unwrap: A closure for mapping a Value to [String]
public init(value: Value, unwrap: @escaping @Sendable (Value) -> [String]) {
wrappedValue = value
self.unwrap = unwrap
}
/// Initializes a new positional when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
/// - _ unwrap: A closure for mapping a Value to [String]
public init(wrappedValue: Value, _ unwrap: @escaping @Sendable (Value) -> [String]) {
self.wrappedValue = wrappedValue
self.unwrap = unwrap
}
}
// MARK: Conditional Conformances
extension Positional: Equatable where Value: Equatable {
public static func == (lhs: Positional<Value>, rhs: Positional<Value>) -> Bool {
lhs.unwrapped == rhs.unwrapped
}
}
extension Positional: Hashable where Value: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(unwrapped)
hasher.combine(ObjectIdentifier(Self.self))
}
}
extension Positional: Sendable where Value: Sendable {}
// MARK: Convenience initializers when Value: CustomStringConvertible
extension Positional where Value: CustomStringConvertible {
/// Initializes a new positional when not used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(value: Value) {
wrappedValue = value
unwrap = Self.unwrap(_:)
}
/// Initializes a new positional when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
unwrap = Self.unwrap(_:)
}
@Sendable
public static func unwrap(_ value: Value) -> [String] {
[value.description]
}
}
extension Positional where Value: RawRepresentable, Value.RawValue: CustomStringConvertible {
/// Initializes a new positional when not used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(value: Value) {
wrappedValue = value
unwrap = Self.unwrap(_:)
}
/// Initializes a new positional when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
unwrap = Self.unwrap(_:)
}
@Sendable
public static func unwrap(_ value: Value) -> [String] {
[value.rawValue.description]
}
}
extension Positional where Value: CustomStringConvertible, Value: RawRepresentable,
Value.RawValue: CustomStringConvertible
{
/// Initializes a new positional when not used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(value: Value) {
wrappedValue = value
unwrap = Self.unwrap(_:)
}
/// Initializes a new positional when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
unwrap = Self.unwrap(_:)
}
@Sendable
public static func unwrap(_ value: Value) -> [String] {
[value.rawValue.description]
}
}
// MARK: Convenience initializers when Value == Positionalal<Wrapped>
extension Positional {
/// Initializes a new positional when not used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init<Wrapped>(value: Wrapped?) where Wrapped: CustomStringConvertible,
Value == Wrapped?
{
wrappedValue = value
unwrap = Self.unwrap(_:)
}
/// Initializes a new positional when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init<Wrapped>(wrappedValue: Wrapped?) where Wrapped: CustomStringConvertible,
Value == Wrapped?
{
self.wrappedValue = wrappedValue
unwrap = Self.unwrap(_:)
}
@Sendable
public static func unwrap<Wrapped>(_ value: Wrapped?) -> [String] where Wrapped: CustomStringConvertible,
Value == Wrapped?
{
[value?.description].compactMap { $0 }
}
}
// MARK: Convenience initializers when Value == Sequence<E>
extension Positional {
/// Initializes a new positional when not used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init<E>(values: Value) where Value: Sequence, Value.Element == E,
E: CustomStringConvertible
{
wrappedValue = values
unwrap = Self.unwrap(_:)
}
/// Initializes a new positional when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init<E>(wrappedValue: Value) where Value: Sequence, Value.Element == E,
E: CustomStringConvertible
{
self.wrappedValue = wrappedValue
unwrap = Self.unwrap(_:)
}
@Sendable
public static func unwrap<E>(_ value: Value) -> [String] where Value: Sequence, Value.Element == E,
E: CustomStringConvertible
{
value.map(\E.description)
}
}
// MARK: Convenience initializers when Value: ArgumentGroup
extension Positional where Value: ArgumentGroup {
/// Initializes a new positional when not used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(value: Value) {
wrappedValue = value
unwrap = Self.unwrap(_:)
}
/// Initializes a new positional when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
unwrap = Self.unwrap(_:)
}
@Sendable
public static func unwrap(_ value: Value) -> [String] {
value.arguments()
}
}
// MARK: ExpressibleBy...Literal conformances
extension Positional: ExpressibleByIntegerLiteral where Value: BinaryInteger, Value.IntegerLiteralType == Int {
public init(integerLiteral value: IntegerLiteralType) {
self.init(wrappedValue: Value(integerLiteral: value)) { [$0.description] }
}
}
#if os(macOS)
extension Positional: ExpressibleByFloatLiteral where Value: BinaryFloatingPoint {
public init(floatLiteral value: FloatLiteralType) {
self.init(wrappedValue: Value(value)) { [$0.formatted()] }
}
}
#endif
extension Positional: ExpressibleByExtendedGraphemeClusterLiteral where Value: StringProtocol {
public init(extendedGraphemeClusterLiteral value: String) {
self.init(wrappedValue: Value(stringLiteral: value))
}
}
extension Positional: ExpressibleByUnicodeScalarLiteral where Value: StringProtocol {
public init(unicodeScalarLiteral value: String) {
self.init(wrappedValue: Value(stringLiteral: value))
}
}
extension Positional: ExpressibleByStringLiteral where Value: StringProtocol {
public init(stringLiteral value: StringLiteralType) {
self.init(wrappedValue: Value(stringLiteral: value))
}
}
extension Positional: ExpressibleByStringInterpolation where Value: StringProtocol {
public init(stringInterpolation: DefaultStringInterpolation) {
self.init(wrappedValue: Value(stringInterpolation: stringInterpolation))
}
}
extension Positional: DecodableWithConfiguration where Value: Decodable {
public init(from decoder: Decoder, configuration: @escaping @Sendable (Value) -> [String]) throws {
let container = try decoder.singleValueContainer()
try self.init(wrappedValue: container.decode(Value.self), configuration)
}
}
// MARK: Coding
extension Positional: Decodable where Value: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
guard let configurationCodingUserInfoKey = Self.configurationCodingUserInfoKey(for: Value.Type.self) else {
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "No CodingUserInfoKey found for accessing the DecodingConfiguration.",
underlyingError: nil
))
}
guard let _configuration = decoder.userInfo[configurationCodingUserInfoKey] else {
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "No DecodingConfiguration found for key: \(configurationCodingUserInfoKey.rawValue)",
underlyingError: nil
))
}
guard let configuration = _configuration as? Self.DecodingConfiguration else {
let desc = "Invalid DecodingConfiguration found for key: \(configurationCodingUserInfoKey.rawValue)"
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: desc,
underlyingError: nil
))
}
try self.init(wrappedValue: container.decode(Value.self), configuration)
}
public static func configurationCodingUserInfoKey(for _: (some Any).Type) -> CodingUserInfoKey? {
CodingUserInfoKey(rawValue: ObjectIdentifier(Self.self).debugDescription)
}
}
extension Positional: Encodable where Value: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(wrappedValue)
}
}
// MARK: Internal Types
/*
Since Positional is generic, we need a single type to cast to in ArgumentGroup.
PositionalProtocol is that type and Positional is the only type that conforms.
*/
protocol PositionalProtocol {
func arguments() -> [String]
}