Skip to content

Commit

Permalink
fix: Report binary files to user
Browse files Browse the repository at this point in the history
Fixes #38
  • Loading branch information
epage committed Jul 19, 2019
1 parent 807a4a8 commit d247d68
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -58,7 +63,7 @@ pub fn process_file(
correction,
non_exhaustive: (),
};
report(msg);
report(msg.into());
}
}
}
Expand Down
61 changes: 52 additions & 9 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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,
#[serde(skip)]
pub line: &'m [u8],
Expand All @@ -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();

Expand Down

0 comments on commit d247d68

Please sign in to comment.