@@ -5,18 +5,26 @@ extern crate tokei;
5
5
6
6
use colored:: Color ;
7
7
use colored:: * ;
8
- use git2:: { Error , Repository } ;
8
+ use git2:: Repository ;
9
9
use license:: License ;
10
10
use std:: {
11
11
convert:: From ,
12
+ error,
12
13
ffi:: OsStr ,
13
14
fmt,
14
15
fmt:: Write ,
15
16
fs,
16
- process:: { exit, Command , Stdio } ,
17
+ process:: { Command , Stdio } ,
18
+ result,
17
19
str:: FromStr ,
18
20
} ;
19
21
22
+ macro_rules! err {
23
+ ( $( $tt: tt) * ) => { Err ( Box :: <error:: Error >:: from( format!( $( $tt) * ) ) ) }
24
+ }
25
+
26
+ type Result < T > = result:: Result < T , Box < error:: Error > > ;
27
+
20
28
struct Info {
21
29
project_name : String ,
22
30
language : Language ,
@@ -51,12 +59,7 @@ impl fmt::Display for Info {
51
59
"Author: "
52
60
} ;
53
61
54
- writeln ! (
55
- buffer,
56
- "{}{}" ,
57
- title. color( color) . bold( ) ,
58
- self . authors[ 0 ]
59
- ) ?;
62
+ writeln ! ( buffer, "{}{}" , title. color( color) . bold( ) , self . authors[ 0 ] ) ?;
60
63
61
64
let title = " " . repeat ( title. len ( ) ) ;
62
65
@@ -144,40 +147,43 @@ impl fmt::Display for Language {
144
147
}
145
148
}
146
149
147
- fn main ( ) {
150
+ fn main ( ) -> Result < ( ) > {
148
151
let tokei_langs = project_languages ( ) ;
149
152
let language = match get_dominant_language ( & tokei_langs) {
150
153
Some ( language) => language,
151
154
None => {
152
- eprintln ! ( "Error: Could not find any source code in this directory." ) ;
153
- exit ( 1 ) ;
155
+ return err ! ( "Could not find any source code in this directory." ) ;
154
156
}
155
157
} ;
156
158
157
159
if !is_git_installed ( ) {
158
- eprintln ! ( "Error: Could not execute git for project information." ) ;
159
- exit ( 1 ) ;
160
+ return err ! ( "Could not execute git for project information." ) ;
160
161
}
161
162
162
163
let authors = get_authors ( 3 ) ;
163
164
let config: Configuration = match get_configuration ( ) {
164
165
Ok ( config) => config,
165
166
Err ( _) => {
166
- eprintln ! ( "Error: Could not retrieve git configuration data" ) ;
167
- exit ( 1 ) ;
167
+ return err ! ( "Could not retrieve git configuration data" ) ;
168
168
}
169
169
} ;
170
170
171
+ let license = match project_license ( ) {
172
+ Ok ( l) => l,
173
+ Err ( _) => return err ! ( "Could read directory ./" ) ,
174
+ } ;
175
+
171
176
let info = Info {
172
177
project_name : config. repository_name ,
173
178
language,
174
179
authors,
175
180
repo : config. repository_url ,
176
181
number_of_lines : get_total_loc ( & tokei_langs) ,
177
- license : project_license ( ) ,
182
+ license,
178
183
} ;
179
184
180
185
println ! ( "{}" , info) ;
186
+ Ok ( ( ) )
181
187
}
182
188
183
189
fn project_languages ( ) -> tokei:: Languages {
@@ -187,10 +193,9 @@ fn project_languages() -> tokei::Languages {
187
193
languages
188
194
}
189
195
190
- fn project_license ( ) -> String {
191
- let output = fs:: read_dir ( "." )
192
- . unwrap ( )
193
- . filter_map ( Result :: ok)
196
+ fn project_license ( ) -> Result < String > {
197
+ let output = fs:: read_dir ( "." ) ?
198
+ . filter_map ( result:: Result :: ok)
194
199
. map ( |entry| entry. path ( ) )
195
200
. filter (
196
201
|entry| {
@@ -203,15 +208,15 @@ fn project_license() -> String {
203
208
} , // TODO: multiple prefixes, like COPYING?
204
209
)
205
210
. map ( |entry| license:: Kind :: from_str ( & fs:: read_to_string ( entry) . unwrap_or ( "" . into ( ) ) ) )
206
- . filter_map ( Result :: ok)
211
+ . filter_map ( result :: Result :: ok)
207
212
. map ( |license| license. name ( ) . to_string ( ) )
208
213
. collect :: < Vec < _ > > ( )
209
214
. join ( ", " ) ;
210
215
211
216
if output == "" {
212
- "Unknown" . into ( )
217
+ Ok ( "Unknown" . into ( ) )
213
218
} else {
214
- output
219
+ Ok ( output)
215
220
}
216
221
}
217
222
@@ -229,7 +234,7 @@ struct Configuration {
229
234
pub repository_url : String ,
230
235
}
231
236
232
- fn get_configuration ( ) -> Result < Configuration , Error > {
237
+ fn get_configuration ( ) -> Result < Configuration > {
233
238
let repo = Repository :: open ( "./" ) ?;
234
239
let config = repo. config ( ) ?;
235
240
let mut remote_url = String :: new ( ) ;
0 commit comments