@@ -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
0 commit comments