add rustfmt settings file

This commit is contained in:
Tshepang Mbambo 2025-03-30 00:31:44 +02:00
parent 55aebf682d
commit ed9122f534
4 changed files with 39 additions and 127 deletions

View File

@ -1,11 +1,8 @@
use std::{ use std::collections::BTreeMap;
collections::BTreeMap, use std::convert::TryInto as _;
convert::TryInto as _, use std::path::{Path, PathBuf};
env, fmt, fs, use std::str::FromStr;
path::{Path, PathBuf}, use std::{env, fmt, fs, process};
process,
str::FromStr,
};
use chrono::{Datelike as _, Month, TimeZone as _, Utc}; use chrono::{Datelike as _, Month, TimeZone as _, Utc};
use glob::glob; use glob::glob;
@ -19,19 +16,13 @@ struct Date {
impl Date { impl Date {
fn months_since(self, other: Date) -> Option<u32> { fn months_since(self, other: Date) -> Option<u32> {
let self_chrono = Utc let self_chrono =
.with_ymd_and_hms(self.year.try_into().unwrap(), self.month, 1, 0, 0, 0) Utc.with_ymd_and_hms(self.year.try_into().unwrap(), self.month, 1, 0, 0, 0).unwrap();
.unwrap(); let other_chrono =
let other_chrono = Utc Utc.with_ymd_and_hms(other.year.try_into().unwrap(), other.month, 1, 0, 0, 0).unwrap();
.with_ymd_and_hms(other.year.try_into().unwrap(), other.month, 1, 0, 0, 0)
.unwrap();
let duration_since = self_chrono.signed_duration_since(other_chrono); let duration_since = self_chrono.signed_duration_since(other_chrono);
let months_since = duration_since.num_days() / 30; let months_since = duration_since.num_days() / 30;
if months_since < 0 { if months_since < 0 { None } else { Some(months_since.try_into().unwrap()) }
None
} else {
Some(months_since.try_into().unwrap())
}
} }
} }
@ -66,26 +57,18 @@ fn collect_dates_from_file(date_regex: &Regex, text: &str) -> Vec<(usize, Date)>
date_regex date_regex
.captures_iter(text) .captures_iter(text)
.filter_map(|cap| { .filter_map(|cap| {
if let (Some(month), Some(year), None, None) | (None, None, Some(month), Some(year)) = ( if let (Some(month), Some(year), None, None) | (None, None, Some(month), Some(year)) =
cap.name("m1"), (cap.name("m1"), cap.name("y1"), cap.name("m2"), cap.name("y2"))
cap.name("y1"), {
cap.name("m2"),
cap.name("y2"),
) {
let year = year.as_str().parse().expect("year"); let year = year.as_str().parse().expect("year");
let month = Month::from_str(month.as_str()) let month = Month::from_str(month.as_str()).expect("month").number_from_month();
.expect("month")
.number_from_month();
Some((cap.get(0).expect("all").range(), Date { year, month })) Some((cap.get(0).expect("all").range(), Date { year, month }))
} else { } else {
None None
} }
}) })
.map(|(byte_range, date)| { .map(|(byte_range, date)| {
line += text[end_of_last_cap..byte_range.end] line += text[end_of_last_cap..byte_range.end].chars().filter(|c| *c == '\n').count();
.chars()
.filter(|c| *c == '\n')
.count();
end_of_last_cap = byte_range.end; end_of_last_cap = byte_range.end;
(line, date) (line, date)
}) })
@ -138,10 +121,7 @@ fn main() {
let root_dir_path = Path::new(&root_dir); let root_dir_path = Path::new(&root_dir);
let glob_pat = format!("{}/**/*.md", root_dir); let glob_pat = format!("{}/**/*.md", root_dir);
let today_chrono = Utc::now().date_naive(); let today_chrono = Utc::now().date_naive();
let current_month = Date { let current_month = Date { year: today_chrono.year_ce().1, month: today_chrono.month() };
year: today_chrono.year_ce().1,
month: today_chrono.month(),
};
let dates_by_file = collect_dates(glob(&glob_pat).unwrap().map(Result::unwrap)); let dates_by_file = collect_dates(glob(&glob_pat).unwrap().map(Result::unwrap));
let dates_by_file: BTreeMap<_, _> = let dates_by_file: BTreeMap<_, _> =
@ -173,10 +153,7 @@ fn main() {
println!(); println!();
for (path, dates) in dates_by_file { for (path, dates) in dates_by_file {
println!( println!("- {}", path.strip_prefix(&root_dir_path).unwrap_or(&path).display(),);
"- {}",
path.strip_prefix(&root_dir_path).unwrap_or(&path).display(),
);
for (line, date) in dates { for (line, date) in dates {
println!(" - [ ] line {}: {}", line, date); println!(" - [ ] line {}: {}", line, date);
} }
@ -191,14 +168,8 @@ mod tests {
#[test] #[test]
fn test_months_since() { fn test_months_since() {
let date1 = Date { let date1 = Date { year: 2020, month: 3 };
year: 2020, let date2 = Date { year: 2021, month: 1 };
month: 3,
};
let date2 = Date {
year: 2021,
month: 1,
};
assert_eq!(date2.months_since(date1), Some(10)); assert_eq!(date2.months_since(date1), Some(10));
} }
@ -273,83 +244,17 @@ Test8
assert_eq!( assert_eq!(
collect_dates_from_file(&make_date_regex(), text), collect_dates_from_file(&make_date_regex(), text),
vec![ vec![
( (3, Date { year: 2021, month: 1 }),
3, (6, Date { year: 2021, month: 2 }),
Date { (9, Date { year: 2021, month: 3 }),
year: 2021, (11, Date { year: 2021, month: 4 }),
month: 1, (17, Date { year: 2021, month: 5 }),
} (20, Date { year: 2021, month: 1 }),
), (23, Date { year: 2021, month: 2 }),
( (26, Date { year: 2021, month: 3 }),
6, (28, Date { year: 2021, month: 4 }),
Date { (34, Date { year: 2021, month: 5 }),
year: 2021, (38, Date { year: 2021, month: 6 }),
month: 2,
}
),
(
9,
Date {
year: 2021,
month: 3,
}
),
(
11,
Date {
year: 2021,
month: 4,
}
),
(
17,
Date {
year: 2021,
month: 5,
}
),
(
20,
Date {
year: 2021,
month: 1,
}
),
(
23,
Date {
year: 2021,
month: 2,
}
),
(
26,
Date {
year: 2021,
month: 3,
}
),
(
28,
Date {
year: 2021,
month: 4,
}
),
(
34,
Date {
year: 2021,
month: 5,
}
),
(
38,
Date {
year: 2021,
month: 6,
}
),
], ],
); );
} }

View File

@ -20,7 +20,7 @@ use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use rustc_ast_pretty::pprust::item_to_string; use rustc_ast_pretty::pprust::item_to_string;
use rustc_driver::{run_compiler, Compilation}; use rustc_driver::{Compilation, run_compiler};
use rustc_interface::interface::{Compiler, Config}; use rustc_interface::interface::{Compiler, Config};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;

View File

@ -20,7 +20,7 @@ use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use rustc_ast_pretty::pprust::item_to_string; use rustc_ast_pretty::pprust::item_to_string;
use rustc_driver::{run_compiler, Compilation}; use rustc_driver::{Compilation, run_compiler};
use rustc_interface::interface::{Compiler, Config}; use rustc_interface::interface::{Compiler, Config};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;

7
rustfmt.toml Normal file
View File

@ -0,0 +1,7 @@
# matches that of rust-lang/rust
style_edition = "2024"
use_small_heuristics = "Max"
merge_derives = false
group_imports = "StdExternalCrate"
imports_granularity = "Module"
use_field_init_shorthand = true