Skip to content

Commit

Permalink
refactor(dict): Allow for owned corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 25, 2019
1 parent b12e90c commit a5b8636
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 4 additions & 1 deletion benches/corrections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
8 changes: 5 additions & 3 deletions src/dict.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use unicase::UniCase;

#[derive(Default)]
Expand All @@ -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())
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/report.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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)]
pub line: &'m [u8],
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: (),
}
Expand Down

0 comments on commit a5b8636

Please sign in to comment.