diff --git a/src/lib.rs b/src/lib.rs index f46af98..facb8bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,11 @@ pub fn process_file( let mut buffer = Vec::new(); File::open(path)?.read_to_end(&mut buffer)?; if !binary && buffer.find_byte(b'\0').is_some() { + let msg = report::BinaryFile { + path, + non_exhaustive: (), + }; + report(msg.into()); return Ok(()); } @@ -35,7 +40,7 @@ pub fn process_file( } if let Some(correction) = dictionary.correct_ident(ident) { let col_num = ident.offset(); - let msg = report::Message { + let msg = report::Correction { path, line, line_num, @@ -44,12 +49,12 @@ pub fn process_file( correction, non_exhaustive: (), }; - report(msg); + report(msg.into()); } for word in ident.split() { if let Some(correction) = dictionary.correct_word(word) { let col_num = word.offset(); - let msg = report::Message { + let msg = report::Correction { path, line, line_num, @@ -58,7 +63,7 @@ pub fn process_file( correction, non_exhaustive: (), }; - report(msg); + report(msg.into()); } } } diff --git a/src/report.rs b/src/report.rs index 129755a..6247264 100644 --- a/src/report.rs +++ b/src/report.rs @@ -2,7 +2,34 @@ use std::borrow::Cow; use std::io::{self, Write}; #[derive(Clone, Debug, Serialize)] -pub struct Message<'m> { +#[serde(rename_all = "snake_case")] +#[serde(tag = "type")] +pub enum Message<'m> { + BinaryFile(BinaryFile<'m>), + Correction(Correction<'m>), +} + +impl<'m> From> for Message<'m> { + fn from(msg: BinaryFile<'m>) -> Self { + Message::BinaryFile(msg) + } +} + +impl<'m> From> for Message<'m> { + fn from(msg: Correction<'m>) -> Self { + Message::Correction(msg) + } +} + +#[derive(Clone, Debug, Serialize)] +pub struct BinaryFile<'m> { + pub path: &'m std::path::Path, + #[serde(skip)] + pub(crate) non_exhaustive: (), +} + +#[derive(Clone, Debug, Serialize)] +pub struct Correction<'m> { pub path: &'m std::path::Path, #[serde(skip)] pub line: &'m [u8], @@ -19,17 +46,33 @@ pub type Report = fn(msg: Message); pub fn print_silent(_: Message) {} pub fn print_brief(msg: Message) { - println!( - "{}:{}:{}: {} -> {}", - msg.path.display(), - msg.line_num, - msg.col_num, - msg.typo, - msg.correction - ); + match msg { + Message::BinaryFile(msg) => { + println!("Skipping binary file {}", msg.path.display(),); + } + Message::Correction(msg) => { + println!( + "{}:{}:{}: {} -> {}", + msg.path.display(), + msg.line_num, + msg.col_num, + msg.typo, + msg.correction + ); + } + } } pub fn print_long(msg: Message) { + match msg { + Message::BinaryFile(msg) => { + println!("Skipping binary file {}", msg.path.display(),); + } + Message::Correction(msg) => print_long_correction(msg), + } +} + +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();