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} ;
9
6
10
7
use chrono:: { Datelike as _, Month , TimeZone as _, Utc } ;
11
8
use glob:: glob;
@@ -19,19 +16,13 @@ struct Date {
19
16
20
17
impl Date {
21
18
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 ( ) ;
28
23
let duration_since = self_chrono. signed_duration_since ( other_chrono) ;
29
24
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 ( ) ) }
35
26
}
36
27
}
37
28
@@ -66,26 +57,18 @@ fn collect_dates_from_file(date_regex: &Regex, text: &str) -> Vec<(usize, Date)>
66
57
date_regex
67
58
. captures_iter ( text)
68
59
. 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
+ {
75
63
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 ( ) ;
79
65
Some ( ( cap. get ( 0 ) . expect ( "all" ) . range ( ) , Date { year, month } ) )
80
66
} else {
81
67
None
82
68
}
83
69
} )
84
70
. 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 ( ) ;
89
72
end_of_last_cap = byte_range. end ;
90
73
( line, date)
91
74
} )
@@ -138,10 +121,7 @@ fn main() {
138
121
let root_dir_path = Path :: new ( & root_dir) ;
139
122
let glob_pat = format ! ( "{}/**/*.md" , root_dir) ;
140
123
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 ( ) } ;
145
125
146
126
let dates_by_file = collect_dates ( glob ( & glob_pat) . unwrap ( ) . map ( Result :: unwrap) ) ;
147
127
let dates_by_file: BTreeMap < _ , _ > =
@@ -173,10 +153,7 @@ fn main() {
173
153
println ! ( ) ;
174
154
175
155
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( ) , ) ;
180
157
for ( line, date) in dates {
181
158
println ! ( " - [ ] line {}: {}" , line, date) ;
182
159
}
@@ -191,14 +168,8 @@ mod tests {
191
168
192
169
#[ test]
193
170
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 } ;
202
173
assert_eq ! ( date2. months_since( date1) , Some ( 10 ) ) ;
203
174
}
204
175
@@ -273,83 +244,17 @@ Test8
273
244
assert_eq ! (
274
245
collect_dates_from_file( & make_date_regex( ) , text) ,
275
246
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 } ) ,
353
258
] ,
354
259
) ;
355
260
}
0 commit comments