typos/src/lib.rs

108 lines
3.3 KiB
Rust
Raw Normal View History

2019-01-23 07:33:51 -07:00
#[macro_use]
extern crate serde_derive;
2019-01-24 08:24:20 -07:00
mod dict;
2019-06-14 14:53:34 -06:00
mod dict_codegen;
2019-01-22 15:01:33 -07:00
2019-01-24 08:24:20 -07:00
pub mod report;
2019-04-16 20:16:31 -06:00
pub mod tokens;
2019-01-23 07:44:01 -07:00
2019-01-24 08:24:20 -07:00
pub use crate::dict::*;
2019-01-22 15:01:33 -07:00
2019-01-24 08:24:20 -07:00
use std::fs::File;
use std::io::Read;
2019-01-23 07:33:51 -07:00
use bstr::ByteSlice;
2019-06-14 06:43:21 -06:00
pub fn process_file(
path: &std::path::Path,
dictionary: &Dictionary,
2019-07-18 20:20:45 -06:00
check_filenames: bool,
check_files: bool,
parser: &tokens::Parser,
2019-07-13 20:14:06 -06:00
binary: bool,
2019-06-14 06:43:21 -06:00
report: report::Report,
) -> Result<bool, failure::Error> {
let mut typos_found = false;
2019-07-18 20:20:45 -06:00
if check_filenames {
for part in path.components().filter_map(|c| c.as_os_str().to_str()) {
for ident in parser.parse(part) {
2019-07-18 20:20:45 -06:00
if let Some(correction) = dictionary.correct_ident(ident) {
let msg = report::FilenameCorrection {
path,
typo: ident.token(),
correction,
non_exhaustive: (),
};
report(msg.into());
typos_found = true;
2019-07-18 20:20:45 -06:00
}
for word in ident.split() {
if let Some(correction) = dictionary.correct_word(word) {
let msg = report::FilenameCorrection {
path,
typo: word.token(),
correction,
non_exhaustive: (),
};
report(msg.into());
typos_found = true;
2019-07-18 20:20:45 -06:00
}
}
}
}
}
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(typos_found);
}
for (line_idx, line) in buffer.lines().enumerate() {
let line_num = line_idx + 1;
for ident in parser.parse_bytes(line) {
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: (),
};
typos_found = true;
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: (),
};
typos_found = true;
report(msg.into());
}
}
}
2019-01-22 15:01:33 -07:00
}
}
Ok(typos_found)
2019-01-22 15:01:33 -07:00
}