mirror of
https://github.com/crate-ci/typos.git
synced 2025-02-03 20:29:45 -05:00
refactor: Split out checks
This commit is contained in:
parent
79d9a4d801
commit
5cfe913d03
2 changed files with 169 additions and 167 deletions
165
src/checks.rs
Normal file
165
src/checks.rs
Normal file
|
@ -0,0 +1,165 @@
|
||||||
|
use std::sync::atomic;
|
||||||
|
|
||||||
|
pub(crate) trait Checks: Send + Sync {
|
||||||
|
fn check_filename(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
dictionary: &dyn typos::Dictionary,
|
||||||
|
report: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, typos::Error>;
|
||||||
|
|
||||||
|
fn check_file(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
explicit: bool,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
dictionary: &dyn typos::Dictionary,
|
||||||
|
report: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, typos::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'p> Checks for typos::checks::ParseIdentifiers {
|
||||||
|
fn check_filename(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
_dictionary: &dyn typos::Dictionary,
|
||||||
|
report: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, typos::Error> {
|
||||||
|
self.check_filename(path, parser, report)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_file(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
explicit: bool,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
_dictionary: &dyn typos::Dictionary,
|
||||||
|
report: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, typos::Error> {
|
||||||
|
self.check_file(path, explicit, parser, report)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'p> Checks for typos::checks::ParseWords {
|
||||||
|
fn check_filename(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
_dictionary: &dyn typos::Dictionary,
|
||||||
|
report: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, typos::Error> {
|
||||||
|
self.check_filename(path, parser, report)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_file(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
explicit: bool,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
_dictionary: &dyn typos::Dictionary,
|
||||||
|
report: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, typos::Error> {
|
||||||
|
self.check_file(path, explicit, parser, report)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'d, 'p> Checks for typos::checks::Checks {
|
||||||
|
fn check_filename(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
dictionary: &dyn typos::Dictionary,
|
||||||
|
report: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, typos::Error> {
|
||||||
|
self.check_filename(path, parser, dictionary, report)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_file(
|
||||||
|
&self,
|
||||||
|
path: &std::path::Path,
|
||||||
|
explicit: bool,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
dictionary: &dyn typos::Dictionary,
|
||||||
|
report: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, typos::Error> {
|
||||||
|
self.check_file(path, explicit, parser, dictionary, report)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn check_path(
|
||||||
|
walk: ignore::Walk,
|
||||||
|
checks: &dyn Checks,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
dictionary: &dyn typos::Dictionary,
|
||||||
|
reporter: &dyn typos::report::Report,
|
||||||
|
) -> (bool, bool) {
|
||||||
|
let mut typos_found = false;
|
||||||
|
let mut errors_found = false;
|
||||||
|
|
||||||
|
for entry in walk {
|
||||||
|
match check_entry(entry, checks, parser, dictionary, reporter) {
|
||||||
|
Ok(true) => typos_found = true,
|
||||||
|
Err(err) => {
|
||||||
|
let msg = typos::report::Error::new(err.to_string());
|
||||||
|
reporter.report(msg.into());
|
||||||
|
errors_found = true
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(typos_found, errors_found)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn check_path_parallel(
|
||||||
|
walk: ignore::WalkParallel,
|
||||||
|
checks: &dyn Checks,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
dictionary: &dyn typos::Dictionary,
|
||||||
|
reporter: &dyn typos::report::Report,
|
||||||
|
) -> (bool, bool) {
|
||||||
|
let typos_found = atomic::AtomicBool::new(false);
|
||||||
|
let errors_found = atomic::AtomicBool::new(false);
|
||||||
|
|
||||||
|
walk.run(|| {
|
||||||
|
Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| {
|
||||||
|
match check_entry(entry, checks, parser, dictionary, reporter) {
|
||||||
|
Ok(true) => typos_found.store(true, atomic::Ordering::Relaxed),
|
||||||
|
Err(err) => {
|
||||||
|
let msg = typos::report::Error::new(err.to_string());
|
||||||
|
reporter.report(msg.into());
|
||||||
|
errors_found.store(true, atomic::Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
ignore::WalkState::Continue
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
(typos_found.into_inner(), errors_found.into_inner())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_entry(
|
||||||
|
entry: Result<ignore::DirEntry, ignore::Error>,
|
||||||
|
checks: &dyn Checks,
|
||||||
|
parser: &typos::tokens::Parser,
|
||||||
|
dictionary: &dyn typos::Dictionary,
|
||||||
|
reporter: &dyn typos::report::Report,
|
||||||
|
) -> Result<bool, anyhow::Error> {
|
||||||
|
let mut typos_found = false;
|
||||||
|
|
||||||
|
let entry = entry?;
|
||||||
|
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
|
||||||
|
let explicit = entry.depth() == 0;
|
||||||
|
if checks.check_filename(entry.path(), parser, dictionary, reporter)? {
|
||||||
|
typos_found = true;
|
||||||
|
}
|
||||||
|
if checks.check_file(entry.path(), explicit, parser, dictionary, reporter)? {
|
||||||
|
typos_found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(typos_found)
|
||||||
|
}
|
171
src/main.rs
171
src/main.rs
|
@ -8,97 +8,10 @@ use std::sync::atomic;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
|
mod checks;
|
||||||
mod config;
|
mod config;
|
||||||
mod dict;
|
mod dict;
|
||||||
|
|
||||||
trait Checks: Send + Sync {
|
|
||||||
fn check_filename(
|
|
||||||
&self,
|
|
||||||
path: &std::path::Path,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
dictionary: &dyn typos::Dictionary,
|
|
||||||
report: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, typos::Error>;
|
|
||||||
|
|
||||||
fn check_file(
|
|
||||||
&self,
|
|
||||||
path: &std::path::Path,
|
|
||||||
explicit: bool,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
dictionary: &dyn typos::Dictionary,
|
|
||||||
report: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, typos::Error>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'p> Checks for typos::checks::ParseIdentifiers {
|
|
||||||
fn check_filename(
|
|
||||||
&self,
|
|
||||||
path: &std::path::Path,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
_dictionary: &dyn typos::Dictionary,
|
|
||||||
report: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, typos::Error> {
|
|
||||||
self.check_filename(path, parser, report)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_file(
|
|
||||||
&self,
|
|
||||||
path: &std::path::Path,
|
|
||||||
explicit: bool,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
_dictionary: &dyn typos::Dictionary,
|
|
||||||
report: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, typos::Error> {
|
|
||||||
self.check_file(path, explicit, parser, report)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'p> Checks for typos::checks::ParseWords {
|
|
||||||
fn check_filename(
|
|
||||||
&self,
|
|
||||||
path: &std::path::Path,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
_dictionary: &dyn typos::Dictionary,
|
|
||||||
report: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, typos::Error> {
|
|
||||||
self.check_filename(path, parser, report)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_file(
|
|
||||||
&self,
|
|
||||||
path: &std::path::Path,
|
|
||||||
explicit: bool,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
_dictionary: &dyn typos::Dictionary,
|
|
||||||
report: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, typos::Error> {
|
|
||||||
self.check_file(path, explicit, parser, report)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'d, 'p> Checks for typos::checks::Checks {
|
|
||||||
fn check_filename(
|
|
||||||
&self,
|
|
||||||
path: &std::path::Path,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
dictionary: &dyn typos::Dictionary,
|
|
||||||
report: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, typos::Error> {
|
|
||||||
self.check_filename(path, parser, dictionary, report)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_file(
|
|
||||||
&self,
|
|
||||||
path: &std::path::Path,
|
|
||||||
explicit: bool,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
dictionary: &dyn typos::Dictionary,
|
|
||||||
report: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, typos::Error> {
|
|
||||||
self.check_file(path, explicit, parser, dictionary, report)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init_logging(level: Option<log::Level>) {
|
fn init_logging(level: Option<log::Level>) {
|
||||||
if let Some(level) = level {
|
if let Some(level) = level {
|
||||||
let mut builder = env_logger::Builder::new();
|
let mut builder = env_logger::Builder::new();
|
||||||
|
@ -122,82 +35,6 @@ fn init_logging(level: Option<log::Level>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_path(
|
|
||||||
walk: ignore::Walk,
|
|
||||||
checks: &dyn Checks,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
dictionary: &dyn typos::Dictionary,
|
|
||||||
reporter: &dyn typos::report::Report,
|
|
||||||
) -> (bool, bool) {
|
|
||||||
let mut typos_found = false;
|
|
||||||
let mut errors_found = false;
|
|
||||||
|
|
||||||
for entry in walk {
|
|
||||||
match check_entry(entry, checks, parser, dictionary, reporter) {
|
|
||||||
Ok(true) => typos_found = true,
|
|
||||||
Err(err) => {
|
|
||||||
let msg = typos::report::Error::new(err.to_string());
|
|
||||||
reporter.report(msg.into());
|
|
||||||
errors_found = true
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
(typos_found, errors_found)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_path_parallel(
|
|
||||||
walk: ignore::WalkParallel,
|
|
||||||
checks: &dyn Checks,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
dictionary: &dyn typos::Dictionary,
|
|
||||||
reporter: &dyn typos::report::Report,
|
|
||||||
) -> (bool, bool) {
|
|
||||||
let typos_found = atomic::AtomicBool::new(false);
|
|
||||||
let errors_found = atomic::AtomicBool::new(false);
|
|
||||||
|
|
||||||
walk.run(|| {
|
|
||||||
Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| {
|
|
||||||
match check_entry(entry, checks, parser, dictionary, reporter) {
|
|
||||||
Ok(true) => typos_found.store(true, atomic::Ordering::Relaxed),
|
|
||||||
Err(err) => {
|
|
||||||
let msg = typos::report::Error::new(err.to_string());
|
|
||||||
reporter.report(msg.into());
|
|
||||||
errors_found.store(true, atomic::Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
ignore::WalkState::Continue
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
(typos_found.into_inner(), errors_found.into_inner())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_entry(
|
|
||||||
entry: Result<ignore::DirEntry, ignore::Error>,
|
|
||||||
checks: &dyn Checks,
|
|
||||||
parser: &typos::tokens::Parser,
|
|
||||||
dictionary: &dyn typos::Dictionary,
|
|
||||||
reporter: &dyn typos::report::Report,
|
|
||||||
) -> Result<bool, anyhow::Error> {
|
|
||||||
let mut typos_found = false;
|
|
||||||
|
|
||||||
let entry = entry?;
|
|
||||||
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
|
|
||||||
let explicit = entry.depth() == 0;
|
|
||||||
if checks.check_filename(entry.path(), parser, dictionary, reporter)? {
|
|
||||||
typos_found = true;
|
|
||||||
}
|
|
||||||
if checks.check_file(entry.path(), explicit, parser, dictionary, reporter)? {
|
|
||||||
typos_found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(typos_found)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run() -> Result<i32, anyhow::Error> {
|
fn run() -> Result<i32, anyhow::Error> {
|
||||||
let args = args::Args::from_args();
|
let args = args::Args::from_args();
|
||||||
|
|
||||||
|
@ -295,7 +132,7 @@ fn run() -> Result<i32, anyhow::Error> {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let (identifier_parser, word_parser, checks);
|
let (identifier_parser, word_parser, checks);
|
||||||
let selected_checks: &dyn Checks = if args.identifiers {
|
let selected_checks: &dyn checks::Checks = if args.identifiers {
|
||||||
identifier_parser = settings.build_identifier_parser();
|
identifier_parser = settings.build_identifier_parser();
|
||||||
&identifier_parser
|
&identifier_parser
|
||||||
} else if args.words {
|
} else if args.words {
|
||||||
|
@ -307,7 +144,7 @@ fn run() -> Result<i32, anyhow::Error> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let (cur_typos, cur_errors) = if single_threaded {
|
let (cur_typos, cur_errors) = if single_threaded {
|
||||||
check_path(
|
checks::check_path(
|
||||||
walk.build(),
|
walk.build(),
|
||||||
selected_checks,
|
selected_checks,
|
||||||
&parser,
|
&parser,
|
||||||
|
@ -315,7 +152,7 @@ fn run() -> Result<i32, anyhow::Error> {
|
||||||
reporter,
|
reporter,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
check_path_parallel(
|
checks::check_path_parallel(
|
||||||
walk.build_parallel(),
|
walk.build_parallel(),
|
||||||
selected_checks,
|
selected_checks,
|
||||||
&parser,
|
&parser,
|
||||||
|
|
Loading…
Add table
Reference in a new issue