From b7700fa21463637cd6c057485e853891b076226e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 10 Nov 2020 06:26:42 -0600 Subject: [PATCH] refactor: Don't special case --files --- crates/typos/src/checks.rs | 256 +++++++++++++++++++++++-------------- src/main.rs | 105 +++++---------- 2 files changed, 198 insertions(+), 163 deletions(-) diff --git a/crates/typos/src/checks.rs b/crates/typos/src/checks.rs index 374bebc..56a689d 100644 --- a/crates/typos/src/checks.rs +++ b/crates/typos/src/checks.rs @@ -149,6 +149,10 @@ impl TyposSettings { binary: self.binary, } } + + pub fn build_files(&self) -> Files { + Files {} + } } impl Default for TyposSettings { @@ -161,6 +165,123 @@ impl Default for TyposSettings { } } +#[derive(Debug, Clone)] +pub struct Typos { + check_filenames: bool, + check_files: bool, + binary: bool, +} + +impl Check for Typos { + fn check_str( + &self, + buffer: &str, + parser: &tokens::Parser, + dictionary: &dyn Dictionary, + reporter: &dyn report::Report, + ) -> Result { + let mut typos_found = false; + + for ident in parser.parse_str(buffer) { + match dictionary.correct_ident(ident) { + Some(Status::Valid) => {} + Some(corrections) => { + let byte_offset = ident.offset(); + let msg = report::Typo { + context: report::Context::None, + buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()), + byte_offset, + typo: ident.token(), + corrections, + }; + typos_found |= reporter.report(msg.into()); + } + None => { + for word in ident.split() { + match dictionary.correct_word(word) { + Some(Status::Valid) => {} + Some(corrections) => { + let byte_offset = word.offset(); + let msg = report::Typo { + context: report::Context::None, + buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()), + byte_offset, + typo: word.token(), + corrections, + }; + typos_found |= reporter.report(msg.into()); + } + None => {} + } + } + } + } + } + + Ok(typos_found) + } + + fn check_bytes( + &self, + buffer: &[u8], + parser: &tokens::Parser, + dictionary: &dyn Dictionary, + reporter: &dyn report::Report, + ) -> Result { + let mut typos_found = false; + + for ident in parser.parse_bytes(buffer) { + match dictionary.correct_ident(ident) { + Some(Status::Valid) => {} + Some(corrections) => { + let byte_offset = ident.offset(); + let msg = report::Typo { + context: report::Context::None, + buffer: std::borrow::Cow::Borrowed(buffer), + byte_offset, + typo: ident.token(), + corrections, + }; + typos_found |= reporter.report(msg.into()); + } + None => { + for word in ident.split() { + match dictionary.correct_word(word) { + Some(Status::Valid) => {} + Some(corrections) => { + let byte_offset = word.offset(); + let msg = report::Typo { + context: report::Context::None, + buffer: std::borrow::Cow::Borrowed(buffer), + byte_offset, + typo: word.token(), + corrections, + }; + typos_found |= reporter.report(msg.into()); + } + None => {} + } + } + } + } + } + + Ok(typos_found) + } + + fn check_filenames(&self) -> bool { + self.check_filenames + } + + fn check_files(&self) -> bool { + self.check_files + } + + fn binary(&self) -> bool { + self.binary + } +} + #[derive(Debug, Clone)] pub struct ParseIdentifiers { check_filenames: bool, @@ -294,119 +415,68 @@ impl Check for ParseWords { } #[derive(Debug, Clone)] -pub struct Typos { - check_filenames: bool, - check_files: bool, - binary: bool, -} +pub struct Files {} -impl Check for Typos { +impl Check for Files { fn check_str( &self, - buffer: &str, - parser: &tokens::Parser, - dictionary: &dyn Dictionary, - reporter: &dyn report::Report, + _buffer: &str, + _parser: &tokens::Parser, + _dictionary: &dyn Dictionary, + _reporter: &dyn report::Report, ) -> Result { - let mut typos_found = false; - - for ident in parser.parse_str(buffer) { - match dictionary.correct_ident(ident) { - Some(Status::Valid) => {} - Some(corrections) => { - let byte_offset = ident.offset(); - let msg = report::Typo { - context: report::Context::None, - buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()), - byte_offset, - typo: ident.token(), - corrections, - }; - typos_found |= reporter.report(msg.into()); - } - None => { - for word in ident.split() { - match dictionary.correct_word(word) { - Some(Status::Valid) => {} - Some(corrections) => { - let byte_offset = word.offset(); - let msg = report::Typo { - context: report::Context::None, - buffer: std::borrow::Cow::Borrowed(buffer.as_bytes()), - byte_offset, - typo: word.token(), - corrections, - }; - typos_found |= reporter.report(msg.into()); - } - None => {} - } - } - } - } - } - + let typos_found = false; Ok(typos_found) } fn check_bytes( &self, - buffer: &[u8], - parser: &tokens::Parser, - dictionary: &dyn Dictionary, - reporter: &dyn report::Report, + _buffer: &[u8], + _parser: &tokens::Parser, + _dictionary: &dyn Dictionary, + _reporter: &dyn report::Report, ) -> Result { - let mut typos_found = false; - - for ident in parser.parse_bytes(buffer) { - match dictionary.correct_ident(ident) { - Some(Status::Valid) => {} - Some(corrections) => { - let byte_offset = ident.offset(); - let msg = report::Typo { - context: report::Context::None, - buffer: std::borrow::Cow::Borrowed(buffer), - byte_offset, - typo: ident.token(), - corrections, - }; - typos_found |= reporter.report(msg.into()); - } - None => { - for word in ident.split() { - match dictionary.correct_word(word) { - Some(Status::Valid) => {} - Some(corrections) => { - let byte_offset = word.offset(); - let msg = report::Typo { - context: report::Context::None, - buffer: std::borrow::Cow::Borrowed(buffer), - byte_offset, - typo: word.token(), - corrections, - }; - typos_found |= reporter.report(msg.into()); - } - None => {} - } - } - } - } - } - + let typos_found = false; Ok(typos_found) } fn check_filenames(&self) -> bool { - self.check_filenames + true } fn check_files(&self) -> bool { - self.check_files + true } fn binary(&self) -> bool { - self.binary + true + } + + fn check_filename( + &self, + _path: &std::path::Path, + _parser: &tokens::Parser, + _dictionary: &dyn Dictionary, + _reporter: &dyn report::Report, + ) -> Result { + let typos_found = false; + Ok(typos_found) + } + + fn check_file( + &self, + path: &std::path::Path, + _explicit: bool, + _parser: &tokens::Parser, + _dictionary: &dyn Dictionary, + reporter: &dyn report::Report, + ) -> Result { + let typos_found = false; + + let msg = report::File::new(path); + reporter.report(msg.into()); + + Ok(typos_found) } } diff --git a/src/main.rs b/src/main.rs index 4e7a9fd..5206c26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ extern crate clap; use std::io::Write; -use std::sync::atomic; use structopt::StructOpt; @@ -91,77 +90,43 @@ fn run() -> Result { reporter = &replace_reporter; } - if args.files { - if single_threaded { - for entry in walk.build() { - match entry { - Ok(entry) => { - let msg = typos::report::File::new(entry.path()); - reporter.report(msg.into()); - } - Err(err) => { - let msg = typos::report::Error::new(err.to_string()); - reporter.report(msg.into()); - errors_found = true - } - } - } - } else { - let atomic_errors = atomic::AtomicBool::new(errors_found); - walk.build_parallel().run(|| { - Box::new(|entry: Result| { - match entry { - Ok(entry) => { - let msg = typos::report::File::new(entry.path()); - reporter.report(msg.into()); - } - Err(err) => { - let msg = typos::report::Error::new(err.to_string()); - reporter.report(msg.into()); - atomic_errors.store(true, atomic::Ordering::Relaxed); - } - } - ignore::WalkState::Continue - }) - }); - errors_found = atomic_errors.into_inner(); - } + let (files, identifier_parser, word_parser, checks); + let selected_checks: &dyn typos::checks::Check = if args.files { + files = settings.build_files(); + &files + } else if args.identifiers { + identifier_parser = settings.build_identifier_parser(); + &identifier_parser + } else if args.words { + word_parser = settings.build_word_parser(); + &word_parser } else { - let (identifier_parser, word_parser, checks); - let selected_checks: &dyn typos::checks::Check = if args.identifiers { - identifier_parser = settings.build_identifier_parser(); - &identifier_parser - } else if args.words { - word_parser = settings.build_word_parser(); - &word_parser - } else { - checks = settings.build_typos(); - &checks - }; + checks = settings.build_typos(); + &checks + }; - let (cur_typos, cur_errors) = if single_threaded { - checks::check_path( - walk.build(), - selected_checks, - &parser, - &dictionary, - reporter, - ) - } else { - checks::check_path_parallel( - walk.build_parallel(), - selected_checks, - &parser, - &dictionary, - reporter, - ) - }; - if cur_typos { - typos_found = true; - } - if cur_errors { - errors_found = true; - } + let (cur_typos, cur_errors) = if single_threaded { + checks::check_path( + walk.build(), + selected_checks, + &parser, + &dictionary, + reporter, + ) + } else { + checks::check_path_parallel( + walk.build_parallel(), + selected_checks, + &parser, + &dictionary, + reporter, + ) + }; + if cur_typos { + typos_found = true; + } + if cur_errors { + errors_found = true; } if args.write_changes {