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

View file

@ -15,6 +15,32 @@ pub enum Message<'m> {
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)]
#[display(fmt = "Skipping binary file {}", "path.display()")]
#[non_exhaustive]
@ -147,22 +173,24 @@ impl Default for Error {
}
pub trait Report: Send + Sync {
fn report(&self, msg: Message);
fn report(&self, msg: Message) -> bool;
}
#[derive(Copy, Clone, Debug)]
pub struct PrintSilent;
impl Report for PrintSilent {
fn report(&self, _msg: Message) {}
fn report(&self, msg: Message) -> bool {
msg.is_correction()
}
}
#[derive(Copy, Clone, Debug)]
pub struct PrintBrief;
impl Report for PrintBrief {
fn report(&self, msg: Message) {
match msg {
fn report(&self, msg: Message) -> bool {
match &msg {
Message::BinaryFile(msg) => {
log::info!("{}", msg);
}
@ -192,6 +220,7 @@ impl Report for PrintBrief {
log::error!("{}", msg.msg);
}
}
msg.is_correction()
}
}
@ -199,8 +228,8 @@ impl Report for PrintBrief {
pub struct PrintLong;
impl Report for PrintLong {
fn report(&self, msg: Message) {
match msg {
fn report(&self, msg: Message) -> bool {
match &msg {
Message::BinaryFile(msg) => {
log::info!("{}", msg);
}
@ -226,10 +255,11 @@ impl Report for PrintLong {
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_indent: String = itertools::repeat_n(" ", line_num.len()).collect();
@ -266,7 +296,8 @@ fn print_long_correction(msg: Correction) {
pub struct PrintJson;
impl Report for PrintJson {
fn report(&self, msg: Message) {
fn report(&self, msg: Message) -> bool {
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> {
fn report(&self, msg: typos::report::Message<'_>) {
fn report(&self, msg: typos::report::Message<'_>) -> bool {
match msg {
typos::report::Message::Correction(msg) => {
let path = msg.path.to_owned();
@ -69,6 +69,7 @@ impl<'r> typos::report::Report for Replace<'r> {
.entry(line_num)
.or_insert_with(Vec::new);
content.push(correction);
false
}
typos::report::Message::PathCorrection(msg) => {
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 content = deferred.paths.entry(path).or_insert_with(Vec::new);
content.push(correction);
false
}
_ => self.reporter.report(msg),
}