Skip to content

add rustfmt settings file #2310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 30 additions & 125 deletions ci/date-check/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::{
collections::BTreeMap,
convert::TryInto as _,
env, fmt, fs,
path::{Path, PathBuf},
process,
str::FromStr,
};
use std::collections::BTreeMap;
use std::convert::TryInto as _;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{env, fmt, fs, process};

use chrono::{Datelike as _, Month, TimeZone as _, Utc};
use glob::glob;
Expand All @@ -19,19 +16,13 @@ struct Date {

impl Date {
fn months_since(self, other: Date) -> Option<u32> {
let self_chrono = Utc
.with_ymd_and_hms(self.year.try_into().unwrap(), self.month, 1, 0, 0, 0)
.unwrap();
let other_chrono = Utc
.with_ymd_and_hms(other.year.try_into().unwrap(), other.month, 1, 0, 0, 0)
.unwrap();
let self_chrono =
Utc.with_ymd_and_hms(self.year.try_into().unwrap(), self.month, 1, 0, 0, 0).unwrap();
let other_chrono =
Utc.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 months_since = duration_since.num_days() / 30;
if months_since < 0 {
None
} else {
Some(months_since.try_into().unwrap())
}
if months_since < 0 { None } else { Some(months_since.try_into().unwrap()) }
}
}

Expand Down Expand Up @@ -66,26 +57,18 @@ fn collect_dates_from_file(date_regex: &Regex, text: &str) -> Vec<(usize, Date)>
date_regex
.captures_iter(text)
.filter_map(|cap| {
if let (Some(month), Some(year), None, None) | (None, None, Some(month), Some(year)) = (
cap.name("m1"),
cap.name("y1"),
cap.name("m2"),
cap.name("y2"),
) {
if let (Some(month), Some(year), None, None) | (None, None, Some(month), Some(year)) =
(cap.name("m1"), cap.name("y1"), cap.name("m2"), cap.name("y2"))
{
let year = year.as_str().parse().expect("year");
let month = Month::from_str(month.as_str())
.expect("month")
.number_from_month();
let month = Month::from_str(month.as_str()).expect("month").number_from_month();
Some((cap.get(0).expect("all").range(), Date { year, month }))
} else {
None
}
})
.map(|(byte_range, date)| {
line += text[end_of_last_cap..byte_range.end]
.chars()
.filter(|c| *c == '\n')
.count();
line += text[end_of_last_cap..byte_range.end].chars().filter(|c| *c == '\n').count();
end_of_last_cap = byte_range.end;
(line, date)
})
Expand Down Expand Up @@ -138,10 +121,7 @@ fn main() {
let root_dir_path = Path::new(&root_dir);
let glob_pat = format!("{}/**/*.md", root_dir);
let today_chrono = Utc::now().date_naive();
let current_month = Date {
year: today_chrono.year_ce().1,
month: today_chrono.month(),
};
let current_month = Date { 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: BTreeMap<_, _> =
Expand Down Expand Up @@ -173,10 +153,7 @@ fn main() {
println!();

for (path, dates) in dates_by_file {
println!(
"- {}",
path.strip_prefix(&root_dir_path).unwrap_or(&path).display(),
);
println!("- {}", path.strip_prefix(&root_dir_path).unwrap_or(&path).display(),);
for (line, date) in dates {
println!(" - [ ] line {}: {}", line, date);
}
Expand All @@ -191,14 +168,8 @@ mod tests {

#[test]
fn test_months_since() {
let date1 = Date {
year: 2020,
month: 3,
};
let date2 = Date {
year: 2021,
month: 1,
};
let date1 = Date { year: 2020, month: 3 };
let date2 = Date { year: 2021, month: 1 };
assert_eq!(date2.months_since(date1), Some(10));
}

Expand Down Expand Up @@ -273,83 +244,17 @@ Test8
assert_eq!(
collect_dates_from_file(&make_date_regex(), text),
vec![
(
3,
Date {
year: 2021,
month: 1,
}
),
(
6,
Date {
year: 2021,
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,
}
),
(3, Date { year: 2021, month: 1 }),
(6, Date { year: 2021, 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 }),
],
);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::path::Path;
use std::sync::Arc;

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_middle::ty::TyCtxt;

Expand Down
2 changes: 1 addition & 1 deletion examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::path::Path;
use std::sync::Arc;

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_middle::ty::TyCtxt;

Expand Down
7 changes: 7 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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