Skip to content

Commit

Permalink
refactor(dict): Make feature flag paths clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Page committed May 19, 2021
1 parent e6c595c commit d65fa79
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/dict.rs
Expand Up @@ -48,8 +48,10 @@ impl BuiltIn {
.for_each(|mut s| case_correct(&mut s, word_token.case()));
Some(corrections)
}
}

#[cfg(feature = "dict")]
#[cfg(feature = "dict")]
impl BuiltIn {
// Not using `Status` to avoid the allocations
fn correct_with_dict(&self, word: &str) -> Option<&'static [&'static str]> {
if typos_dict::WORD_RANGE.contains(&word.len()) {
Expand All @@ -58,13 +60,17 @@ impl BuiltIn {
None
}
}
}

#[cfg(not(feature = "dict"))]
#[cfg(not(feature = "dict"))]
impl BuiltIn {
fn correct_with_dict(&self, _word: &str) -> Option<&'static [&'static str]> {
None
}
}

#[cfg(feature = "vars")]
#[cfg(feature = "vars")]
impl BuiltIn {
fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> {
let mut chained: Vec<_> = corrections
.iter()
Expand All @@ -84,12 +90,6 @@ impl BuiltIn {
Status::Corrections(chained)
}

#[cfg(not(feature = "vars"))]
fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> {
Status::Corrections(corrections.iter().map(|c| Cow::Borrowed(*c)).collect())
}

#[cfg(feature = "vars")]
fn correct_with_vars(&self, word: &str) -> Option<Status<'static>> {
if typos_vars::WORD_RANGE.contains(&word.len()) {
map_lookup(&typos_vars::VARS_DICTIONARY, word)
Expand All @@ -99,12 +99,6 @@ impl BuiltIn {
}
}

#[cfg(not(feature = "vars"))]
fn correct_with_vars(&self, _word: &str) -> Option<Status<'static>> {
None
}

#[cfg(feature = "vars")]
fn select_variant(
&self,
vars: &'static [(u8, &'static typos_vars::VariantsMap)],
Expand Down Expand Up @@ -148,6 +142,17 @@ impl BuiltIn {
}
}

#[cfg(not(feature = "vars"))]
impl BuiltIn {
fn chain_with_vars(&self, corrections: &'static [&'static str]) -> Status<'static> {
Status::Corrections(corrections.iter().map(|c| Cow::Borrowed(*c)).collect())
}

fn correct_with_vars(&self, _word: &str) -> Option<Status<'static>> {
None
}
}

impl typos::Dictionary for BuiltIn {
fn correct_ident<'s, 'w>(&'s self, ident: typos::tokens::Identifier<'w>) -> Option<Status<'s>> {
BuiltIn::correct_ident(self, ident)
Expand Down

0 comments on commit d65fa79

Please sign in to comment.