Skip to content

Commit a1cfa05

Browse files
authored
Merge pull request #3 from MFB-Technologies-Inc/bugfix/remove-rawrep-string-init-for-option
Minor API improvements
2 parents 796754f + cf09382 commit a1cfa05

File tree

8 files changed

+45
-94
lines changed

8 files changed

+45
-94
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![ci](https://github.com/MFB-Technologies-Inc/swift-argument-encoding/actions/workflows/ci.yml/badge.svg)](https://github.com/MFB-Technologies-Inc/swift-argument-encoding/actions/workflows/ci.yml)
44
[![codecov](https://codecov.io/gh/MFB-Technologies-Inc/swift-argument-encoding/branch/main/graph/badge.svg?token=UU95IDUXAX)](https://codecov.io/gh/MFB-Technologies-Inc/swift-argument-encoding)
5+
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FMFB-Technologies-Inc%2Fswift-argument-encoding%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/MFB-Technologies-Inc/swift-argument-encoding)
6+
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FMFB-Technologies-Inc%2Fswift-argument-encoding%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/MFB-Technologies-Inc/swift-argument-encoding)
57

68
A library for encoding types into an Array of Strings, or 'arguments'.
79

Sources/ArgumentEncoding/CommandRepresentable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import Dependencies
3535
/// let arguments = container.arguments()
3636
/// ```
3737
///
38-
/// `CommandRepresentable` inherits from ``ArgumentGroup`` and ``FormatterNode``
39-
public protocol CommandRepresentable: ArgumentGroup, FormatterNode {}
38+
/// `CommandRepresentable` inherits from ``ArgumentGroup``
39+
public protocol CommandRepresentable: ArgumentGroup {}
4040

4141
extension CommandRepresentable {
4242
/// Prefixes the child arguments with an explicit command value.

Sources/ArgumentEncoding/Option.swift

Lines changed: 14 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -127,32 +127,6 @@ extension Option where Value: CustomStringConvertible {
127127
}
128128
}
129129

130-
// MARK: Convenience initializers when Value: RawRepresentable and Value.RawValue == String
131-
132-
extension Option where Value: RawRepresentable, Value.RawValue == String {
133-
/// Initializes a new option when not used as a `@propertyWrapper`
134-
///
135-
/// - Parameters
136-
/// - key: Explicit key value
137-
/// - wrappedValue: The underlying value
138-
public init(key: some CustomStringConvertible, value: Value) {
139-
keyOverride = key.description
140-
wrappedValue = value
141-
unwrap = { [$0.rawValue] }
142-
}
143-
144-
/// Initializes a new option when used as a `@propertyWrapper`
145-
///
146-
/// - Parameters
147-
/// - wrappedValue: The underlying value
148-
/// - _ key: Optional explicit key value
149-
public init(wrappedValue: Value, _ key: String? = nil) {
150-
keyOverride = key
151-
self.wrappedValue = wrappedValue
152-
unwrap = { [$0.rawValue] }
153-
}
154-
}
155-
156130
// MARK: Convenience initializers when Value == Optional<Wrapped>
157131

158132
extension Option {
@@ -181,32 +155,6 @@ extension Option {
181155
self.wrappedValue = wrappedValue
182156
unwrap = { [$0?.description].compactMap { $0 } }
183157
}
184-
185-
/// Initializes a new option when not used as a `@propertyWrapper`
186-
///
187-
/// - Parameters
188-
/// - key: Explicit key value
189-
/// - wrappedValue: The underlying value
190-
public init<Wrapped>(key: some CustomStringConvertible, value: Wrapped?) where Wrapped: RawRepresentable,
191-
Wrapped.RawValue == String, Value == Wrapped?
192-
{
193-
keyOverride = key.description
194-
wrappedValue = value
195-
unwrap = { [$0?.rawValue].compactMap { $0 } }
196-
}
197-
198-
/// Initializes a new option when used as a `@propertyWrapper`
199-
///
200-
/// - Parameters
201-
/// - wrappedValue: The underlying value
202-
/// - _ key: Optional explicit key value
203-
public init<Wrapped>(wrappedValue: Wrapped?, _ key: String? = nil) where Wrapped: RawRepresentable,
204-
Wrapped.RawValue == String, Value == Wrapped?
205-
{
206-
keyOverride = key
207-
self.wrappedValue = wrappedValue
208-
unwrap = { [$0?.rawValue].compactMap { $0 } }
209-
}
210158
}
211159

212160
// MARK: Convenience initializers when Value == Sequence<E>
@@ -237,32 +185,6 @@ extension Option {
237185
self.wrappedValue = wrappedValue
238186
unwrap = { $0.map(\E.description) }
239187
}
240-
241-
/// Initializes a new option when not used as a `@propertyWrapper`
242-
///
243-
/// - Parameters
244-
/// - key: Explicit key value
245-
/// - wrappedValue: The underlying value
246-
public init<E>(key: some CustomStringConvertible, values: Value) where Value: Sequence, Value.Element == E,
247-
E: RawRepresentable, E.RawValue == String
248-
{
249-
keyOverride = key.description
250-
wrappedValue = values
251-
unwrap = { $0.map(\E.rawValue) }
252-
}
253-
254-
/// Initializes a new option when used as a `@propertyWrapper`
255-
///
256-
/// - Parameters
257-
/// - wrappedValue: The underlying value
258-
/// - _ key: Optional explicit key value
259-
public init<E>(wrappedValue: Value, _ key: String? = nil) where Value: Sequence, Value.Element == E,
260-
E: RawRepresentable, E.RawValue == String
261-
{
262-
keyOverride = key
263-
self.wrappedValue = wrappedValue
264-
unwrap = { $0.map(\E.rawValue) }
265-
}
266188
}
267189

268190
// MARK: ExpressibleBy...Literal conformances
@@ -305,6 +227,20 @@ extension Option: ExpressibleByStringInterpolation where Value: StringProtocol {
305227
}
306228
}
307229

230+
extension Option: Decodable where Value: Decodable & CustomStringConvertible {
231+
public init(from decoder: Decoder) throws {
232+
let container = try decoder.singleValueContainer()
233+
self.init(wrappedValue: try container.decode(Value.self))
234+
}
235+
}
236+
237+
extension Option: Encodable where Value: Encodable {
238+
public func encode(to encoder: Encoder) throws {
239+
var container = encoder.singleValueContainer()
240+
try container.encode(wrappedValue)
241+
}
242+
}
243+
308244
// MARK: Internal Types
309245

310246
/*

Sources/ArgumentEncoding/TopLevelCommandRepresentable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/// ```
3535
///
3636
/// `TopLevelCommandRepresentable` inherits from ``ArgumentGroup``, ``FormatterNode``, and ``CommandRepresentable``
37-
public protocol TopLevelCommandRepresentable: CommandRepresentable {
37+
public protocol TopLevelCommandRepresentable: CommandRepresentable, FormatterNode {
3838
func commandValue() -> Command
3939
}
4040

Tests/ArgumentEncodingTests/ArgumentGroupTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ final class ArgumentGroupTests: XCTestCase {
7575
self.buildTests = buildTests
7676
}
7777

78-
enum Configuration: String {
78+
enum Configuration: String, CustomStringConvertible {
7979
case arm64
8080
case x86_64
81+
82+
var description: String { rawValue }
8183
}
8284
}
8385

Tests/ArgumentEncodingTests/CommandRepresentableTests.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import ArgumentEncoding
77
import XCTest
88

99
final class CommandRepresentableTests: XCTestCase {
10-
private struct Container<T>: ArgumentGroup where T: CommandRepresentable {
11-
static var flagFormatter: FlagFormatter { .doubleDashPrefix }
12-
static var optionFormatter: OptionFormatter { .doubleDashPrefix }
10+
private struct Container<T>: ArgumentGroup, FormatterNode where T: CommandRepresentable {
11+
var flagFormatter: FlagFormatter { .doubleDashPrefix }
12+
var optionFormatter: OptionFormatter { .doubleDashPrefix }
1313

1414
var command: T
1515

@@ -18,7 +18,7 @@ final class CommandRepresentableTests: XCTestCase {
1818
}
1919
}
2020

21-
private struct EmptyCommand: CommandRepresentable {
21+
private struct EmptyCommand: CommandRepresentable, FormatterNode {
2222
let flagFormatter: FlagFormatter = .doubleDashPrefix
2323
let optionFormatter: OptionFormatter = .doubleDashPrefix
2424
}
@@ -29,7 +29,7 @@ final class CommandRepresentableTests: XCTestCase {
2929
XCTAssertEqual(args, ["command"])
3030
}
3131

32-
private struct CommandGroup: CommandRepresentable {
32+
private struct CommandGroup: CommandRepresentable, FormatterNode {
3333
let flagFormatter: FlagFormatter = .doubleDashPrefix
3434
let optionFormatter: OptionFormatter = .doubleDashPrefix
3535

@@ -67,7 +67,7 @@ final class CommandRepresentableTests: XCTestCase {
6767
)
6868
}
6969

70-
private struct ParentCommand: CommandRepresentable {
70+
private struct ParentCommand: CommandRepresentable, FormatterNode {
7171
let flagFormatter: FlagFormatter = .doubleDashPrefix
7272
let optionFormatter: OptionFormatter = .doubleDashPrefix
7373

@@ -82,7 +82,7 @@ final class CommandRepresentableTests: XCTestCase {
8282
}
8383
}
8484

85-
private struct ChildCommand: CommandRepresentable {
85+
private struct ChildCommand: CommandRepresentable, FormatterNode {
8686
let flagFormatter: FlagFormatter = .singleDashPrefix
8787
let optionFormatter: OptionFormatter = .singleDashPrefix
8888

@@ -94,9 +94,13 @@ final class CommandRepresentableTests: XCTestCase {
9494
self.buildTests = buildTests
9595
}
9696

97-
enum Configuration: String {
97+
enum Configuration: String, CustomStringConvertible {
9898
case arm64
9999
case x86_64
100+
101+
var description: String {
102+
rawValue
103+
}
100104
}
101105
}
102106

@@ -126,7 +130,7 @@ final class CommandRepresentableTests: XCTestCase {
126130
)
127131
}
128132

129-
private enum ParentEnumCommand: CommandRepresentable {
133+
private enum ParentEnumCommand: CommandRepresentable, FormatterNode {
130134
var flagFormatter: FlagFormatter { .singleDashPrefix }
131135
var optionFormatter: OptionFormatter { .singleDashPrefix }
132136

Tests/ArgumentEncodingTests/TopLevelCommandRepresentableTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ final class TopLevelCommandRepresentableTests: XCTestCase {
7373
self.child = child
7474
}
7575

76-
struct ChildCommand: CommandRepresentable {
76+
struct ChildCommand: CommandRepresentable, FormatterNode {
7777
func commandValue() -> Command { "child" }
7878
let flagFormatter: FlagFormatter = .singleDashPrefix
7979
let optionFormatter: OptionFormatter = .singleDashPrefix
@@ -86,9 +86,11 @@ final class TopLevelCommandRepresentableTests: XCTestCase {
8686
self.buildTests = buildTests
8787
}
8888

89-
enum Configuration: String {
89+
enum Configuration: String, CustomStringConvertible {
9090
case arm64
9191
case x86_64
92+
93+
var description: String { rawValue }
9294
}
9395
}
9496
}
@@ -142,9 +144,11 @@ final class TopLevelCommandRepresentableTests: XCTestCase {
142144
self.buildTests = buildTests
143145
}
144146

145-
enum Configuration: String {
147+
enum Configuration: String, CustomStringConvertible {
146148
case arm64
147149
case x86_64
150+
151+
var description: String { rawValue }
148152
}
149153
}
150154

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage:
2+
status:
3+
patch: false

0 commit comments

Comments
 (0)