refactor(dict): Split out a trait

This commit is contained in:
Ed Page 2019-07-27 19:42:45 -06:00
parent 834b9f77f2
commit adcbe68621
4 changed files with 36 additions and 14 deletions

View file

@ -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),
@ -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));

View file

@ -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));
@ -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));
@ -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));
@ -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));
@ -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));
@ -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));

View file

@ -4,12 +4,21 @@ use unicase::UniCase;
use crate::tokens::Case;
#[derive(Default)]
pub struct Dictionary {}
pub trait Dictionary {
fn correct_ident<'s, 'w>(
&'s self,
_ident: crate::tokens::Identifier<'w>,
) -> Option<Cow<'s, str>>;
impl Dictionary {
fn correct_word<'s, 'w>(&'s self, word: crate::tokens::Word<'w>) -> Option<Cow<'s, str>>;
}
#[derive(Default)]
pub struct BuiltIn {}
impl BuiltIn {
pub fn new() -> Self {
Dictionary {}
Self {}
}
pub fn correct_ident<'s, 'w>(
@ -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,

View file

@ -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)