Skip to content

Commit e65242a

Browse files
authored
Merge pull request #11 from enuance/CPFeature-009-AddResultType
CPFeature-009 Add The Result Type
2 parents 1865fb0 + 9b5be80 commit e65242a

File tree

9 files changed

+277
-176
lines changed

9 files changed

+277
-176
lines changed

Sources/CommitPrefix/CLInterface/CLIArguments.swift

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,46 +73,46 @@ struct CLIArguments {
7373
self.userEntry = argBuilder.buildUserEntryArgument(parser: parser)
7474
}
7575

76-
private func singleCommandParse(_ allCommands: [ParsedCommand]) throws -> UserCommand {
76+
private func singleCommandParse(_ allCommands: [ParsedCommand]) -> Result<UserCommand, CPError> {
7777
precondition(allCommands.count == 1, "Intended for single Parsed Command only!")
7878
guard let foundCommand = allCommands.first else {
79-
throw CPError.commandNotRecognized
79+
return .failure(.commandNotRecognized)
8080
}
8181

8282
switch foundCommand {
8383
case .outputVersion:
84-
return .outputVersion
84+
return .success(.outputVersion)
8585
case .outputPrefixes:
86-
return .outputPrefixes
86+
return .success(.outputPrefixes)
8787
case .deletePrefixes:
88-
return .deletePrefixes
88+
return .success(.deletePrefixes)
8989
case .modeNormal:
90-
return .modeNormal
90+
return .success(.modeNormal)
9191
case .userEntry(value: let prefixes):
92-
return .newPrefixes(value: prefixes)
92+
return .success(.newPrefixes(value: prefixes))
9393
default:
94-
throw CPError.commandNotRecognized
94+
return .failure(.commandNotRecognized)
9595
}
9696
}
9797

98-
private func doubleCommandParse(_ allCommands: [ParsedCommand]) throws -> UserCommand {
98+
private func doubleCommandParse(_ allCommands: [ParsedCommand]) -> Result<UserCommand, CPError> {
9999
precondition(allCommands.count == 2, "Intended for two Parsed Commands only!")
100100
let firstCommand = allCommands[0]
101101
let secondCommand = allCommands[1]
102102

103103
switch (firstCommand, secondCommand) {
104104
case (.modeBranchParse, .userEntry(value: let validator)):
105-
return .modeBranchParse(validator: validator)
105+
return .success(.modeBranchParse(validator: validator))
106106
case (.userEntry(value: let validator), .modeBranchParse):
107-
return .modeBranchParse(validator: validator)
107+
return .success(.modeBranchParse(validator: validator))
108108
default:
109-
throw CPError.commandNotRecognized
109+
return .failure(.commandNotRecognized)
110110
}
111111
}
112112

113-
func getCommand() throws -> UserCommand {
113+
private func getCommand() -> Result<UserCommand, CPError> {
114114
guard let parsedArgs = try? parser.parse(rawArgs) else {
115-
throw CPError.commandNotRecognized
115+
return .failure(.commandNotRecognized)
116116
}
117117

118118
var allCommands = [ParsedCommand]()
@@ -123,24 +123,32 @@ struct CLIArguments {
123123
parsedArgs.get(modeNormal).map { _ in allCommands.append(.modeNormal) }
124124
parsedArgs.get(modeBranchParse).map { _ in allCommands.append(.modeBranchParse) }
125125

126-
try parsedArgs.get(userEntry).map { userEntry in
127-
let noMoreThanOneEntry = userEntry.count < 2
128-
guard noMoreThanOneEntry else { throw CPError.invalidEntryFormat }
129-
guard let theEntry = userEntry.first else { throw CPError.emptyEntry }
130-
allCommands.append(.userEntry(value: theEntry))
126+
do {
127+
try parsedArgs.get(userEntry).map { userEntry in
128+
let noMoreThanOneEntry = userEntry.count < 2
129+
guard noMoreThanOneEntry else { throw CPError.invalidEntryFormat }
130+
guard let theEntry = userEntry.first else { throw CPError.emptyEntry }
131+
allCommands.append(.userEntry(value: theEntry))
132+
}
133+
} catch let cpError as CPError {
134+
return .failure(cpError)
135+
} catch {
136+
return .failure(.unexpectedError)
131137
}
132138

133139
switch allCommands.count {
134140
case 0:
135-
return .viewState
141+
return .success(.viewState)
136142
case 1:
137-
return try singleCommandParse(allCommands)
143+
return singleCommandParse(allCommands)
138144
case 2:
139-
return try doubleCommandParse(allCommands)
145+
return doubleCommandParse(allCommands)
140146
default:
141-
throw CPError.tooManyArguments
147+
return .failure(.tooManyArguments)
142148
}
143149

144150
}
145151

152+
var command: UserCommand { getCommand().resolveOrExit() }
153+
146154
}

Sources/CommitPrefix/CPInteractor.swift

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ struct CPInteractor {
4141
self.gitHEADFile = gitHEADFile
4242
}
4343

44+
static func create(_ gitDirectory: Folder) -> Result<CPInteractor, CPError> {
45+
do {
46+
let cpInteractor = try CPInteractor(gitDirectory: gitDirectory)
47+
return .success(cpInteractor)
48+
} catch let cpError as CPError {
49+
return .failure(cpError)
50+
} catch {
51+
return .failure(.unexpectedError)
52+
}
53+
}
54+
4455
private static func build(using gitDirectory: Folder) throws -> (File, CPModel, File) {
4556
do {
4657
let initialModelData = try JSONEncoder().encode(CPModel.empty())
@@ -58,35 +69,36 @@ struct CPInteractor {
5869
}
5970
}
6071

61-
private func saveCommitPrefix(model: CPModel) throws {
72+
private func saveCommitPrefix(model: CPModel) -> Result<Void, CPError> {
6273
do {
6374
let jsonEncoder = JSONEncoder()
6475
let modelData = try jsonEncoder.encode(model)
6576
try commitPrefixFile.write(modelData)
77+
return .success(())
6678
} catch {
6779
cpDebugPrint(error)
68-
throw CPError.cpFileIOError
80+
return .failure(.cpFileIOError)
6981
}
7082
}
7183

72-
private func branchPrefixes() throws -> [String] {
84+
private func branchPrefixes() -> Result<[String], CPError> {
7385
guard let regexValue = commitPrefixModel.regexValue else {
74-
throw CPError.branchValidatorNotFound
86+
return .failure(.branchValidatorNotFound)
7587
}
7688

7789
guard let branch = try? gitHEADFile.readAsString(encodedAs: .utf8) else {
78-
throw CPError.headFileIOError
90+
return .failure(.headFileIOError)
7991
}
8092

8193
let matches = branch.occurances(ofRegex: regexValue)
8294

8395
guard matches.count > 0 else {
8496
let validator = commitPrefixModel.branchValidator ?? "Validator Not Present"
85-
throw CPError.invalidBranchPrefix(validator: validator)
97+
return .failure(.invalidBranchPrefix(validator: validator))
8698
}
8799

88100
let uniqueMatches = Set(matches)
89-
return Array(uniqueMatches)
101+
return .success(Array(uniqueMatches))
90102
}
91103

92104
private func prefixFormatter(_ rawValue: String) -> [String] {
@@ -97,79 +109,91 @@ struct CPInteractor {
97109
return parsedValues.map { "[\($0)]" }
98110
}
99111

100-
private func validatorFormatter(_ rawValue: String) throws -> String {
112+
private func validatorFormatter(_ rawValue: String) -> Result<String, CPError> {
101113
let validator = rawValue.trimmingCharacters(in: .whitespacesAndNewlines)
102114
let containsNoNumbers = validator.occurances(ofRegex: #"(\d+)"#).isEmpty
103115
let atLeastTwoCharacters = validator.count > 1
104116
guard containsNoNumbers && atLeastTwoCharacters else {
105-
throw CPError.invalidBranchValidatorFormat
117+
return .failure(.invalidBranchValidatorFormat)
106118
}
107-
return validator
119+
return .success(validator)
108120
}
109121

110-
func outputPrefixes() throws -> ConslerOutput {
122+
func outputPrefixes() -> Result<ConslerOutput, CPError> {
111123
switch commitPrefixModel.prefixMode {
112124
case .normal:
113-
return ConslerOutput(commitPrefixModel.prefixes.joined())
125+
return .success(ConslerOutput(commitPrefixModel.prefixes.joined()))
114126
case .branchParse:
115-
let retrievedBranchPrefixes = try branchPrefixes()
116-
let branchPrefixes = retrievedBranchPrefixes.map { "[\($0)]" }.joined()
117-
let normalPrefixes = commitPrefixModel.prefixes.joined()
118-
return ConslerOutput(branchPrefixes, normalPrefixes)
127+
return branchPrefixes().map {
128+
let branchPrefixes = $0.map { "[\($0)]" }.joined()
129+
let normalPrefixes = commitPrefixModel.prefixes.joined()
130+
return ConslerOutput(branchPrefixes, normalPrefixes)
131+
}
119132
}
120133
}
121134

122-
func getCommitPrefixState() throws -> CPState {
135+
func getCommitPrefixState() -> Result<CPState, CPError> {
123136
switch commitPrefixModel.prefixMode {
124137
case .normal:
125-
return CPState(
138+
return .success(CPState(
126139
mode: .normal,
127140
branchPrefixes: [],
128141
normalPrefixes: commitPrefixModel.prefixes
129-
)
142+
))
130143
case .branchParse:
131-
let retrievedBranchPrefixes = try branchPrefixes()
132-
let branchPrefixes = retrievedBranchPrefixes.map { "[\($0)]" }
133-
let normalPrefixes = commitPrefixModel.prefixes
134-
return CPState(
135-
mode: .branchParse,
136-
branchPrefixes: branchPrefixes,
137-
normalPrefixes: normalPrefixes
138-
)
144+
return branchPrefixes().map {
145+
let branchPrefixes = $0.map { "[\($0)]" }
146+
let normalPrefixes = commitPrefixModel.prefixes
147+
return CPState(
148+
mode: .branchParse,
149+
branchPrefixes: branchPrefixes,
150+
normalPrefixes: normalPrefixes
151+
)
152+
}
139153
}
140154
}
141155

142-
func deletePrefixes() throws -> ConslerOutput {
156+
func deletePrefixes() -> Result<ConslerOutput, CPError> {
143157
let newModel = commitPrefixModel.updated(with: [])
144-
try saveCommitPrefix(model: newModel)
145-
return ConslerOutput("CommitPrefix ", "DELETED").describedBy(.normal, .red)
158+
return saveCommitPrefix(model: newModel)
159+
.transform(ConslerOutput(
160+
"CommitPrefix ", "DELETED")
161+
.describedBy(.normal, .red))
146162
}
147163

148-
func writeNew(prefixes rawValue: String) throws -> ConslerOutput {
164+
func writeNew(prefixes rawValue: String) -> Result<ConslerOutput, CPError> {
149165
let newPrefixes = prefixFormatter(rawValue)
150166
let newModel = commitPrefixModel.updated(with: newPrefixes)
151-
try saveCommitPrefix(model: newModel)
152-
return ConslerOutput("CommitPrefix ", "STORED ", newPrefixes.joined())
153-
.describedBy(.normal, .green, .green)
167+
return saveCommitPrefix(model: newModel)
168+
.transform(ConslerOutput(
169+
"CommitPrefix ", "STORED ", newPrefixes.joined())
170+
.describedBy(.normal, .green, .green))
154171
}
155172

156-
func activateBranchMode(with validator: String) throws -> ConslerOutput {
157-
let formattedValidator = try validatorFormatter(validator)
158-
let newModel = commitPrefixModel.updatedAsBranchMode(with: formattedValidator)
159-
try saveCommitPrefix(model: newModel)
160-
return ConslerOutput("CommitPrefix ","MODE BRANCH_PARSE ", formattedValidator)
161-
.describedBy(.normal, .cyan, .green)
173+
func activateBranchMode(with validator: String) -> Result<ConslerOutput, CPError> {
174+
switch validatorFormatter(validator) {
175+
case let .success(formattedValidator):
176+
let newModel = commitPrefixModel.updatedAsBranchMode(with: formattedValidator)
177+
return saveCommitPrefix(model: newModel)
178+
.transform(ConslerOutput(
179+
"CommitPrefix ","MODE BRANCH_PARSE ", formattedValidator)
180+
.describedBy(.normal, .cyan, .green))
181+
case let .failure(cpError):
182+
return .failure(cpError)
183+
}
162184
}
163185

164-
func activateNormalMode() throws -> ConslerOutput {
186+
func activateNormalMode() -> Result<ConslerOutput, CPError> {
165187
switch commitPrefixModel.prefixMode {
166188
case .normal:
167-
return ConslerOutput("CommitPrefix ", "already in ", "MODE NORMAL")
168-
.describedBy(.normal, .yellow, .cyan)
189+
return .success(ConslerOutput("CommitPrefix ", "already in ", "MODE NORMAL")
190+
.describedBy(.normal, .yellow, .cyan))
169191
case .branchParse:
170192
let newModel = commitPrefixModel.updatedAsNormalMode()
171-
try saveCommitPrefix(model: newModel)
172-
return ConslerOutput("CommitPrefix ", "MODE NORMAL").describedBy(.normal, .cyan)
193+
return saveCommitPrefix(model: newModel)
194+
.transform(ConslerOutput(
195+
"CommitPrefix ", "MODE NORMAL")
196+
.describedBy(.normal, .cyan))
173197
}
174198
}
175199

Sources/CommitPrefix/CPInterface.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ protocol CPInterface {
3131

3232
func outputVersion() -> ConslerOutput
3333

34-
func outputPrefixes() throws -> ConslerOutput
34+
func outputPrefixes() -> ConslerOutput
3535

36-
func viewState() throws -> ConslerOutput
36+
func viewState() -> ConslerOutput
3737

38-
func deletePrefixes() throws -> ConslerOutput
38+
func deletePrefixes() -> ConslerOutput
3939

40-
func writeNew(prefixes rawValue: String) throws -> ConslerOutput
40+
func writeNew(prefixes rawValue: String) -> ConslerOutput
4141

42-
func activateBranchMode(with validator: String) throws -> ConslerOutput
42+
func activateBranchMode(with validator: String) -> ConslerOutput
4343

44-
func activateNormalMode() throws -> ConslerOutput
44+
func activateNormalMode() -> ConslerOutput
4545

4646
}

0 commit comments

Comments
 (0)