typos/src/lib.rs
2019-04-16 17:41:07 -06:00

41 lines
1.1 KiB
Rust

#[macro_use]
extern crate serde_derive;
mod dict;
pub mod identifier;
pub mod report;
pub use crate::dict::*;
use std::fs::File;
use std::io::Read;
pub fn process_file(path: &std::path::Path, dictionary: &Dictionary, report: report::Report) -> Result<(), failure::Error> {
let mut buffer = Vec::new();
File::open(path)?.read_to_end(&mut buffer)?;
for (line_idx, line) in grep_searcher::LineIter::new(b'\n', &buffer).enumerate() {
let line_num = line_idx + 1;
for token in identifier::tokenize(line) {
if let Some(word) = std::str::from_utf8(token.token).ok() {
// Correct tokens as-is
if let Some(correction) = dictionary.correct_str(word) {
let col_num = token.offset;
let msg = report::Message {
path,
line,
line_num,
col_num,
word,
correction,
non_exhaustive: (),
};
report(msg);
}
}
}
}
Ok(())
}