From 5cfe913d034ee93ac847775a73e48602e91d7ddb Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 23 Mar 2020 20:37:06 -0500 Subject: [PATCH] refactor: Split out checks --- src/checks.rs | 165 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 171 ++------------------------------------------------ 2 files changed, 169 insertions(+), 167 deletions(-) create mode 100644 src/checks.rs diff --git a/src/checks.rs b/src/checks.rs new file mode 100644 index 0000000..76d3351 --- /dev/null +++ b/src/checks.rs @@ -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; + + fn check_file( + &self, + path: &std::path::Path, + explicit: bool, + parser: &typos::tokens::Parser, + dictionary: &dyn typos::Dictionary, + report: &dyn typos::report::Report, + ) -> Result; +} + +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 { + 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 { + 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 { + 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 { + 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 { + 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 { + 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| { + 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, + checks: &dyn Checks, + parser: &typos::tokens::Parser, + dictionary: &dyn typos::Dictionary, + reporter: &dyn typos::report::Report, +) -> Result { + 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) +} diff --git a/src/main.rs b/src/main.rs index b58742d..fe07f5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,97 +8,10 @@ use std::sync::atomic; use structopt::StructOpt; mod args; +mod checks; mod config; 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; - - fn check_file( - &self, - path: &std::path::Path, - explicit: bool, - parser: &typos::tokens::Parser, - dictionary: &dyn typos::Dictionary, - report: &dyn typos::report::Report, - ) -> Result; -} - -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 { - 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 { - 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 { - 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 { - 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 { - 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 { - self.check_file(path, explicit, parser, dictionary, report) - } -} - fn init_logging(level: Option) { if let Some(level) = level { let mut builder = env_logger::Builder::new(); @@ -122,82 +35,6 @@ fn init_logging(level: Option) { } } -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| { - 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, - checks: &dyn Checks, - parser: &typos::tokens::Parser, - dictionary: &dyn typos::Dictionary, - reporter: &dyn typos::report::Report, -) -> Result { - 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 { let args = args::Args::from_args(); @@ -295,7 +132,7 @@ fn run() -> Result { } } else { 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 } else if args.words { @@ -307,7 +144,7 @@ fn run() -> Result { }; let (cur_typos, cur_errors) = if single_threaded { - check_path( + checks::check_path( walk.build(), selected_checks, &parser, @@ -315,7 +152,7 @@ fn run() -> Result { reporter, ) } else { - check_path_parallel( + checks::check_path_parallel( walk.build_parallel(), selected_checks, &parser,