chore: Run cargo fmt

This commit is contained in:
Ed Page 2019-06-14 06:43:21 -06:00
parent 42e51be1b7
commit 9f198c973d
8 changed files with 86 additions and 29 deletions

View file

@ -20,4 +20,3 @@ fn no_correction(b: &mut test::Bencher) {
assert_eq!(corrections.correct_str("success"), None); assert_eq!(corrections.correct_str("success"), None);
b.iter(|| corrections.correct_str("success")); b.iter(|| corrections.correct_str("success"));
} }

View file

@ -29,4 +29,3 @@ fn main() {
"; ";
pub const CORPUS: &str = include_str!("../assets/words.csv"); pub const CORPUS: &str = include_str!("../assets/words.csv");

View file

@ -13,7 +13,13 @@ fn process_empty(b: &mut test::Bencher) {
sample_path.write_str(data::EMPTY).unwrap(); sample_path.write_str(data::EMPTY).unwrap();
let corrections = defenestrate::Dictionary::new(); let corrections = defenestrate::Dictionary::new();
b.iter(|| defenestrate::process_file(sample_path.path(), &corrections, defenestrate::report::print_silent)); b.iter(|| {
defenestrate::process_file(
sample_path.path(),
&corrections,
defenestrate::report::print_silent,
)
});
temp.close().unwrap(); temp.close().unwrap();
} }
@ -25,7 +31,13 @@ fn process_no_tokens(b: &mut test::Bencher) {
sample_path.write_str(data::NO_TOKENS).unwrap(); sample_path.write_str(data::NO_TOKENS).unwrap();
let corrections = defenestrate::Dictionary::new(); let corrections = defenestrate::Dictionary::new();
b.iter(|| defenestrate::process_file(sample_path.path(), &corrections, defenestrate::report::print_silent)); b.iter(|| {
defenestrate::process_file(
sample_path.path(),
&corrections,
defenestrate::report::print_silent,
)
});
temp.close().unwrap(); temp.close().unwrap();
} }
@ -37,7 +49,13 @@ fn process_single_token(b: &mut test::Bencher) {
sample_path.write_str(data::SINGLE_TOKEN).unwrap(); sample_path.write_str(data::SINGLE_TOKEN).unwrap();
let corrections = defenestrate::Dictionary::new(); let corrections = defenestrate::Dictionary::new();
b.iter(|| defenestrate::process_file(sample_path.path(), &corrections, defenestrate::report::print_silent)); b.iter(|| {
defenestrate::process_file(
sample_path.path(),
&corrections,
defenestrate::report::print_silent,
)
});
temp.close().unwrap(); temp.close().unwrap();
} }
@ -49,7 +67,13 @@ fn process_sherlock(b: &mut test::Bencher) {
sample_path.write_str(data::SHERLOCK).unwrap(); sample_path.write_str(data::SHERLOCK).unwrap();
let corrections = defenestrate::Dictionary::new(); let corrections = defenestrate::Dictionary::new();
b.iter(|| defenestrate::process_file(sample_path.path(), &corrections, defenestrate::report::print_silent)); b.iter(|| {
defenestrate::process_file(
sample_path.path(),
&corrections,
defenestrate::report::print_silent,
)
});
temp.close().unwrap(); temp.close().unwrap();
} }
@ -61,7 +85,13 @@ fn process_code(b: &mut test::Bencher) {
sample_path.write_str(data::CODE).unwrap(); sample_path.write_str(data::CODE).unwrap();
let corrections = defenestrate::Dictionary::new(); let corrections = defenestrate::Dictionary::new();
b.iter(|| defenestrate::process_file(sample_path.path(), &corrections, defenestrate::report::print_silent)); b.iter(|| {
defenestrate::process_file(
sample_path.path(),
&corrections,
defenestrate::report::print_silent,
)
});
temp.close().unwrap(); temp.close().unwrap();
} }
@ -73,7 +103,13 @@ fn process_corpus(b: &mut test::Bencher) {
sample_path.write_str(data::CORPUS).unwrap(); sample_path.write_str(data::CORPUS).unwrap();
let corrections = defenestrate::Dictionary::new(); let corrections = defenestrate::Dictionary::new();
b.iter(|| defenestrate::process_file(sample_path.path(), &corrections, defenestrate::report::print_silent)); b.iter(|| {
defenestrate::process_file(
sample_path.path(),
&corrections,
defenestrate::report::print_silent,
)
});
temp.close().unwrap(); temp.close().unwrap();
} }

View file

@ -16,7 +16,9 @@ fn tokenize_no_tokens(b: &mut test::Bencher) {
#[bench] #[bench]
fn tokenize_single_token(b: &mut test::Bencher) { fn tokenize_single_token(b: &mut test::Bencher) {
b.iter(|| defenestrate::tokens::Symbol::parse(data::SINGLE_TOKEN.as_bytes()).collect::<Vec<_>>()); b.iter(|| {
defenestrate::tokens::Symbol::parse(data::SINGLE_TOKEN.as_bytes()).collect::<Vec<_>>()
});
} }
#[bench] #[bench]

View file

@ -11,7 +11,11 @@ pub use crate::dict::*;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
pub fn process_file(path: &std::path::Path, dictionary: &Dictionary, report: report::Report) -> Result<(), failure::Error> { pub fn process_file(
path: &std::path::Path,
dictionary: &Dictionary,
report: report::Report,
) -> Result<(), failure::Error> {
let mut buffer = Vec::new(); let mut buffer = Vec::new();
File::open(path)?.read_to_end(&mut buffer)?; File::open(path)?.read_to_end(&mut buffer)?;
for (line_idx, line) in grep_searcher::LineIter::new(b'\n', &buffer).enumerate() { for (line_idx, line) in grep_searcher::LineIter::new(b'\n', &buffer).enumerate() {
@ -38,4 +42,3 @@ pub fn process_file(path: &std::path::Path, dictionary: &Dictionary, report: rep
Ok(()) Ok(())
} }

View file

@ -4,7 +4,7 @@ extern crate clap;
use structopt::StructOpt; use structopt::StructOpt;
arg_enum!{ arg_enum! {
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Format { enum Format {
Silent, Silent,
@ -37,13 +37,14 @@ struct Options {
/// Paths to check /// Paths to check
path: Vec<std::path::PathBuf>, path: Vec<std::path::PathBuf>,
#[structopt(
#[structopt(long = "format", long = "format",
raw(possible_values = "&Format::variants()", case_insensitive = "true"), raw(possible_values = "&Format::variants()", case_insensitive = "true"),
default_value = "long")] default_value = "long"
)]
pub format: Format, pub format: Format,
#[structopt(short="j", long="threads", default_value="0")] #[structopt(short = "j", long = "threads", default_value = "0")]
/// The approximate number of threads to use. /// The approximate number of threads to use.
threads: usize, threads: usize,
} }
@ -65,7 +66,10 @@ fn run() -> Result<(), failure::Error> {
let dictionary = defenestrate::Dictionary::new(); let dictionary = defenestrate::Dictionary::new();
let first_path = &options.path.get(0).expect("arg parsing enforces at least one"); let first_path = &options
.path
.get(0)
.expect("arg parsing enforces at least one");
let mut walk = ignore::WalkBuilder::new(first_path); let mut walk = ignore::WalkBuilder::new(first_path);
for path in &options.path[1..] { for path in &options.path[1..] {
walk.add(path); walk.add(path);

View file

@ -13,11 +13,17 @@ pub struct Message<'m> {
pub type Report = fn(msg: Message); pub type Report = fn(msg: Message);
pub fn print_silent(_: Message) { pub fn print_silent(_: Message) {}
}
pub fn print_brief(msg: Message) { pub fn print_brief(msg: Message) {
println!("{}:{}:{}: {} -> {}", msg.path.display(), msg.line_num, msg.col_num, msg.word, msg.correction); println!(
"{}:{}:{}: {} -> {}",
msg.path.display(),
msg.line_num,
msg.col_num,
msg.word,
msg.correction
);
} }
pub fn print_long(msg: Message) { pub fn print_long(msg: Message) {
@ -28,9 +34,18 @@ pub fn print_long(msg: Message) {
let hl: String = itertools::repeat_n("^", msg.word.len()).collect(); let hl: String = itertools::repeat_n("^", msg.word.len()).collect();
println!("error: `{}` should be `{}`", msg.word, msg.correction); println!("error: `{}` should be `{}`", msg.word, msg.correction);
println!(" --> {}:{}:{}", msg.path.display(), msg.line_num, msg.col_num); println!(
" --> {}:{}:{}",
msg.path.display(),
msg.line_num,
msg.col_num
);
println!("{} |", line_indent); println!("{} |", line_indent);
println!("{} | {}", msg.line_num, String::from_utf8_lossy(msg.line).trim_end()); println!(
"{} | {}",
msg.line_num,
String::from_utf8_lossy(msg.line).trim_end()
);
println!("{} | {}{}", line_indent, hl_indent, hl); println!("{} | {}{}", line_indent, hl_indent, hl);
println!("{} |", line_indent); println!("{} |", line_indent);
} }

View file

@ -6,17 +6,16 @@ pub struct Symbol<'t> {
impl<'t> Symbol<'t> { impl<'t> Symbol<'t> {
pub fn new(token: &'t [u8], offset: usize) -> Self { pub fn new(token: &'t [u8], offset: usize) -> Self {
Self { Self { token, offset }
token,
offset,
}
} }
pub fn parse<'s>(content: &'s [u8]) -> impl Iterator<Item=Symbol<'s>> { pub fn parse<'s>(content: &'s [u8]) -> impl Iterator<Item = Symbol<'s>> {
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref SPLIT: regex::bytes::Regex = regex::bytes::Regex::new(r#"\b(\p{Alphabetic}|\d|_)+\b"#).unwrap(); static ref SPLIT: regex::bytes::Regex = regex::bytes::Regex::new(r#"\b(\p{Alphabetic}|\d|_)+\b"#).unwrap();
} }
SPLIT.find_iter(content).map(|m| Symbol::new(m.as_bytes(), m.start())) SPLIT
.find_iter(content)
.map(|m| Symbol::new(m.as_bytes(), m.start()))
} }
} }