fix: Report binary files to user

Fixes #38
This commit is contained in:
Ed Page 2019-07-16 19:16:54 -06:00
parent 807a4a8a2f
commit d247d68c37
2 changed files with 61 additions and 13 deletions

View file

@ -24,6 +24,11 @@ pub fn process_file(
let mut buffer = Vec::new(); let mut buffer = Vec::new();
File::open(path)?.read_to_end(&mut buffer)?; File::open(path)?.read_to_end(&mut buffer)?;
if !binary && buffer.find_byte(b'\0').is_some() { if !binary && buffer.find_byte(b'\0').is_some() {
let msg = report::BinaryFile {
path,
non_exhaustive: (),
};
report(msg.into());
return Ok(()); return Ok(());
} }
@ -35,7 +40,7 @@ pub fn process_file(
} }
if let Some(correction) = dictionary.correct_ident(ident) { if let Some(correction) = dictionary.correct_ident(ident) {
let col_num = ident.offset(); let col_num = ident.offset();
let msg = report::Message { let msg = report::Correction {
path, path,
line, line,
line_num, line_num,
@ -44,12 +49,12 @@ pub fn process_file(
correction, correction,
non_exhaustive: (), non_exhaustive: (),
}; };
report(msg); report(msg.into());
} }
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) {
let col_num = word.offset(); let col_num = word.offset();
let msg = report::Message { let msg = report::Correction {
path, path,
line, line,
line_num, line_num,
@ -58,7 +63,7 @@ pub fn process_file(
correction, correction,
non_exhaustive: (), non_exhaustive: (),
}; };
report(msg); report(msg.into());
} }
} }
} }

View file

@ -2,7 +2,34 @@ use std::borrow::Cow;
use std::io::{self, Write}; use std::io::{self, Write};
#[derive(Clone, Debug, Serialize)] #[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<BinaryFile<'m>> for Message<'m> {
fn from(msg: BinaryFile<'m>) -> Self {
Message::BinaryFile(msg)
}
}
impl<'m> From<Correction<'m>> 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, pub path: &'m std::path::Path,
#[serde(skip)] #[serde(skip)]
pub line: &'m [u8], pub line: &'m [u8],
@ -19,6 +46,11 @@ pub type Report = fn(msg: Message);
pub fn print_silent(_: Message) {} pub fn print_silent(_: Message) {}
pub fn print_brief(msg: Message) { pub fn print_brief(msg: Message) {
match msg {
Message::BinaryFile(msg) => {
println!("Skipping binary file {}", msg.path.display(),);
}
Message::Correction(msg) => {
println!( println!(
"{}:{}:{}: {} -> {}", "{}:{}:{}: {} -> {}",
msg.path.display(), msg.path.display(),
@ -28,8 +60,19 @@ pub fn print_brief(msg: Message) {
msg.correction msg.correction
); );
} }
}
}
pub fn print_long(msg: Message) { 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_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();