refactor: Use a single reporter instance

This commit is contained in:
Ed Page 2020-03-23 18:31:15 -05:00 committed by Ed Page
parent 575971a5c5
commit 8732d24f53

View file

@ -394,20 +394,20 @@ fn init_logging(level: Option<log::Level>) {
fn check_path( fn check_path(
walk: ignore::Walk, walk: ignore::Walk,
format: Format,
checks: &dyn Checks, checks: &dyn Checks,
parser: &typos::tokens::Parser, parser: &typos::tokens::Parser,
dictionary: &dyn typos::Dictionary, dictionary: &dyn typos::Dictionary,
reporter: &dyn typos::report::Report,
) -> (bool, bool) { ) -> (bool, bool) {
let mut typos_found = false; let mut typos_found = false;
let mut errors_found = false; let mut errors_found = false;
for entry in walk { for entry in walk {
match check_entry(entry, format, checks, parser, dictionary) { match check_entry(entry, checks, parser, dictionary, reporter) {
Ok(true) => typos_found = true, Ok(true) => typos_found = true,
Err(err) => { Err(err) => {
let msg = typos::report::Error::new(err.to_string()); let msg = typos::report::Error::new(err.to_string());
format.reporter().report(msg.into()); reporter.report(msg.into());
errors_found = true errors_found = true
} }
_ => (), _ => (),
@ -419,21 +419,21 @@ fn check_path(
fn check_path_parallel( fn check_path_parallel(
walk: ignore::WalkParallel, walk: ignore::WalkParallel,
format: Format,
checks: &dyn Checks, checks: &dyn Checks,
parser: &typos::tokens::Parser, parser: &typos::tokens::Parser,
dictionary: &dyn typos::Dictionary, dictionary: &dyn typos::Dictionary,
reporter: &dyn typos::report::Report,
) -> (bool, bool) { ) -> (bool, bool) {
let typos_found = atomic::AtomicBool::new(false); let typos_found = atomic::AtomicBool::new(false);
let errors_found = atomic::AtomicBool::new(false); let errors_found = atomic::AtomicBool::new(false);
walk.run(|| { walk.run(|| {
Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| { Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| {
match check_entry(entry, format, checks, parser, dictionary) { match check_entry(entry, checks, parser, dictionary, reporter) {
Ok(true) => typos_found.store(true, atomic::Ordering::Relaxed), Ok(true) => typos_found.store(true, atomic::Ordering::Relaxed),
Err(err) => { Err(err) => {
let msg = typos::report::Error::new(err.to_string()); let msg = typos::report::Error::new(err.to_string());
format.reporter().report(msg.into()); reporter.report(msg.into());
errors_found.store(true, atomic::Ordering::Relaxed); errors_found.store(true, atomic::Ordering::Relaxed);
} }
_ => (), _ => (),
@ -447,26 +447,20 @@ fn check_path_parallel(
fn check_entry( fn check_entry(
entry: Result<ignore::DirEntry, ignore::Error>, entry: Result<ignore::DirEntry, ignore::Error>,
format: Format,
checks: &dyn Checks, checks: &dyn Checks,
parser: &typos::tokens::Parser, parser: &typos::tokens::Parser,
dictionary: &dyn typos::Dictionary, dictionary: &dyn typos::Dictionary,
reporter: &dyn typos::report::Report,
) -> Result<bool, anyhow::Error> { ) -> Result<bool, anyhow::Error> {
let mut typos_found = false; let mut typos_found = false;
let entry = entry?; let entry = entry?;
if entry.file_type().map(|t| t.is_file()).unwrap_or(true) { if entry.file_type().map(|t| t.is_file()).unwrap_or(true) {
let explicit = entry.depth() == 0; let explicit = entry.depth() == 0;
if checks.check_filename(entry.path(), parser, dictionary, format.reporter())? { if checks.check_filename(entry.path(), parser, dictionary, reporter)? {
typos_found = true; typos_found = true;
} }
if checks.check_file( if checks.check_file(entry.path(), explicit, parser, dictionary, reporter)? {
entry.path(),
explicit,
parser,
dictionary,
format.reporter(),
)? {
typos_found = true; typos_found = true;
} }
} }
@ -528,35 +522,38 @@ fn run() -> Result<i32, anyhow::Error> {
.git_ignore(config.files.ignore_vcs()) .git_ignore(config.files.ignore_vcs())
.git_exclude(config.files.ignore_vcs()) .git_exclude(config.files.ignore_vcs())
.parents(config.files.ignore_parent()); .parents(config.files.ignore_parent());
let reporter = args.format.reporter();
let single_threaded = args.threads == 1; let single_threaded = args.threads == 1;
if args.files { if args.files {
if single_threaded { if single_threaded {
for entry in walk.build() { for entry in walk.build() {
match entry { match entry {
Ok(entry) => { Ok(entry) => {
let msg = typos::report::File::new(entry.path()); let msg = typos::report::File::new(entry.path());
args.format.reporter().report(msg.into()); reporter.report(msg.into());
} }
Err(err) => { Err(err) => {
let msg = typos::report::Error::new(err.to_string()); let msg = typos::report::Error::new(err.to_string());
args.format.reporter().report(msg.into()); reporter.report(msg.into());
errors_found = true errors_found = true
} }
} }
} }
} else { } else {
let format = args.format;
let atomic_errors = atomic::AtomicBool::new(errors_found); let atomic_errors = atomic::AtomicBool::new(errors_found);
walk.build_parallel().run(|| { walk.build_parallel().run(|| {
Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| { Box::new(|entry: Result<ignore::DirEntry, ignore::Error>| {
match entry { match entry {
Ok(entry) => { Ok(entry) => {
let msg = typos::report::File::new(entry.path()); let msg = typos::report::File::new(entry.path());
format.reporter().report(msg.into()); reporter.report(msg.into());
} }
Err(err) => { Err(err) => {
let msg = typos::report::Error::new(err.to_string()); let msg = typos::report::Error::new(err.to_string());
format.reporter().report(msg.into()); reporter.report(msg.into());
atomic_errors.store(true, atomic::Ordering::Relaxed); atomic_errors.store(true, atomic::Ordering::Relaxed);
} }
} }
@ -568,14 +565,14 @@ fn run() -> Result<i32, anyhow::Error> {
} else if args.identifiers { } else if args.identifiers {
let checks = settings.build_identifier_parser(); let checks = settings.build_identifier_parser();
let (cur_typos, cur_errors) = if single_threaded { let (cur_typos, cur_errors) = if single_threaded {
check_path(walk.build(), args.format, &checks, &parser, &dictionary) check_path(walk.build(), &checks, &parser, &dictionary, reporter)
} else { } else {
check_path_parallel( check_path_parallel(
walk.build_parallel(), walk.build_parallel(),
args.format,
&checks, &checks,
&parser, &parser,
&dictionary, &dictionary,
reporter,
) )
}; };
if cur_typos { if cur_typos {
@ -587,14 +584,14 @@ fn run() -> Result<i32, anyhow::Error> {
} else if args.words { } else if args.words {
let checks = settings.build_word_parser(); let checks = settings.build_word_parser();
let (cur_typos, cur_errors) = if single_threaded { let (cur_typos, cur_errors) = if single_threaded {
check_path(walk.build(), args.format, &checks, &parser, &dictionary) check_path(walk.build(), &checks, &parser, &dictionary, reporter)
} else { } else {
check_path_parallel( check_path_parallel(
walk.build_parallel(), walk.build_parallel(),
args.format,
&checks, &checks,
&parser, &parser,
&dictionary, &dictionary,
reporter,
) )
}; };
if cur_typos { if cur_typos {
@ -606,14 +603,14 @@ fn run() -> Result<i32, anyhow::Error> {
} else { } else {
let checks = settings.build_checks(); let checks = settings.build_checks();
let (cur_typos, cur_errors) = if single_threaded { let (cur_typos, cur_errors) = if single_threaded {
check_path(walk.build(), args.format, &checks, &parser, &dictionary) check_path(walk.build(), &checks, &parser, &dictionary, reporter)
} else { } else {
check_path_parallel( check_path_parallel(
walk.build_parallel(), walk.build_parallel(),
args.format,
&checks, &checks,
&parser, &parser,
&dictionary, &dictionary,
reporter,
) )
}; };
if cur_typos { if cur_typos {