fix(replace): Don't error on successful replacement

This commit is contained in:
Ed Page 2020-06-30 21:08:38 -05:00
parent 44cc750ab8
commit a5ed18ee46
3 changed files with 46 additions and 17 deletions

View file

@ -235,8 +235,7 @@ impl Checks {
typo: ident.token(), typo: ident.token(),
correction, correction,
}; };
reporter.report(msg.into()); typos_found |= reporter.report(msg.into());
typos_found = true;
} else { } else {
for word in ident.split() { for word in ident.split() {
if let Some(correction) = dictionary.correct_word(word) { if let Some(correction) = dictionary.correct_word(word) {
@ -247,8 +246,7 @@ impl Checks {
typo: word.token(), typo: word.token(),
correction, correction,
}; };
reporter.report(msg.into()); typos_found |= reporter.report(msg.into());
typos_found = true;
} }
} }
} }
@ -293,8 +291,7 @@ impl Checks {
typo: ident.token(), typo: ident.token(),
correction, correction,
}; };
typos_found = true; typos_found |= reporter.report(msg.into());
reporter.report(msg.into());
} else { } else {
for word in ident.split() { for word in ident.split() {
if let Some(correction) = dictionary.correct_word(word) { if let Some(correction) = dictionary.correct_word(word) {
@ -307,8 +304,7 @@ impl Checks {
typo: word.token(), typo: word.token(),
correction, correction,
}; };
typos_found = true; typos_found |= reporter.report(msg.into());
reporter.report(msg.into());
} }
} }
} }

View file

@ -15,6 +15,32 @@ pub enum Message<'m> {
Error(Error), Error(Error),
} }
impl<'m> Message<'m> {
pub fn is_correction(&self) -> bool {
match self {
Message::BinaryFile(_) => false,
Message::Correction(_) => true,
Message::PathCorrection(_) => true,
Message::File(_) => false,
Message::Parse(_) => false,
Message::PathError(_) => false,
Message::Error(_) => false,
}
}
pub fn is_error(&self) -> bool {
match self {
Message::BinaryFile(_) => false,
Message::Correction(_) => false,
Message::PathCorrection(_) => false,
Message::File(_) => false,
Message::Parse(_) => false,
Message::PathError(_) => true,
Message::Error(_) => true,
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_more::Display, derive_setters::Setters)] #[derive(Clone, Debug, serde::Serialize, derive_more::Display, derive_setters::Setters)]
#[display(fmt = "Skipping binary file {}", "path.display()")] #[display(fmt = "Skipping binary file {}", "path.display()")]
#[non_exhaustive] #[non_exhaustive]
@ -147,22 +173,24 @@ impl Default for Error {
} }
pub trait Report: Send + Sync { pub trait Report: Send + Sync {
fn report(&self, msg: Message); fn report(&self, msg: Message) -> bool;
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct PrintSilent; pub struct PrintSilent;
impl Report for PrintSilent { impl Report for PrintSilent {
fn report(&self, _msg: Message) {} fn report(&self, msg: Message) -> bool {
msg.is_correction()
}
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct PrintBrief; pub struct PrintBrief;
impl Report for PrintBrief { impl Report for PrintBrief {
fn report(&self, msg: Message) { fn report(&self, msg: Message) -> bool {
match msg { match &msg {
Message::BinaryFile(msg) => { Message::BinaryFile(msg) => {
log::info!("{}", msg); log::info!("{}", msg);
} }
@ -192,6 +220,7 @@ impl Report for PrintBrief {
log::error!("{}", msg.msg); log::error!("{}", msg.msg);
} }
} }
msg.is_correction()
} }
} }
@ -199,8 +228,8 @@ impl Report for PrintBrief {
pub struct PrintLong; pub struct PrintLong;
impl Report for PrintLong { impl Report for PrintLong {
fn report(&self, msg: Message) { fn report(&self, msg: Message) -> bool {
match msg { match &msg {
Message::BinaryFile(msg) => { Message::BinaryFile(msg) => {
log::info!("{}", msg); log::info!("{}", msg);
} }
@ -226,10 +255,11 @@ impl Report for PrintLong {
log::error!("{}", msg.msg); log::error!("{}", msg.msg);
} }
} }
msg.is_correction()
} }
} }
fn print_long_correction(msg: Correction) { fn print_long_correction(msg: &Correction) {
let line_num = msg.line_num.to_string(); let line_num = msg.line_num.to_string();
let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect(); let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect();
@ -266,7 +296,8 @@ fn print_long_correction(msg: Correction) {
pub struct PrintJson; pub struct PrintJson;
impl Report for PrintJson { impl Report for PrintJson {
fn report(&self, msg: Message) { fn report(&self, msg: Message) -> bool {
println!("{}", serde_json::to_string(&msg).unwrap()); println!("{}", serde_json::to_string(&msg).unwrap());
msg.is_correction()
} }
} }

View file

@ -55,7 +55,7 @@ impl<'r> Replace<'r> {
} }
impl<'r> typos::report::Report for Replace<'r> { impl<'r> typos::report::Report for Replace<'r> {
fn report(&self, msg: typos::report::Message<'_>) { fn report(&self, msg: typos::report::Message<'_>) -> bool {
match msg { match msg {
typos::report::Message::Correction(msg) => { typos::report::Message::Correction(msg) => {
let path = msg.path.to_owned(); let path = msg.path.to_owned();
@ -69,6 +69,7 @@ impl<'r> typos::report::Report for Replace<'r> {
.entry(line_num) .entry(line_num)
.or_insert_with(Vec::new); .or_insert_with(Vec::new);
content.push(correction); content.push(correction);
false
} }
typos::report::Message::PathCorrection(msg) => { typos::report::Message::PathCorrection(msg) => {
let path = msg.path.to_owned(); let path = msg.path.to_owned();
@ -76,6 +77,7 @@ impl<'r> typos::report::Report for Replace<'r> {
let mut deferred = self.deferred.lock().unwrap(); let mut deferred = self.deferred.lock().unwrap();
let content = deferred.paths.entry(path).or_insert_with(Vec::new); let content = deferred.paths.entry(path).or_insert_with(Vec::new);
content.push(correction); content.push(correction);
false
} }
_ => self.reporter.report(msg), _ => self.reporter.report(msg),
} }