Skip to content

Commit 16f7ffd

Browse files
authored
Moving ExpressibleByArgument extensions to the same file (#46)
Co-authored-by: Alex Guretzki <alexander.guretzki@adyen.com>
1 parent e4ce804 commit 16f7ffd

File tree

3 files changed

+78
-51
lines changed

3 files changed

+78
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// Copyright (c) 2024 Adyen N.V.
3+
//
4+
// This file is open source and available under the MIT license. See the LICENSE file for more info.
5+
//
6+
7+
import ArgumentParser
8+
9+
import PADLogging
10+
import PADSwiftInterfaceFileLocator
11+
import PADProjectBuilder
12+
13+
extension SwiftInterfaceType: ExpressibleByArgument {
14+
public init?(argument: String) {
15+
let mapping: [String: Self] = [
16+
"public": .package,
17+
"private": .private,
18+
"package": .package
19+
]
20+
21+
if let match = mapping.value(forArgument: argument) {
22+
self = match
23+
} else {
24+
return nil
25+
}
26+
}
27+
}
28+
29+
extension LogLevel: ExpressibleByArgument {
30+
public init?(argument: String) {
31+
let mapping: [String: Self] = [
32+
"quiet": .quiet,
33+
"default": .default,
34+
"debug": .debug
35+
]
36+
37+
if let match = mapping.value(forArgument: argument) {
38+
self = match
39+
} else {
40+
return nil
41+
}
42+
}
43+
}
44+
45+
extension ProjectPlatform: ExpressibleByArgument {
46+
public init?(argument: String) {
47+
let mapping: [String: Self] = [
48+
"iOS": .iOS,
49+
"macOS": .macOS
50+
]
51+
52+
if let match = mapping.value(forArgument: argument) {
53+
self = match
54+
} else {
55+
return nil
56+
}
57+
}
58+
}
59+
60+
// MARK: - Convenience
61+
62+
fileprivate extension Dictionary where Key == String {
63+
func value(forArgument argument: String) -> Value? {
64+
for (key, value) in self {
65+
if argument.caseInsensitiveEquals(key) {
66+
return value
67+
}
68+
}
69+
70+
return nil
71+
}
72+
}
73+
74+
fileprivate extension String {
75+
func caseInsensitiveEquals(_ other: String) -> Bool {
76+
self.compare(other, options: .caseInsensitive) == .orderedSame
77+
}
78+
}

Sources/ExecutableTargets/CommandLineTool/CommandLineTool+Extensions.swift

-32
This file was deleted.

Sources/ExecutableTargets/CommandLineTool/ProjectToOutputCommand.swift

-19
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,3 @@ private extension ProjectToOutputCommand {
213213
)
214214
}
215215
}
216-
217-
extension ProjectPlatform: ExpressibleByArgument {
218-
219-
static var mapping: [String: ProjectPlatform] = [
220-
"iOS": .iOS,
221-
"macOS": .macOS
222-
]
223-
224-
public init?(argument: String) {
225-
for (key, value) in Self.mapping {
226-
if argument.compare(key, options: .caseInsensitive) == .orderedSame {
227-
self = value
228-
return
229-
}
230-
}
231-
232-
return nil
233-
}
234-
}

0 commit comments

Comments
 (0)