1
+ use getopts:: Options ;
1
2
use std:: { collections:: HashMap , error:: Error , io:: Write , num:: ParseIntError , ops:: AddAssign } ;
2
3
3
4
fn main ( ) -> Result < ( ) , Box < dyn Error > > {
4
5
let mut opts = getopts:: Options :: new ( ) ;
5
6
let opts = opts
6
- . optflag ( "q" , "quiet" , "Hides the output of commit messages" )
7
+ . optflag ( "q" , "quiet" , "Hides the output of commit messages." )
8
+ . optflag ( "h" , "help" , "Show this help and exit." )
7
9
. optmulti (
8
10
"a" ,
9
11
"author-contains" ,
@@ -79,12 +81,12 @@ fn main() -> Result<(), Box<dyn Error>> {
79
81
let matches = match opts. parse ( std:: env:: args ( ) ) {
80
82
Ok ( it) => it,
81
83
Err ( err) => {
82
- usage ( "commit-analzer" , opts) ;
84
+ usage ( opts) ;
83
85
return Err ( err. into ( ) ) ;
84
86
}
85
87
} ;
86
- if matches. free . len ( ) < 2 {
87
- usage ( & matches . free [ 0 ] , opts) ;
88
+ if matches. opt_present ( "h" ) || matches . free . len ( ) < 2 {
89
+ usage ( opts) ;
88
90
return Ok ( ( ) ) ;
89
91
}
90
92
let is_quiet = matches. opt_present ( "q" ) ;
@@ -176,9 +178,15 @@ fn main() -> Result<(), Box<dyn Error>> {
176
178
Ok ( ( ) )
177
179
}
178
180
179
- fn usage ( program_name : & str , opts : & getopts:: Options ) {
180
- println ! ( "Usage: {} <FILE> [OPTIONS]\n " , program_name) ;
181
- println ! ( "{}" , opts. usage( "Parses the output of `git log`." ) ) ;
181
+ /// A small in-app documentation.
182
+ ///
183
+ /// This function will write a brief usage information, including a short
184
+ /// introduction to the meaning of the configured `options`, to `stdout`.
185
+ fn usage ( options : & Options ) {
186
+ println ! (
187
+ "Usage: commit-analyzer <FILE> [OPTIONS]\n \n {}" ,
188
+ options. usage( "Parses the output of `git log`." )
189
+ ) ;
182
190
}
183
191
184
192
fn matches_filter ( commit : & Commit , filter : & Filter ) -> bool {
@@ -239,7 +247,11 @@ impl Filter {
239
247
}
240
248
241
249
fn check_loc ( & self , loc : & & Loc ) -> bool {
242
- self . file_extension . is_empty ( ) || self . file_extension . iter ( ) . any ( |ext| loc. file . ends_with ( & format ! ( ".{}" , ext) ) )
250
+ self . file_extension . is_empty ( )
251
+ || self
252
+ . file_extension
253
+ . iter ( )
254
+ . any ( |ext| loc. file . ends_with ( & format ! ( ".{}" , ext) ) )
243
255
}
244
256
}
245
257
@@ -320,7 +332,7 @@ fn parse_commit(commit: &str) -> Result<(Commit, &str), CommitParseError> {
320
332
if loc. is_empty ( ) || loc. starts_with ( "commit" ) {
321
333
break ;
322
334
}
323
- locs. push ( Loc :: parse ( loc) . map_err ( |err| CommitParseError :: LocFailed ( err ) ) ?) ;
335
+ locs. push ( Loc :: parse ( loc) . map_err ( CommitParseError :: LocFailed ) ?) ;
324
336
remainder_result = remainder;
325
337
if let Some ( remainder) = remainder_result. strip_prefix ( '\n' ) {
326
338
remainder_result = remainder;
@@ -331,9 +343,9 @@ fn parse_commit(commit: &str) -> Result<(Commit, &str), CommitParseError> {
331
343
let commit = Commit {
332
344
commit : commit. into ( ) ,
333
345
merge,
334
- author : Author :: parse ( author) . map_err ( |err| CommitParseError :: AuthorFailed ( err ) ) ?,
346
+ author : Author :: parse ( author) . map_err ( CommitParseError :: AuthorFailed ) ?,
335
347
date : chrono:: DateTime :: parse_from_str ( date, "%a %b %e %T %Y %z" )
336
- . map_err ( |err| CommitParseError :: DateFailed ( err ) ) ?,
348
+ . map_err ( CommitParseError :: DateFailed ) ?,
337
349
message : message. into ( ) ,
338
350
locs,
339
351
} ;
@@ -352,7 +364,11 @@ pub struct Commit {
352
364
353
365
impl Commit {
354
366
pub fn loc ( & self , filter : & Filter ) -> i64 {
355
- self . locs . iter ( ) . filter ( |l| filter. check_loc ( l) ) . map ( |l| l. loc ( ) ) . sum ( )
367
+ self . locs
368
+ . iter ( )
369
+ . filter ( |l| filter. check_loc ( l) )
370
+ . map ( |l| l. loc ( ) )
371
+ . sum ( )
356
372
}
357
373
}
358
374
@@ -407,20 +423,12 @@ impl Loc {
407
423
let added = if added == "-" {
408
424
None
409
425
} else {
410
- Some (
411
- added
412
- . parse ( )
413
- . map_err ( |err| LocParseError :: AddedParseError ( err) ) ?,
414
- )
426
+ Some ( added. parse ( ) . map_err ( LocParseError :: AddedParseError ) ?)
415
427
} ;
416
428
let removed = if removed == "-" {
417
429
None
418
430
} else {
419
- Some (
420
- removed
421
- . parse ( )
422
- . map_err ( |err| LocParseError :: RemovedParseError ( err) ) ?,
423
- )
431
+ Some ( removed. parse ( ) . map_err ( LocParseError :: RemovedParseError ) ?)
424
432
} ;
425
433
let file = file. into ( ) ;
426
434
Ok ( Self {
0 commit comments