Skip to content

Commit

Permalink
refactor(dict): Split out a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jul 28, 2019
1 parent 834b9f7 commit adcbe68
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
6 changes: 3 additions & 3 deletions benches/corrections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ extern crate test;

#[bench]
fn load_corrections(b: &mut test::Bencher) {
b.iter(|| typos::Dictionary::new());
b.iter(|| typos::BuiltIn::new());
}

#[bench]
fn correct_word_hit(b: &mut test::Bencher) {
let corrections = typos::Dictionary::new();
let corrections = typos::BuiltIn::new();
let input = typos::tokens::Word::new("successs", 0).unwrap();
assert_eq!(
corrections.correct_word(input),
Expand All @@ -20,7 +20,7 @@ fn correct_word_hit(b: &mut test::Bencher) {

#[bench]
fn correct_word_miss(b: &mut test::Bencher) {
let corrections = typos::Dictionary::new();
let corrections = typos::BuiltIn::new();
let input = typos::tokens::Word::new("success", 0).unwrap();
assert_eq!(corrections.correct_word(input), None);
b.iter(|| corrections.correct_word(input));
Expand Down
12 changes: 6 additions & 6 deletions benches/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn process_empty(b: &mut test::Bencher) {
let sample_path = temp.child("sample");
sample_path.write_str(data::EMPTY).unwrap();

let corrections = typos::Dictionary::new();
let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser);
b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent));
Expand All @@ -26,7 +26,7 @@ fn process_no_tokens(b: &mut test::Bencher) {
let sample_path = temp.child("sample");
sample_path.write_str(data::NO_TOKENS).unwrap();

let corrections = typos::Dictionary::new();
let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser);
b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent));
Expand All @@ -40,7 +40,7 @@ fn process_single_token(b: &mut test::Bencher) {
let sample_path = temp.child("sample");
sample_path.write_str(data::SINGLE_TOKEN).unwrap();

let corrections = typos::Dictionary::new();
let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser);
b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent));
Expand All @@ -54,7 +54,7 @@ fn process_sherlock(b: &mut test::Bencher) {
let sample_path = temp.child("sample");
sample_path.write_str(data::SHERLOCK).unwrap();

let corrections = typos::Dictionary::new();
let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser);
b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent));
Expand All @@ -68,7 +68,7 @@ fn process_code(b: &mut test::Bencher) {
let sample_path = temp.child("sample");
sample_path.write_str(data::CODE).unwrap();

let corrections = typos::Dictionary::new();
let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser);
b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent));
Expand All @@ -82,7 +82,7 @@ fn process_corpus(b: &mut test::Bencher) {
let sample_path = temp.child("sample");
sample_path.write_str(data::CORPUS).unwrap();

let corrections = typos::Dictionary::new();
let corrections = typos::BuiltIn::new();
let parser = typos::tokens::Parser::new();
let checks = typos::checks::CheckSettings::new().build(&corrections, &parser);
b.iter(|| checks.check_file(sample_path.path(), typos::report::print_silent));
Expand Down
28 changes: 25 additions & 3 deletions src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ use unicase::UniCase;

use crate::tokens::Case;

pub trait Dictionary {
fn correct_ident<'s, 'w>(
&'s self,
_ident: crate::tokens::Identifier<'w>,
) -> Option<Cow<'s, str>>;

fn correct_word<'s, 'w>(&'s self, word: crate::tokens::Word<'w>) -> Option<Cow<'s, str>>;
}

#[derive(Default)]
pub struct Dictionary {}
pub struct BuiltIn {}

impl Dictionary {
impl BuiltIn {
pub fn new() -> Self {
Dictionary {}
Self {}
}

pub fn correct_ident<'s, 'w>(
Expand All @@ -25,6 +34,19 @@ impl Dictionary {
}
}

impl Dictionary for BuiltIn {
fn correct_ident<'s, 'w>(
&'s self,
ident: crate::tokens::Identifier<'w>,
) -> Option<Cow<'s, str>> {
BuiltIn::correct_ident(self, ident)
}

fn correct_word<'s, 'w>(&'s self, word: crate::tokens::Word<'w>) -> Option<Cow<'s, str>> {
BuiltIn::correct_word(self, word)
}
}

fn map_lookup(
map: &'static phf::Map<UniCase<&'static str>, &'static str>,
key: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fn run() -> Result<i32, failure::Error> {
let ignore_hex = options.ignore_hex().unwrap_or(true);
let binary = options.binary().unwrap_or(false);

let dictionary = typos::Dictionary::new();
let dictionary = typos::BuiltIn::new();

let parser = typos::tokens::ParserBuilder::new()
.ignore_hex(ignore_hex)
Expand Down

0 comments on commit adcbe68

Please sign in to comment.