From 736db107087e8014bd54f0e0f0ce393229664227 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 28 Oct 2020 21:01:33 -0500 Subject: [PATCH] fix(format): Clarify message types --- crates/typos/src/checks.rs | 8 ++++---- crates/typos/src/report.rs | 30 +++++++++++++++--------------- src/replace.rs | 16 ++++++---------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/crates/typos/src/checks.rs b/crates/typos/src/checks.rs index 984413f..ba196ca 100644 --- a/crates/typos/src/checks.rs +++ b/crates/typos/src/checks.rs @@ -249,7 +249,7 @@ impl Checks { Some(Status::Valid) => {} Some(corrections) => { let byte_offset = ident.offset(); - let msg = report::PathCorrection { + let msg = report::PathTypo { path, byte_offset, typo: ident.token(), @@ -263,7 +263,7 @@ impl Checks { Some(Status::Valid) => {} Some(corrections) => { let byte_offset = word.offset(); - let msg = report::PathCorrection { + let msg = report::PathTypo { path, byte_offset, typo: word.token(), @@ -318,7 +318,7 @@ impl Checks { Some(Status::Valid) => {} Some(corrections) => { let byte_offset = ident.offset(); - let msg = report::Correction { + let msg = report::FileTypo { path, line, line_num, @@ -334,7 +334,7 @@ impl Checks { Some(Status::Valid) => {} Some(corrections) => { let byte_offset = word.offset(); - let msg = report::Correction { + let msg = report::FileTypo { path, line, line_num, diff --git a/crates/typos/src/report.rs b/crates/typos/src/report.rs index e1cabf4..3626ebd 100644 --- a/crates/typos/src/report.rs +++ b/crates/typos/src/report.rs @@ -8,8 +8,8 @@ use std::io::{self, Write}; #[non_exhaustive] pub enum Message<'m> { BinaryFile(BinaryFile<'m>), - Correction(Correction<'m>), - PathCorrection(PathCorrection<'m>), + FileTypo(FileTypo<'m>), + PathTypo(PathTypo<'m>), File(File<'m>), Parse(Parse<'m>), PathError(PathError<'m>), @@ -20,8 +20,8 @@ impl<'m> Message<'m> { pub fn is_correction(&self) -> bool { match self { Message::BinaryFile(_) => false, - Message::Correction(c) => c.corrections.is_correction(), - Message::PathCorrection(c) => c.corrections.is_correction(), + Message::FileTypo(c) => c.corrections.is_correction(), + Message::PathTypo(c) => c.corrections.is_correction(), Message::File(_) => false, Message::Parse(_) => false, Message::PathError(_) => false, @@ -32,8 +32,8 @@ impl<'m> Message<'m> { pub fn is_error(&self) -> bool { match self { Message::BinaryFile(_) => false, - Message::Correction(_) => false, - Message::PathCorrection(_) => false, + Message::FileTypo(_) => false, + Message::PathTypo(_) => false, Message::File(_) => false, Message::Parse(_) => false, Message::PathError(_) => true, @@ -51,7 +51,7 @@ pub struct BinaryFile<'m> { #[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)] #[non_exhaustive] -pub struct Correction<'m> { +pub struct FileTypo<'m> { pub path: &'m std::path::Path, #[serde(skip)] pub line: &'m [u8], @@ -61,7 +61,7 @@ pub struct Correction<'m> { pub corrections: crate::Status<'m>, } -impl<'m> Default for Correction<'m> { +impl<'m> Default for FileTypo<'m> { fn default() -> Self { Self { path: std::path::Path::new("-"), @@ -76,14 +76,14 @@ impl<'m> Default for Correction<'m> { #[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)] #[non_exhaustive] -pub struct PathCorrection<'m> { +pub struct PathTypo<'m> { pub path: &'m std::path::Path, pub byte_offset: usize, pub typo: &'m str, pub corrections: crate::Status<'m>, } -impl<'m> Default for PathCorrection<'m> { +impl<'m> Default for PathTypo<'m> { fn default() -> Self { Self { path: std::path::Path::new("-"), @@ -195,7 +195,7 @@ impl Report for PrintBrief { Message::BinaryFile(msg) => { log::info!("{}", msg); } - Message::Correction(msg) => match &msg.corrections { + Message::FileTypo(msg) => match &msg.corrections { crate::Status::Valid => {} crate::Status::Invalid => { println!( @@ -217,7 +217,7 @@ impl Report for PrintBrief { ); } }, - Message::PathCorrection(msg) => match &msg.corrections { + Message::PathTypo(msg) => match &msg.corrections { crate::Status::Valid => {} crate::Status::Invalid => { println!("{}: {} is disallowed", msg.path.display(), msg.typo,); @@ -257,8 +257,8 @@ impl Report for PrintLong { Message::BinaryFile(msg) => { log::info!("{}", msg); } - Message::Correction(msg) => print_long_correction(msg), - Message::PathCorrection(msg) => match &msg.corrections { + Message::FileTypo(msg) => print_long_correction(msg), + Message::PathTypo(msg) => match &msg.corrections { crate::Status::Valid => {} crate::Status::Invalid => { println!( @@ -293,7 +293,7 @@ impl Report for PrintLong { } } -fn print_long_correction(msg: &Correction) { +fn print_long_correction(msg: &FileTypo) { let line_num = msg.line_num.to_string(); let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect(); diff --git a/src/replace.rs b/src/replace.rs index 80ac6bb..0e91c07 100644 --- a/src/replace.rs +++ b/src/replace.rs @@ -57,7 +57,7 @@ impl<'r> Replace<'r> { impl<'r> typos::report::Report for Replace<'r> { fn report(&self, msg: typos::report::Message<'_>) -> bool { match msg { - typos::report::Message::Correction(msg) => match msg.corrections { + typos::report::Message::FileTypo(msg) => match msg.corrections { typos::Status::Corrections(corrections) if corrections.len() == 1 => { let path = msg.path.to_owned(); let line_num = msg.line_num; @@ -73,11 +73,9 @@ impl<'r> typos::report::Report for Replace<'r> { content.push(correction); false } - _ => self - .reporter - .report(typos::report::Message::Correction(msg)), + _ => self.reporter.report(typos::report::Message::FileTypo(msg)), }, - typos::report::Message::PathCorrection(msg) => match msg.corrections { + typos::report::Message::PathTypo(msg) => match msg.corrections { typos::Status::Corrections(corrections) if corrections.len() == 1 => { let path = msg.path.to_owned(); let correction = @@ -87,9 +85,7 @@ impl<'r> typos::report::Report for Replace<'r> { content.push(correction); false } - _ => self - .reporter - .report(typos::report::Message::PathCorrection(msg)), + _ => self.reporter.report(typos::report::Message::PathTypo(msg)), }, _ => self.reporter.report(msg), } @@ -208,7 +204,7 @@ mod test { let primary = typos::report::PrintSilent; let replace = Replace::new(&primary); replace.report( - typos::report::Correction::default() + typos::report::FileTypo::default() .path(input_file.path()) .line(b"1 foo 2\n3 4 5") .line_num(1) @@ -233,7 +229,7 @@ mod test { let primary = typos::report::PrintSilent; let replace = Replace::new(&primary); replace.report( - typos::report::PathCorrection::default() + typos::report::PathTypo::default() .path(input_file.path()) .byte_offset(0) .typo("foo")