From 0a2f865d0f262da66300c9cc23012acb0926c467 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 26 Oct 2019 20:31:10 -0600 Subject: [PATCH] refactor: Change error strategy for future thread use --- src/main.rs | 41 ++++++++++++++++++++++++++++++++--------- typos/src/report.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index c1df67a..d7e990a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -266,6 +266,27 @@ pub fn init_logging(level: Option) { } } +fn check_entry( + entry: Result, + args: &Args, + checks: &typos::checks::Checks, +) -> 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(), args.format.report())? { + typos_found = true; + } + if checks.check_file(entry.path(), explicit, args.format.report())? { + typos_found = true; + } + } + + Ok(typos_found) +} + fn run() -> Result { let args = Args::from_args(); @@ -279,6 +300,7 @@ fn run() -> Result { let config = config; let mut typos_found = false; + let mut errors_found = false; for path in args.path.iter() { let path = path.canonicalize()?; let cwd = if path.is_file() { @@ -318,20 +340,21 @@ fn run() -> Result { .git_exclude(config.files.ignore_vcs()) .parents(config.files.ignore_parent()); for entry in walk.build() { - 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(), args.format.report())? { - typos_found = true; - } - if checks.check_file(entry.path(), explicit, args.format.report())? { - typos_found = true; + match check_entry(entry, &args, &checks) { + Ok(true) => typos_found = true, + Err(err) => { + let msg = typos::report::Error::new(err.to_string()); + args.format.report()(msg.into()); + errors_found = true } + _ => (), } } } - if typos_found { + if errors_found { + Ok(2) + } else if typos_found { Ok(1) } else { Ok(0) diff --git a/typos/src/report.rs b/typos/src/report.rs index 0c6adc3..a3d6a96 100644 --- a/typos/src/report.rs +++ b/typos/src/report.rs @@ -8,6 +8,8 @@ pub enum Message<'m> { BinaryFile(BinaryFile<'m>), Correction(Correction<'m>), FilenameCorrection(FilenameCorrection<'m>), + PathError(PathError<'m>), + Error(Error), } #[derive(Clone, Debug, serde::Serialize, derive_more::Display)] @@ -40,6 +42,30 @@ pub struct FilenameCorrection<'m> { pub(crate) non_exhaustive: (), } +#[derive(Clone, Debug, serde::Serialize)] +pub struct PathError<'m> { + pub path: &'m std::path::Path, + pub msg: String, + #[serde(skip)] + pub(crate) non_exhaustive: (), +} + +#[derive(Clone, Debug, serde::Serialize)] +pub struct Error { + pub msg: String, + #[serde(skip)] + pub(crate) non_exhaustive: (), +} + +impl Error { + pub fn new(msg: String) -> Self { + Self { + msg, + non_exhaustive: (), + } + } +} + pub type Report = fn(msg: Message); pub fn print_silent(_: Message) {} @@ -62,6 +88,12 @@ pub fn print_brief(msg: Message) { Message::FilenameCorrection(msg) => { println!("{}: {} -> {}", msg.path.display(), msg.typo, msg.correction); } + Message::PathError(msg) => { + println!("{}: {}", msg.path.display(), msg.msg); + } + Message::Error(msg) => { + println!("{}", msg.msg); + } } } @@ -79,6 +111,12 @@ pub fn print_long(msg: Message) { msg.correction ); } + Message::PathError(msg) => { + println!("{}: {}", msg.path.display(), msg.msg); + } + Message::Error(msg) => { + println!("{}", msg.msg); + } } }