refactor(dict): Allow for owned corrections

This commit is contained in:
Ed Page 2019-06-24 21:45:30 -06:00
parent b12e90c141
commit a5b8636bdb
3 changed files with 12 additions and 6 deletions

View file

@ -11,7 +11,10 @@ fn load_corrections(b: &mut test::Bencher) {
fn correct_word_hit(b: &mut test::Bencher) { fn correct_word_hit(b: &mut test::Bencher) {
let corrections = defenestrate::Dictionary::new(); let corrections = defenestrate::Dictionary::new();
let input = defenestrate::tokens::Word::new("successs", 0).unwrap(); let input = defenestrate::tokens::Word::new("successs", 0).unwrap();
assert_eq!(corrections.correct_word(input), Some("successes")); assert_eq!(
corrections.correct_word(input),
Some(std::borrow::Cow::Borrowed("successes"))
);
b.iter(|| corrections.correct_word(input)); b.iter(|| corrections.correct_word(input));
} }

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use unicase::UniCase; use unicase::UniCase;
#[derive(Default)] #[derive(Default)]
@ -11,12 +13,12 @@ impl Dictionary {
pub fn correct_ident<'s, 'w>( pub fn correct_ident<'s, 'w>(
&'s self, &'s self,
_ident: crate::tokens::Identifier<'w>, _ident: crate::tokens::Identifier<'w>,
) -> Option<&'s str> { ) -> Option<Cow<'s, str>> {
None None
} }
pub fn correct_word<'s, 'w>(&'s self, word: crate::tokens::Word<'w>) -> Option<&'s str> { pub fn correct_word<'s, 'w>(&'s self, word: crate::tokens::Word<'w>) -> Option<Cow<'s, str>> {
map_lookup(&crate::dict_codegen::WORD_DICTIONARY, word.token()) map_lookup(&crate::dict_codegen::WORD_DICTIONARY, word.token()).map(|s| s.into())
} }
} }

View file

@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::io::{self, Write}; use std::io::{self, Write};
#[derive(Copy, Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub struct Message<'m> { pub struct Message<'m> {
pub path: &'m std::path::Path, pub path: &'m std::path::Path,
#[serde(skip)] #[serde(skip)]
@ -8,7 +9,7 @@ pub struct Message<'m> {
pub line_num: usize, pub line_num: usize,
pub col_num: usize, pub col_num: usize,
pub typo: &'m str, pub typo: &'m str,
pub correction: &'m str, pub correction: Cow<'m, str>,
#[serde(skip)] #[serde(skip)]
pub(crate) non_exhaustive: (), pub(crate) non_exhaustive: (),
} }