typos/src/checks.rs

53 lines
1.7 KiB
Rust
Raw Normal View History

2020-03-23 21:37:06 -04:00
pub(crate) fn check_path(
walk: ignore::Walk,
checks: &dyn typos::checks::Check,
2020-03-23 21:37:06 -04:00
parser: &typos::tokens::Parser,
dictionary: &dyn typos::Dictionary,
reporter: &dyn typos::report::Report,
2020-11-16 21:02:10 -05:00
) -> Result<(), anyhow::Error> {
2020-03-23 21:37:06 -04:00
for entry in walk {
2020-11-16 21:02:10 -05:00
check_entry(entry, checks, parser, dictionary, reporter)?;
2020-03-23 21:37:06 -04:00
}
2020-11-16 21:02:10 -05:00
Ok(())
2020-03-23 21:37:06 -04:00
}
pub(crate) fn check_path_parallel(
walk: ignore::WalkParallel,
checks: &dyn typos::checks::Check,
2020-03-23 21:37:06 -04:00
parser: &typos::tokens::Parser,
dictionary: &dyn typos::Dictionary,
reporter: &dyn typos::report::Report,
2020-11-16 21:02:10 -05:00
) -> Result<(), anyhow::Error> {
let error: std::sync::Mutex<Result<(), anyhow::Error>> = std::sync::Mutex::new(Ok(()));
2020-03-23 21:37:06 -04:00
walk.run(|| {
Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| {
match check_entry(entry, checks, parser, dictionary, reporter) {
2020-11-16 21:02:10 -05:00
Ok(()) => ignore::WalkState::Continue,
2020-03-23 21:37:06 -04:00
Err(err) => {
2020-11-16 21:02:10 -05:00
*error.lock().unwrap() = Err(err);
ignore::WalkState::Quit
2020-03-23 21:37:06 -04:00
}
}
})
});
2020-11-16 21:02:10 -05:00
error.into_inner().unwrap()
2020-03-23 21:37:06 -04:00
}
fn check_entry(
entry: Result<ignore::DirEntry, ignore::Error>,
checks: &dyn typos::checks::Check,
2020-03-23 21:37:06 -04:00
parser: &typos::tokens::Parser,
dictionary: &dyn typos::Dictionary,
reporter: &dyn typos::report::Report,
2020-11-16 21:02:10 -05:00
) -> Result<(), anyhow::Error> {
2020-03-23 21:37:06 -04:00
let entry = entry?;
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
let explicit = entry.depth() == 0;
2020-11-16 21:02:10 -05:00
checks.check_filename(entry.path(), parser, dictionary, reporter)?;
checks.check_file(entry.path(), explicit, parser, dictionary, reporter)?;
2020-03-23 21:37:06 -04:00
}
2020-11-16 21:02:10 -05:00
Ok(())
2020-03-23 21:37:06 -04:00
}