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) {
let corrections = defenestrate::Dictionary::new();
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));
}

View file

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

View file

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