fix(format): Clarify message types

This commit is contained in:
Ed Page 2020-10-28 21:01:33 -05:00
parent 2e6cd39781
commit 736db10708
3 changed files with 25 additions and 29 deletions

View file

@ -249,7 +249,7 @@ impl Checks {
Some(Status::Valid) => {} Some(Status::Valid) => {}
Some(corrections) => { Some(corrections) => {
let byte_offset = ident.offset(); let byte_offset = ident.offset();
let msg = report::PathCorrection { let msg = report::PathTypo {
path, path,
byte_offset, byte_offset,
typo: ident.token(), typo: ident.token(),
@ -263,7 +263,7 @@ impl Checks {
Some(Status::Valid) => {} Some(Status::Valid) => {}
Some(corrections) => { Some(corrections) => {
let byte_offset = word.offset(); let byte_offset = word.offset();
let msg = report::PathCorrection { let msg = report::PathTypo {
path, path,
byte_offset, byte_offset,
typo: word.token(), typo: word.token(),
@ -318,7 +318,7 @@ impl Checks {
Some(Status::Valid) => {} Some(Status::Valid) => {}
Some(corrections) => { Some(corrections) => {
let byte_offset = ident.offset(); let byte_offset = ident.offset();
let msg = report::Correction { let msg = report::FileTypo {
path, path,
line, line,
line_num, line_num,
@ -334,7 +334,7 @@ impl Checks {
Some(Status::Valid) => {} Some(Status::Valid) => {}
Some(corrections) => { Some(corrections) => {
let byte_offset = word.offset(); let byte_offset = word.offset();
let msg = report::Correction { let msg = report::FileTypo {
path, path,
line, line,
line_num, line_num,

View file

@ -8,8 +8,8 @@ use std::io::{self, Write};
#[non_exhaustive] #[non_exhaustive]
pub enum Message<'m> { pub enum Message<'m> {
BinaryFile(BinaryFile<'m>), BinaryFile(BinaryFile<'m>),
Correction(Correction<'m>), FileTypo(FileTypo<'m>),
PathCorrection(PathCorrection<'m>), PathTypo(PathTypo<'m>),
File(File<'m>), File(File<'m>),
Parse(Parse<'m>), Parse(Parse<'m>),
PathError(PathError<'m>), PathError(PathError<'m>),
@ -20,8 +20,8 @@ impl<'m> Message<'m> {
pub fn is_correction(&self) -> bool { pub fn is_correction(&self) -> bool {
match self { match self {
Message::BinaryFile(_) => false, Message::BinaryFile(_) => false,
Message::Correction(c) => c.corrections.is_correction(), Message::FileTypo(c) => c.corrections.is_correction(),
Message::PathCorrection(c) => c.corrections.is_correction(), Message::PathTypo(c) => c.corrections.is_correction(),
Message::File(_) => false, Message::File(_) => false,
Message::Parse(_) => false, Message::Parse(_) => false,
Message::PathError(_) => false, Message::PathError(_) => false,
@ -32,8 +32,8 @@ impl<'m> Message<'m> {
pub fn is_error(&self) -> bool { pub fn is_error(&self) -> bool {
match self { match self {
Message::BinaryFile(_) => false, Message::BinaryFile(_) => false,
Message::Correction(_) => false, Message::FileTypo(_) => false,
Message::PathCorrection(_) => false, Message::PathTypo(_) => false,
Message::File(_) => false, Message::File(_) => false,
Message::Parse(_) => false, Message::Parse(_) => false,
Message::PathError(_) => true, Message::PathError(_) => true,
@ -51,7 +51,7 @@ pub struct BinaryFile<'m> {
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)] #[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive] #[non_exhaustive]
pub struct Correction<'m> { pub struct FileTypo<'m> {
pub path: &'m std::path::Path, pub path: &'m std::path::Path,
#[serde(skip)] #[serde(skip)]
pub line: &'m [u8], pub line: &'m [u8],
@ -61,7 +61,7 @@ pub struct Correction<'m> {
pub corrections: crate::Status<'m>, pub corrections: crate::Status<'m>,
} }
impl<'m> Default for Correction<'m> { impl<'m> Default for FileTypo<'m> {
fn default() -> Self { fn default() -> Self {
Self { Self {
path: std::path::Path::new("-"), path: std::path::Path::new("-"),
@ -76,14 +76,14 @@ impl<'m> Default for Correction<'m> {
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)] #[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive] #[non_exhaustive]
pub struct PathCorrection<'m> { pub struct PathTypo<'m> {
pub path: &'m std::path::Path, pub path: &'m std::path::Path,
pub byte_offset: usize, pub byte_offset: usize,
pub typo: &'m str, pub typo: &'m str,
pub corrections: crate::Status<'m>, pub corrections: crate::Status<'m>,
} }
impl<'m> Default for PathCorrection<'m> { impl<'m> Default for PathTypo<'m> {
fn default() -> Self { fn default() -> Self {
Self { Self {
path: std::path::Path::new("-"), path: std::path::Path::new("-"),
@ -195,7 +195,7 @@ impl Report for PrintBrief {
Message::BinaryFile(msg) => { Message::BinaryFile(msg) => {
log::info!("{}", msg); log::info!("{}", msg);
} }
Message::Correction(msg) => match &msg.corrections { Message::FileTypo(msg) => match &msg.corrections {
crate::Status::Valid => {} crate::Status::Valid => {}
crate::Status::Invalid => { crate::Status::Invalid => {
println!( 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::Valid => {}
crate::Status::Invalid => { crate::Status::Invalid => {
println!("{}: {} is disallowed", msg.path.display(), msg.typo,); println!("{}: {} is disallowed", msg.path.display(), msg.typo,);
@ -257,8 +257,8 @@ impl Report for PrintLong {
Message::BinaryFile(msg) => { Message::BinaryFile(msg) => {
log::info!("{}", msg); log::info!("{}", msg);
} }
Message::Correction(msg) => print_long_correction(msg), Message::FileTypo(msg) => print_long_correction(msg),
Message::PathCorrection(msg) => match &msg.corrections { Message::PathTypo(msg) => match &msg.corrections {
crate::Status::Valid => {} crate::Status::Valid => {}
crate::Status::Invalid => { crate::Status::Invalid => {
println!( 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_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();

View file

@ -57,7 +57,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<'_>) -> bool { fn report(&self, msg: typos::report::Message<'_>) -> bool {
match msg { 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 => { typos::Status::Corrections(corrections) if corrections.len() == 1 => {
let path = msg.path.to_owned(); let path = msg.path.to_owned();
let line_num = msg.line_num; let line_num = msg.line_num;
@ -73,11 +73,9 @@ impl<'r> typos::report::Report for Replace<'r> {
content.push(correction); content.push(correction);
false false
} }
_ => self _ => self.reporter.report(typos::report::Message::FileTypo(msg)),
.reporter
.report(typos::report::Message::Correction(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 => { typos::Status::Corrections(corrections) if corrections.len() == 1 => {
let path = msg.path.to_owned(); let path = msg.path.to_owned();
let correction = let correction =
@ -87,9 +85,7 @@ impl<'r> typos::report::Report for Replace<'r> {
content.push(correction); content.push(correction);
false false
} }
_ => self _ => self.reporter.report(typos::report::Message::PathTypo(msg)),
.reporter
.report(typos::report::Message::PathCorrection(msg)),
}, },
_ => self.reporter.report(msg), _ => self.reporter.report(msg),
} }
@ -208,7 +204,7 @@ mod test {
let primary = typos::report::PrintSilent; let primary = typos::report::PrintSilent;
let replace = Replace::new(&primary); let replace = Replace::new(&primary);
replace.report( replace.report(
typos::report::Correction::default() typos::report::FileTypo::default()
.path(input_file.path()) .path(input_file.path())
.line(b"1 foo 2\n3 4 5") .line(b"1 foo 2\n3 4 5")
.line_num(1) .line_num(1)
@ -233,7 +229,7 @@ mod test {
let primary = typos::report::PrintSilent; let primary = typos::report::PrintSilent;
let replace = Replace::new(&primary); let replace = Replace::new(&primary);
replace.report( replace.report(
typos::report::PathCorrection::default() typos::report::PathTypo::default()
.path(input_file.path()) .path(input_file.path())
.byte_offset(0) .byte_offset(0)
.typo("foo") .typo("foo")