Skip to content

Commit b6d85f6

Browse files
committed
Refactor code to use Result and Box
1 parent bc2419c commit b6d85f6

File tree

7 files changed

+34
-33
lines changed

7 files changed

+34
-33
lines changed

Commandant/ArgumentParser.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import Foundation
10-
import LlamaKit
10+
import Result
1111

1212
/// Represents an argument passed on the command line.
1313
private enum RawArgument: Equatable {
@@ -131,13 +131,13 @@ public final class ArgumentParser {
131131
}
132132
}
133133

134-
return failure(missingArgumentError("--\(key)"))
134+
return .failure(missingArgumentError("--\(key)"))
135135
} else {
136136
rawArguments.append(arg)
137137
}
138138
}
139139

140-
return success(foundValue)
140+
return .success(foundValue)
141141
}
142142

143143
/// Returns the next positional argument that hasn't yet been returned, or

Commandant/Command.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import Foundation
10-
import LlamaKit
10+
import Result
1111

1212
/// Represents a subcommand that can be executed with its own set of arguments.
1313
public protocol CommandType {
@@ -120,12 +120,12 @@ extension CommandRegistry {
120120
exit(EXIT_SUCCESS)
121121

122122
case let .Some(.Failure(error)):
123-
switch error.unbox {
123+
switch error.value {
124124
case let .UsageError(description):
125125
fputs(description + "\n", stderr)
126126

127127
case let .CommandError(error):
128-
errorHandler(error.unbox)
128+
errorHandler(error.value)
129129
}
130130

131131
exit(EXIT_FAILURE)

Commandant/Errors.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//
88

99
import Foundation
10-
import LlamaKit
10+
import Box
11+
import Result
1112

1213
/// Possible errors that can originate from Commandant.
1314
///

Commandant/HelpCommand.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import Foundation
10-
import LlamaKit
10+
import Result
1111

1212
/// A basic implementation of a `help` command, using information available in a
1313
/// `CommandRegistry`.
@@ -56,7 +56,7 @@ public struct HelpCommand<ClientError>: CommandType {
5656
println(" \(formattedVerb) \(command.function)")
5757
}
5858

59-
return success(())
59+
return .success(())
6060
}
6161
}
6262
}

Commandant/Option.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import Foundation
10-
import LlamaKit
10+
import Result
1111

1212
/// Represents a record of options for a command, which can be parsed from
1313
/// a list of command-line arguments.
@@ -167,17 +167,17 @@ public func <*> <T, U, ClientError>(f: T -> U, value: Result<T, CommandantError<
167167
public func <*> <T, U, ClientError>(f: Result<(T -> U), CommandantError<ClientError>>, value: Result<T, CommandantError<ClientError>>) -> Result<U, CommandantError<ClientError>> {
168168
switch (f, value) {
169169
case let (.Failure(left), .Failure(right)):
170-
return failure(combineUsageErrors(left.unbox, right.unbox))
170+
return .failure(combineUsageErrors(left.value, right.value))
171171

172172
case let (.Failure(left), .Success):
173-
return failure(left.unbox)
173+
return .failure(left.value)
174174

175175
case let (.Success, .Failure(right)):
176-
return failure(right.unbox)
176+
return .failure(right.value)
177177

178178
case let (.Success(f), .Success(value)):
179-
let newValue = f.unbox(value.unbox)
180-
return success(newValue)
179+
let newValue = f.value(value.value)
180+
return .success(newValue)
181181
}
182182
}
183183

@@ -192,12 +192,12 @@ public func <| <T: ArgumentType, ClientError>(mode: CommandMode, option: Option<
192192
if let key = option.key {
193193
switch arguments.consumeValueForKey(key) {
194194
case let .Success(value):
195-
stringValue = value.unbox
195+
stringValue = value.value
196196

197197
case let .Failure(error):
198-
switch error.unbox {
198+
switch error.value {
199199
case let .UsageError(description):
200-
return failure(.UsageError(description: description))
200+
return .failure(.UsageError(description: description))
201201

202202
case .CommandError:
203203
fatalError("CommandError should be impossible when parameterized over NoError")
@@ -209,18 +209,18 @@ public func <| <T: ArgumentType, ClientError>(mode: CommandMode, option: Option<
209209

210210
if let stringValue = stringValue {
211211
if let value = T.fromString(stringValue) {
212-
return success(value)
212+
return .success(value)
213213
}
214214

215-
return failure(option.invalidUsageError(stringValue))
215+
return .failure(option.invalidUsageError(stringValue))
216216
} else if let defaultValue = option.defaultValue {
217-
return success(defaultValue)
217+
return .success(defaultValue)
218218
} else {
219-
return failure(missingArgumentError(option.description))
219+
return .failure(missingArgumentError(option.description))
220220
}
221221

222222
case .Usage:
223-
return failure(informativeUsageError(option))
223+
return .failure(informativeUsageError(option))
224224
}
225225
}
226226

@@ -234,14 +234,14 @@ public func <| <ClientError>(mode: CommandMode, option: Option<Bool>) -> Result<
234234
switch mode {
235235
case let .Arguments(arguments):
236236
if let value = arguments.consumeBooleanKey(option.key!) {
237-
return success(value)
237+
return .success(value)
238238
} else if let defaultValue = option.defaultValue {
239-
return success(defaultValue)
239+
return .success(defaultValue)
240240
} else {
241-
return failure(missingArgumentError(option.description))
241+
return .failure(missingArgumentError(option.description))
242242
}
243243

244244
case .Usage:
245-
return failure(informativeUsageError(option))
245+
return .failure(informativeUsageError(option))
246246
}
247247
}

Commandant/Switch.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Copyright (c) 2015 Carthage. All rights reserved.
77
//
88

9-
import LlamaKit
9+
import Result
1010

1111
/// Describes a parameterless command line flag that defaults to false and can only
1212
/// be switched on. Canonical examples include `--force` and `--recurse`.
@@ -56,9 +56,9 @@ public func <| <ClientError> (mode: CommandMode, option: Switch) -> Result<Bool,
5656
if let flag = option.flag {
5757
enabled = arguments.consumeBooleanFlag(flag)
5858
}
59-
return success(enabled)
59+
return .success(enabled)
6060

6161
case .Usage:
62-
return failure(informativeUsageError(option.description, option.usage))
62+
return .failure(informativeUsageError(option.description, option.usage))
6363
}
6464
}

CommandantTests/OptionSpec.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import Commandant
1010
import Foundation
11-
import LlamaKit
1211
import Nimble
1312
import Quick
13+
import Result
1414

1515
enum NoError {}
1616

@@ -22,11 +22,11 @@ class OptionsTypeSpec: QuickSpec {
2222
}
2323

2424
it("should fail if a required argument is missing") {
25-
expect(tryArguments().isSuccess).to(beFalsy())
25+
expect(tryArguments().value).to(beNil())
2626
}
2727

2828
it("should fail if an option is missing a value") {
29-
expect(tryArguments("required", "--intValue").isSuccess).to(beFalsy())
29+
expect(tryArguments("required", "--intValue").value).to(beNil())
3030
}
3131

3232
it("should succeed without optional arguments") {

0 commit comments

Comments
 (0)