Skip to content

Commit

Permalink
Merge pull request #3 from MFB-Technologies-Inc/bugfix/remove-rawrep-…
Browse files Browse the repository at this point in the history
…string-init-for-option

Minor API improvements
  • Loading branch information
roanutil authored Mar 7, 2023
2 parents 796754f + cf09382 commit a1cfa05
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 94 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![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)
[![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)
[![](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)
[![](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)

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

Expand Down
4 changes: 2 additions & 2 deletions Sources/ArgumentEncoding/CommandRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import Dependencies
/// let arguments = container.arguments()
/// ```
///
/// `CommandRepresentable` inherits from ``ArgumentGroup`` and ``FormatterNode``
public protocol CommandRepresentable: ArgumentGroup, FormatterNode {}
/// `CommandRepresentable` inherits from ``ArgumentGroup``
public protocol CommandRepresentable: ArgumentGroup {}

extension CommandRepresentable {
/// Prefixes the child arguments with an explicit command value.
Expand Down
92 changes: 14 additions & 78 deletions Sources/ArgumentEncoding/Option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,6 @@ extension Option where Value: CustomStringConvertible {
}
}

// MARK: Convenience initializers when Value: RawRepresentable and Value.RawValue == String

extension Option where Value: RawRepresentable, Value.RawValue == String {
/// Initializes a new option when not used as a `@propertyWrapper`
///
/// - Parameters
/// - key: Explicit key value
/// - wrappedValue: The underlying value
public init(key: some CustomStringConvertible, value: Value) {
keyOverride = key.description
wrappedValue = value
unwrap = { [$0.rawValue] }
}

/// Initializes a new option when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
/// - _ key: Optional explicit key value
public init(wrappedValue: Value, _ key: String? = nil) {
keyOverride = key
self.wrappedValue = wrappedValue
unwrap = { [$0.rawValue] }
}
}

// MARK: Convenience initializers when Value == Optional<Wrapped>

extension Option {
Expand Down Expand Up @@ -181,32 +155,6 @@ extension Option {
self.wrappedValue = wrappedValue
unwrap = { [$0?.description].compactMap { $0 } }
}

/// Initializes a new option when not used as a `@propertyWrapper`
///
/// - Parameters
/// - key: Explicit key value
/// - wrappedValue: The underlying value
public init<Wrapped>(key: some CustomStringConvertible, value: Wrapped?) where Wrapped: RawRepresentable,
Wrapped.RawValue == String, Value == Wrapped?
{
keyOverride = key.description
wrappedValue = value
unwrap = { [$0?.rawValue].compactMap { $0 } }
}

/// Initializes a new option when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
/// - _ key: Optional explicit key value
public init<Wrapped>(wrappedValue: Wrapped?, _ key: String? = nil) where Wrapped: RawRepresentable,
Wrapped.RawValue == String, Value == Wrapped?
{
keyOverride = key
self.wrappedValue = wrappedValue
unwrap = { [$0?.rawValue].compactMap { $0 } }
}
}

// MARK: Convenience initializers when Value == Sequence<E>
Expand Down Expand Up @@ -237,32 +185,6 @@ extension Option {
self.wrappedValue = wrappedValue
unwrap = { $0.map(\E.description) }
}

/// Initializes a new option when not used as a `@propertyWrapper`
///
/// - Parameters
/// - key: Explicit key value
/// - wrappedValue: The underlying value
public init<E>(key: some CustomStringConvertible, values: Value) where Value: Sequence, Value.Element == E,
E: RawRepresentable, E.RawValue == String
{
keyOverride = key.description
wrappedValue = values
unwrap = { $0.map(\E.rawValue) }
}

/// Initializes a new option when used as a `@propertyWrapper`
///
/// - Parameters
/// - wrappedValue: The underlying value
/// - _ key: Optional explicit key value
public init<E>(wrappedValue: Value, _ key: String? = nil) where Value: Sequence, Value.Element == E,
E: RawRepresentable, E.RawValue == String
{
keyOverride = key
self.wrappedValue = wrappedValue
unwrap = { $0.map(\E.rawValue) }
}
}

// MARK: ExpressibleBy...Literal conformances
Expand Down Expand Up @@ -305,6 +227,20 @@ extension Option: ExpressibleByStringInterpolation where Value: StringProtocol {
}
}

extension Option: Decodable where Value: Decodable & CustomStringConvertible {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.init(wrappedValue: try container.decode(Value.self))
}
}

extension Option: Encodable where Value: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(wrappedValue)
}
}

// MARK: Internal Types

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/// ```
///
/// `TopLevelCommandRepresentable` inherits from ``ArgumentGroup``, ``FormatterNode``, and ``CommandRepresentable``
public protocol TopLevelCommandRepresentable: CommandRepresentable {
public protocol TopLevelCommandRepresentable: CommandRepresentable, FormatterNode {
func commandValue() -> Command
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/ArgumentEncodingTests/ArgumentGroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ final class ArgumentGroupTests: XCTestCase {
self.buildTests = buildTests
}

enum Configuration: String {
enum Configuration: String, CustomStringConvertible {
case arm64
case x86_64

var description: String { rawValue }
}
}

Expand Down
22 changes: 13 additions & 9 deletions Tests/ArgumentEncodingTests/CommandRepresentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import ArgumentEncoding
import XCTest

final class CommandRepresentableTests: XCTestCase {
private struct Container<T>: ArgumentGroup where T: CommandRepresentable {
static var flagFormatter: FlagFormatter { .doubleDashPrefix }
static var optionFormatter: OptionFormatter { .doubleDashPrefix }
private struct Container<T>: ArgumentGroup, FormatterNode where T: CommandRepresentable {
var flagFormatter: FlagFormatter { .doubleDashPrefix }
var optionFormatter: OptionFormatter { .doubleDashPrefix }

var command: T

Expand All @@ -18,7 +18,7 @@ final class CommandRepresentableTests: XCTestCase {
}
}

private struct EmptyCommand: CommandRepresentable {
private struct EmptyCommand: CommandRepresentable, FormatterNode {
let flagFormatter: FlagFormatter = .doubleDashPrefix
let optionFormatter: OptionFormatter = .doubleDashPrefix
}
Expand All @@ -29,7 +29,7 @@ final class CommandRepresentableTests: XCTestCase {
XCTAssertEqual(args, ["command"])
}

private struct CommandGroup: CommandRepresentable {
private struct CommandGroup: CommandRepresentable, FormatterNode {
let flagFormatter: FlagFormatter = .doubleDashPrefix
let optionFormatter: OptionFormatter = .doubleDashPrefix

Expand Down Expand Up @@ -67,7 +67,7 @@ final class CommandRepresentableTests: XCTestCase {
)
}

private struct ParentCommand: CommandRepresentable {
private struct ParentCommand: CommandRepresentable, FormatterNode {
let flagFormatter: FlagFormatter = .doubleDashPrefix
let optionFormatter: OptionFormatter = .doubleDashPrefix

Expand All @@ -82,7 +82,7 @@ final class CommandRepresentableTests: XCTestCase {
}
}

private struct ChildCommand: CommandRepresentable {
private struct ChildCommand: CommandRepresentable, FormatterNode {
let flagFormatter: FlagFormatter = .singleDashPrefix
let optionFormatter: OptionFormatter = .singleDashPrefix

Expand All @@ -94,9 +94,13 @@ final class CommandRepresentableTests: XCTestCase {
self.buildTests = buildTests
}

enum Configuration: String {
enum Configuration: String, CustomStringConvertible {
case arm64
case x86_64

var description: String {
rawValue
}
}
}

Expand Down Expand Up @@ -126,7 +130,7 @@ final class CommandRepresentableTests: XCTestCase {
)
}

private enum ParentEnumCommand: CommandRepresentable {
private enum ParentEnumCommand: CommandRepresentable, FormatterNode {
var flagFormatter: FlagFormatter { .singleDashPrefix }
var optionFormatter: OptionFormatter { .singleDashPrefix }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ final class TopLevelCommandRepresentableTests: XCTestCase {
self.child = child
}

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

enum Configuration: String {
enum Configuration: String, CustomStringConvertible {
case arm64
case x86_64

var description: String { rawValue }
}
}
}
Expand Down Expand Up @@ -142,9 +144,11 @@ final class TopLevelCommandRepresentableTests: XCTestCase {
self.buildTests = buildTests
}

enum Configuration: String {
enum Configuration: String, CustomStringConvertible {
case arm64
case x86_64

var description: String { rawValue }
}
}

Expand Down
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage:
status:
patch: false

0 comments on commit a1cfa05

Please sign in to comment.