From a5b8636bdbd3556d7a9162e0d0faac050d0ddc79 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 24 Jun 2019 21:45:30 -0600 Subject: [PATCH] refactor(dict): Allow for owned corrections --- benches/corrections.rs | 5 ++++- src/dict.rs | 8 +++++--- src/report.rs | 5 +++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/benches/corrections.rs b/benches/corrections.rs index 4c16bcc..71c911a 100644 --- a/benches/corrections.rs +++ b/benches/corrections.rs @@ -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)); } diff --git a/src/dict.rs b/src/dict.rs index 7294924..c5160d7 100644 --- a/src/dict.rs +++ b/src/dict.rs @@ -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> { 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> { + map_lookup(&crate::dict_codegen::WORD_DICTIONARY, word.token()).map(|s| s.into()) } } diff --git a/src/report.rs b/src/report.rs index 09e9c07..129755a 100644 --- a/src/report.rs +++ b/src/report.rs @@ -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: (), }