Skip to content

Commit

Permalink
chore: Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 14, 2019
1 parent 42e51be commit 9f198c9
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 29 deletions.
1 change: 0 additions & 1 deletion benches/corrections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ fn no_correction(b: &mut test::Bencher) {
assert_eq!(corrections.correct_str("success"), None);
b.iter(|| corrections.correct_str("success"));
}

1 change: 0 additions & 1 deletion benches/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ fn main() {
";

pub const CORPUS: &str = include_str!("../assets/words.csv");

48 changes: 42 additions & 6 deletions benches/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ fn process_empty(b: &mut test::Bencher) {
sample_path.write_str(data::EMPTY).unwrap();

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();
}
Expand All @@ -25,7 +31,13 @@ fn process_no_tokens(b: &mut test::Bencher) {
sample_path.write_str(data::NO_TOKENS).unwrap();

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();
}
Expand All @@ -37,7 +49,13 @@ fn process_single_token(b: &mut test::Bencher) {
sample_path.write_str(data::SINGLE_TOKEN).unwrap();

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();
}
Expand All @@ -49,7 +67,13 @@ fn process_sherlock(b: &mut test::Bencher) {
sample_path.write_str(data::SHERLOCK).unwrap();

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();
}
Expand All @@ -61,7 +85,13 @@ fn process_code(b: &mut test::Bencher) {
sample_path.write_str(data::CODE).unwrap();

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();
}
Expand All @@ -73,7 +103,13 @@ fn process_corpus(b: &mut test::Bencher) {
sample_path.write_str(data::CORPUS).unwrap();

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();
}
4 changes: 3 additions & 1 deletion benches/tokenize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ fn tokenize_no_tokens(b: &mut test::Bencher) {

#[bench]
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]
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ 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> {
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() {
Expand All @@ -38,4 +42,3 @@ pub fn process_file(path: &std::path::Path, dictionary: &Dictionary, report: rep

Ok(())
}

18 changes: 11 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate clap;

use structopt::StructOpt;

arg_enum!{
arg_enum! {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Format {
Silent,
Expand Down Expand Up @@ -37,13 +37,14 @@ struct Options {
/// Paths to check
path: Vec<std::path::PathBuf>,


#[structopt(long = "format",
raw(possible_values = "&Format::variants()", case_insensitive = "true"),
default_value = "long")]
#[structopt(
long = "format",
raw(possible_values = "&Format::variants()", case_insensitive = "true"),
default_value = "long"
)]
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.
threads: usize,
}
Expand All @@ -65,7 +66,10 @@ fn run() -> Result<(), failure::Error> {

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);
for path in &options.path[1..] {
walk.add(path);
Expand Down
25 changes: 20 additions & 5 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ pub struct Message<'m> {

pub type Report = fn(msg: Message);

pub fn print_silent(_: Message) {
}
pub fn print_silent(_: 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) {
Expand All @@ -28,9 +34,18 @@ pub fn print_long(msg: Message) {
let hl: String = itertools::repeat_n("^", msg.word.len()).collect();

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!("{} | {}", 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);
}
Expand Down
11 changes: 5 additions & 6 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ pub struct Symbol<'t> {

impl<'t> Symbol<'t> {
pub fn new(token: &'t [u8], offset: usize) -> Self {
Self {
token,
offset,
}
Self { 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! {
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()))
}
}

Expand Down

0 comments on commit 9f198c9

Please sign in to comment.