Skip to content

Commit

Permalink
feat: Give control over verifying file content
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 19, 2019
1 parent ec307df commit 95c0aea
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
6 changes: 6 additions & 0 deletions benches/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn process_empty(b: &mut test::Bencher) {
&corrections,
true,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -40,6 +41,7 @@ fn process_no_tokens(b: &mut test::Bencher) {
&corrections,
true,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -61,6 +63,7 @@ fn process_single_token(b: &mut test::Bencher) {
&corrections,
true,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -82,6 +85,7 @@ fn process_sherlock(b: &mut test::Bencher) {
&corrections,
true,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -103,6 +107,7 @@ fn process_code(b: &mut test::Bencher) {
&corrections,
true,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -124,6 +129,7 @@ fn process_corpus(b: &mut test::Bencher) {
&corrections,
true,
true,
true,
false,
typos::report::print_silent,
)
Expand Down
69 changes: 36 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn process_file(
path: &std::path::Path,
dictionary: &Dictionary,
check_filenames: bool,
check_files: bool,
ignore_hex: bool,
binary: bool,
report: report::Report,
Expand Down Expand Up @@ -52,50 +53,52 @@ 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(());
}
if check_files {
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(());
}

for (line_idx, line) in buffer.lines().enumerate() {
let line_num = line_idx + 1;
for ident in tokens::Identifier::parse_bytes(line) {
if !ignore_hex && is_hex(ident.token()) {
continue;
}
if let Some(correction) = dictionary.correct_ident(ident) {
let col_num = ident.offset();
let msg = report::Correction {
path,
line,
line_num,
col_num,
typo: ident.token(),
correction,
non_exhaustive: (),
};
report(msg.into());
}
for word in ident.split() {
if let Some(correction) = dictionary.correct_word(word) {
let col_num = word.offset();
for (line_idx, line) in buffer.lines().enumerate() {
let line_num = line_idx + 1;
for ident in tokens::Identifier::parse_bytes(line) {
if !ignore_hex && is_hex(ident.token()) {
continue;
}
if let Some(correction) = dictionary.correct_ident(ident) {
let col_num = ident.offset();
let msg = report::Correction {
path,
line,
line_num,
col_num,
typo: word.token(),
typo: ident.token(),
correction,
non_exhaustive: (),
};
report(msg.into());
}
for word in ident.split() {
if let Some(correction) = dictionary.correct_word(word) {
let col_num = word.offset();
let msg = report::Correction {
path,
line,
line_num,
col_num,
typo: word.token(),
correction,
non_exhaustive: (),
};
report(msg.into());
}
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ struct Options {
)]
check_filenames: bool,

#[structopt(long, raw(overrides_with = r#""check-files""#))]
/// Skip verifying spelling in filess.
no_check_files: bool,
#[structopt(
long,
raw(overrides_with = r#""no-check-files""#),
raw(hidden = "true")
)]
check_files: bool,

#[structopt(long, raw(overrides_with = r#""hex""#))]
/// Don't try to detect that an identifier looks like hex
no_hex: bool,
Expand Down Expand Up @@ -125,6 +135,15 @@ impl Options {
self
}

pub fn check_files(&self) -> Option<bool> {
match (self.check_files, self.no_check_files) {
(true, false) => Some(true),
(false, true) => Some(false),
(false, false) => None,
(_, _) => unreachable!("StructOpt should make this impossible"),
}
}

pub fn check_filenames(&self) -> Option<bool> {
match (self.check_filenames, self.no_check_filenames) {
(true, false) => Some(true),
Expand Down Expand Up @@ -217,6 +236,7 @@ fn run() -> Result<(), failure::Error> {

let dictionary = typos::Dictionary::new();
let check_filenames = options.check_filenames().unwrap_or(true);
let check_files = options.check_files().unwrap_or(true);
let ignore_hex = options.ignore_hex().unwrap_or(true);
let binary = options.binary().unwrap_or(false);

Expand All @@ -243,6 +263,7 @@ fn run() -> Result<(), failure::Error> {
entry.path(),
&dictionary,
check_filenames,
check_files,
ignore_hex,
binary,
options.format.report(),
Expand Down

0 comments on commit 95c0aea

Please sign in to comment.