typos/src/lib.rs

58 lines
1.6 KiB
Rust
Raw Normal View History

2019-01-23 09:33:51 -05:00
#[macro_use]
extern crate serde_derive;
2019-01-24 10:24:20 -05:00
mod dict;
2019-06-14 16:53:34 -04:00
mod dict_codegen;
2019-01-22 17:01:33 -05:00
2019-01-24 10:24:20 -05:00
pub mod report;
2019-04-16 22:16:31 -04:00
pub mod tokens;
2019-01-23 09:44:01 -05:00
2019-01-24 10:24:20 -05:00
pub use crate::dict::*;
2019-01-22 17:01:33 -05:00
2019-01-24 10:24:20 -05:00
use std::fs::File;
use std::io::Read;
2019-01-23 09:33:51 -05:00
2019-06-14 08:43:21 -04:00
pub fn process_file(
path: &std::path::Path,
dictionary: &Dictionary,
report: report::Report,
) -> Result<(), failure::Error> {
2019-01-22 17:01:33 -05:00
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 symbol in tokens::Symbol::parse(line) {
if let Some(correction) = dictionary.correct_symbol(symbol) {
let col_num = symbol.offset();
let msg = report::Message {
path,
line,
line_num,
col_num,
word: symbol.token(),
correction,
non_exhaustive: (),
};
report(msg);
2019-01-22 17:01:33 -05:00
}
for word in symbol.split() {
if let Some(correction) = dictionary.correct_word(word) {
let col_num = word.offset();
let msg = report::Message {
path,
line,
line_num,
col_num,
word: word.token(),
correction,
non_exhaustive: (),
};
report(msg);
}
}
2019-01-22 17:01:33 -05:00
}
}
Ok(())
}