|
| 1 | +// Formatters.swift |
| 2 | +// ArgumentEncoding |
| 3 | +// |
| 4 | +// Copyright © 2023 MFB Technologies, Inc. All rights reserved. |
| 5 | + |
| 6 | +import Dependencies |
| 7 | +import XCTestDynamicOverlay |
| 8 | + |
| 9 | +/// Formats `Flag`s to match how different executables format arguments |
| 10 | +public struct FlagFormatter: Sendable { |
| 11 | + /// Formats a key string |
| 12 | + public let prefix: @Sendable () -> String |
| 13 | + public let body: @Sendable (_ key: String) -> String |
| 14 | + |
| 15 | + @Sendable |
| 16 | + public func format(key: String) -> String { |
| 17 | + prefix() + body(key) |
| 18 | + } |
| 19 | + |
| 20 | + @Sendable |
| 21 | + internal func _format(encoding: FlagEncoding) -> String { |
| 22 | + format(key: encoding.key) |
| 23 | + } |
| 24 | + |
| 25 | + /// Initialize a new formatter |
| 26 | + /// |
| 27 | + /// - Parameters |
| 28 | + /// - prefix: Closure that returns the prefix string |
| 29 | + /// - body: Closure that transforms the key string for formatting |
| 30 | + public init( |
| 31 | + prefix: @escaping @Sendable () -> String, |
| 32 | + body: @escaping @Sendable (_ key: String) -> String |
| 33 | + ) { |
| 34 | + self.prefix = prefix |
| 35 | + self.body = body |
| 36 | + } |
| 37 | + |
| 38 | + /// Initialize a new formatter |
| 39 | + /// |
| 40 | + /// - Parameters |
| 41 | + /// - prefix: Name spaced closure that returns the prefix string for a Flag |
| 42 | + /// - body: Name spaced closure that transforms the key string for formatting |
| 43 | + public init(prefix: PrefixFormatter = .empty, body: BodyFormatter = .empty) { |
| 44 | + self.init( |
| 45 | + prefix: prefix.transform, |
| 46 | + body: body.transform |
| 47 | + ) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// Formats `Option`s to match how different executables format arguments |
| 52 | +public struct OptionFormatter: Sendable { |
| 53 | + public let prefix: @Sendable () -> String |
| 54 | + public let body: @Sendable (_ key: String) -> String |
| 55 | + public let separator: @Sendable () -> String |
| 56 | + |
| 57 | + public func format(key: String, value: String) -> String { |
| 58 | + prefix() + body(key) + separator() + value |
| 59 | + } |
| 60 | + |
| 61 | + internal func format(encoding: OptionEncoding) -> String { |
| 62 | + format(key: encoding.key, value: encoding.value) |
| 63 | + } |
| 64 | + |
| 65 | + /// Initialize a new formatter |
| 66 | + /// |
| 67 | + /// - Parameters |
| 68 | + /// - prefix: Closure that returns the prefix string |
| 69 | + /// - body: Closure that transforms the key string for formatting |
| 70 | + /// - separator: Closure that returns the string that separates the key and value |
| 71 | + public init( |
| 72 | + prefix: @escaping @Sendable () -> String, |
| 73 | + body: @escaping @Sendable (_ key: String) -> String, |
| 74 | + separator: @escaping @Sendable () -> String |
| 75 | + ) { |
| 76 | + self.prefix = prefix |
| 77 | + self.body = body |
| 78 | + self.separator = separator |
| 79 | + } |
| 80 | + |
| 81 | + /// Initialize a new formatter |
| 82 | + /// |
| 83 | + /// - Parameters |
| 84 | + /// - prefix: Name spaced closure that returns the prefix string for a Flag |
| 85 | + /// - body: Name spaced closure that transforms the key string for formatting |
| 86 | + /// - separator: Name spaced closure that returns the string that separates the key and value |
| 87 | + public init( |
| 88 | + prefix: PrefixFormatter = .empty, |
| 89 | + body: BodyFormatter = .empty, |
| 90 | + separator: SeparatorFormatter = .space |
| 91 | + ) { |
| 92 | + self.init( |
| 93 | + prefix: prefix.transform, |
| 94 | + body: body.transform, |
| 95 | + separator: separator.transform |
| 96 | + ) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// MARK: Supporting formatters |
| 101 | + |
| 102 | +/// Name space for a closure that returns a string that prefixes a Flag or Option's key |
| 103 | +public struct PrefixFormatter: Sendable { |
| 104 | + public let transform: @Sendable () -> String |
| 105 | + |
| 106 | + public init(_ transform: @escaping @Sendable () -> String) { |
| 107 | + self.transform = transform |
| 108 | + } |
| 109 | + |
| 110 | + public static let empty = Self { "" } |
| 111 | + public static let singleDash = Self { StaticString.singleDash.description } |
| 112 | + public static let doubleDash = Self { StaticString.doubleDash.description } |
| 113 | +} |
| 114 | + |
| 115 | +/// Name space for a closure that transforms a Flag or Option's key |
| 116 | +public struct BodyFormatter: Sendable { |
| 117 | + public let transform: @Sendable (_ key: String) -> String |
| 118 | + |
| 119 | + public init(_ transform: @escaping @Sendable (_ key: String) -> String) { |
| 120 | + self.transform = transform |
| 121 | + } |
| 122 | + |
| 123 | + public static let empty = Self { $0 } |
| 124 | + public static let kebabCase = Self(CaseConverter.kebabCase) |
| 125 | + public static let snakeCase = Self(CaseConverter.snakeCase) |
| 126 | +} |
| 127 | + |
| 128 | +/// Name space for a closure that returns the separator string between an Option's key and value |
| 129 | +public struct SeparatorFormatter: Sendable { |
| 130 | + public let transform: @Sendable () -> String |
| 131 | + |
| 132 | + public init(_ transform: @escaping @Sendable () -> String) { |
| 133 | + self.transform = transform |
| 134 | + } |
| 135 | + |
| 136 | + public static let space = Self { StaticString.space.description } |
| 137 | + public static let equal = Self { StaticString.equal.description } |
| 138 | +} |
| 139 | + |
| 140 | +// MARK: Dependency |
| 141 | + |
| 142 | +extension FlagFormatter: TestDependencyKey { |
| 143 | + public static let testValue: FlagFormatter = .unimplemented |
| 144 | +} |
| 145 | + |
| 146 | +extension DependencyValues { |
| 147 | + public var flagFormatter: FlagFormatter { |
| 148 | + get { self[FlagFormatter.self] } |
| 149 | + set { self[FlagFormatter.self] = newValue } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +extension FlagFormatter { |
| 154 | + public static let unimplemented: FlagFormatter = XCTestDynamicOverlay.unimplemented(placeholder: FlagFormatter()) |
| 155 | +} |
| 156 | + |
| 157 | +extension OptionFormatter: TestDependencyKey { |
| 158 | + public static let testValue: OptionFormatter = .unimplemented |
| 159 | +} |
| 160 | + |
| 161 | +extension DependencyValues { |
| 162 | + public var optionFormatter: OptionFormatter { |
| 163 | + get { self[OptionFormatter.self] } |
| 164 | + set { self[OptionFormatter.self] = newValue } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +extension OptionFormatter { |
| 169 | + public static let unimplemented: OptionFormatter = XCTestDynamicOverlay |
| 170 | + .unimplemented(placeholder: OptionFormatter()) |
| 171 | +} |
0 commit comments