1- use std:: {
2- collections:: BTreeMap ,
3- convert:: TryInto as _,
4- env, fmt, fs,
5- path:: { Path , PathBuf } ,
6- process,
7- str:: FromStr ,
8- } ;
1+ use std:: collections:: BTreeMap ;
2+ use std:: convert:: TryInto as _;
3+ use std:: path:: { Path , PathBuf } ;
4+ use std:: str:: FromStr ;
5+ use std:: { env, fmt, fs, process} ;
96
107use chrono:: { Datelike as _, Month , TimeZone as _, Utc } ;
118use glob:: glob;
@@ -19,19 +16,13 @@ struct Date {
1916
2017impl Date {
2118 fn months_since ( self , other : Date ) -> Option < u32 > {
22- let self_chrono = Utc
23- . with_ymd_and_hms ( self . year . try_into ( ) . unwrap ( ) , self . month , 1 , 0 , 0 , 0 )
24- . unwrap ( ) ;
25- let other_chrono = Utc
26- . with_ymd_and_hms ( other. year . try_into ( ) . unwrap ( ) , other. month , 1 , 0 , 0 , 0 )
27- . unwrap ( ) ;
19+ let self_chrono =
20+ Utc . with_ymd_and_hms ( self . year . try_into ( ) . unwrap ( ) , self . month , 1 , 0 , 0 , 0 ) . unwrap ( ) ;
21+ let other_chrono =
22+ Utc . with_ymd_and_hms ( other. year . try_into ( ) . unwrap ( ) , other. month , 1 , 0 , 0 , 0 ) . unwrap ( ) ;
2823 let duration_since = self_chrono. signed_duration_since ( other_chrono) ;
2924 let months_since = duration_since. num_days ( ) / 30 ;
30- if months_since < 0 {
31- None
32- } else {
33- Some ( months_since. try_into ( ) . unwrap ( ) )
34- }
25+ if months_since < 0 { None } else { Some ( months_since. try_into ( ) . unwrap ( ) ) }
3526 }
3627}
3728
@@ -66,26 +57,18 @@ fn collect_dates_from_file(date_regex: &Regex, text: &str) -> Vec<(usize, Date)>
6657 date_regex
6758 . captures_iter ( text)
6859 . filter_map ( |cap| {
69- if let ( Some ( month) , Some ( year) , None , None ) | ( None , None , Some ( month) , Some ( year) ) = (
70- cap. name ( "m1" ) ,
71- cap. name ( "y1" ) ,
72- cap. name ( "m2" ) ,
73- cap. name ( "y2" ) ,
74- ) {
60+ if let ( Some ( month) , Some ( year) , None , None ) | ( None , None , Some ( month) , Some ( year) ) =
61+ ( cap. name ( "m1" ) , cap. name ( "y1" ) , cap. name ( "m2" ) , cap. name ( "y2" ) )
62+ {
7563 let year = year. as_str ( ) . parse ( ) . expect ( "year" ) ;
76- let month = Month :: from_str ( month. as_str ( ) )
77- . expect ( "month" )
78- . number_from_month ( ) ;
64+ let month = Month :: from_str ( month. as_str ( ) ) . expect ( "month" ) . number_from_month ( ) ;
7965 Some ( ( cap. get ( 0 ) . expect ( "all" ) . range ( ) , Date { year, month } ) )
8066 } else {
8167 None
8268 }
8369 } )
8470 . map ( |( byte_range, date) | {
85- line += text[ end_of_last_cap..byte_range. end ]
86- . chars ( )
87- . filter ( |c| * c == '\n' )
88- . count ( ) ;
71+ line += text[ end_of_last_cap..byte_range. end ] . chars ( ) . filter ( |c| * c == '\n' ) . count ( ) ;
8972 end_of_last_cap = byte_range. end ;
9073 ( line, date)
9174 } )
@@ -138,10 +121,7 @@ fn main() {
138121 let root_dir_path = Path :: new ( & root_dir) ;
139122 let glob_pat = format ! ( "{}/**/*.md" , root_dir) ;
140123 let today_chrono = Utc :: now ( ) . date_naive ( ) ;
141- let current_month = Date {
142- year : today_chrono. year_ce ( ) . 1 ,
143- month : today_chrono. month ( ) ,
144- } ;
124+ let current_month = Date { year : today_chrono. year_ce ( ) . 1 , month : today_chrono. month ( ) } ;
145125
146126 let dates_by_file = collect_dates ( glob ( & glob_pat) . unwrap ( ) . map ( Result :: unwrap) ) ;
147127 let dates_by_file: BTreeMap < _ , _ > =
@@ -173,10 +153,7 @@ fn main() {
173153 println ! ( ) ;
174154
175155 for ( path, dates) in dates_by_file {
176- println ! (
177- "- {}" ,
178- path. strip_prefix( & root_dir_path) . unwrap_or( & path) . display( ) ,
179- ) ;
156+ println ! ( "- {}" , path. strip_prefix( & root_dir_path) . unwrap_or( & path) . display( ) , ) ;
180157 for ( line, date) in dates {
181158 println ! ( " - [ ] line {}: {}" , line, date) ;
182159 }
@@ -191,14 +168,8 @@ mod tests {
191168
192169 #[ test]
193170 fn test_months_since ( ) {
194- let date1 = Date {
195- year : 2020 ,
196- month : 3 ,
197- } ;
198- let date2 = Date {
199- year : 2021 ,
200- month : 1 ,
201- } ;
171+ let date1 = Date { year : 2020 , month : 3 } ;
172+ let date2 = Date { year : 2021 , month : 1 } ;
202173 assert_eq ! ( date2. months_since( date1) , Some ( 10 ) ) ;
203174 }
204175
@@ -273,83 +244,17 @@ Test8
273244 assert_eq ! (
274245 collect_dates_from_file( & make_date_regex( ) , text) ,
275246 vec![
276- (
277- 3 ,
278- Date {
279- year: 2021 ,
280- month: 1 ,
281- }
282- ) ,
283- (
284- 6 ,
285- Date {
286- year: 2021 ,
287- month: 2 ,
288- }
289- ) ,
290- (
291- 9 ,
292- Date {
293- year: 2021 ,
294- month: 3 ,
295- }
296- ) ,
297- (
298- 11 ,
299- Date {
300- year: 2021 ,
301- month: 4 ,
302- }
303- ) ,
304- (
305- 17 ,
306- Date {
307- year: 2021 ,
308- month: 5 ,
309- }
310- ) ,
311- (
312- 20 ,
313- Date {
314- year: 2021 ,
315- month: 1 ,
316- }
317- ) ,
318- (
319- 23 ,
320- Date {
321- year: 2021 ,
322- month: 2 ,
323- }
324- ) ,
325- (
326- 26 ,
327- Date {
328- year: 2021 ,
329- month: 3 ,
330- }
331- ) ,
332- (
333- 28 ,
334- Date {
335- year: 2021 ,
336- month: 4 ,
337- }
338- ) ,
339- (
340- 34 ,
341- Date {
342- year: 2021 ,
343- month: 5 ,
344- }
345- ) ,
346- (
347- 38 ,
348- Date {
349- year: 2021 ,
350- month: 6 ,
351- }
352- ) ,
247+ ( 3 , Date { year: 2021 , month: 1 } ) ,
248+ ( 6 , Date { year: 2021 , month: 2 } ) ,
249+ ( 9 , Date { year: 2021 , month: 3 } ) ,
250+ ( 11 , Date { year: 2021 , month: 4 } ) ,
251+ ( 17 , Date { year: 2021 , month: 5 } ) ,
252+ ( 20 , Date { year: 2021 , month: 1 } ) ,
253+ ( 23 , Date { year: 2021 , month: 2 } ) ,
254+ ( 26 , Date { year: 2021 , month: 3 } ) ,
255+ ( 28 , Date { year: 2021 , month: 4 } ) ,
256+ ( 34 , Date { year: 2021 , month: 5 } ) ,
257+ ( 38 , Date { year: 2021 , month: 6 } ) ,
353258 ] ,
354259 ) ;
355260 }
0 commit comments