diff --git a/benches/corrections.rs b/benches/corrections.rs index b849986..8e0d751 100644 --- a/benches/corrections.rs +++ b/benches/corrections.rs @@ -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)); diff --git a/benches/file.rs b/benches/file.rs index d25c7d1..0783e86 100644 --- a/benches/file.rs +++ b/benches/file.rs @@ -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)); diff --git a/src/dict.rs b/src/dict.rs index 1861757..1ff7359 100644 --- a/src/dict.rs +++ b/src/dict.rs @@ -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>; -impl Dictionary { + fn correct_word<'s, 'w>(&'s self, word: crate::tokens::Word<'w>) -> Option>; +} + +#[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> { + BuiltIn::correct_ident(self, ident) + } + + fn correct_word<'s, 'w>(&'s self, word: crate::tokens::Word<'w>) -> Option> { + BuiltIn::correct_word(self, word) + } +} + fn map_lookup( map: &'static phf::Map, &'static str>, key: &str, diff --git a/src/main.rs b/src/main.rs index efe688e..8673f7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -260,7 +260,7 @@ fn run() -> Result { 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)