use once_cell::sync::OnceCell; use levenshtein_automata::{ LevenshteinAutomatonBuilder as LevBuilder, DFA, }; static LEVDIST0: OnceCell = OnceCell::new(); static LEVDIST1: OnceCell = OnceCell::new(); static LEVDIST2: OnceCell = OnceCell::new(); #[derive(Copy, Clone)] enum PrefixSetting { Prefix, NoPrefix, } fn build_dfa_with_setting(query: &str, setting: PrefixSetting) -> DFA { use PrefixSetting::{Prefix, NoPrefix}; match query.len() { 0 ..= 4 => { let builder = LEVDIST0.get_or_init(|| LevBuilder::new(0, false)); match setting { Prefix => builder.build_prefix_dfa(query), NoPrefix => builder.build_dfa(query), } }, 5 ..= 8 => { let builder = LEVDIST1.get_or_init(|| LevBuilder::new(1, false)); match setting { Prefix => builder.build_prefix_dfa(query), NoPrefix => builder.build_dfa(query), } }, _ => { let builder = LEVDIST2.get_or_init(|| LevBuilder::new(2, false)); match setting { Prefix => builder.build_prefix_dfa(query), NoPrefix => builder.build_dfa(query), } }, } } pub fn build_prefix_dfa(query: &str) -> DFA { build_dfa_with_setting(query, PrefixSetting::Prefix) } pub fn build_dfa(query: &str) -> DFA { build_dfa_with_setting(query, PrefixSetting::NoPrefix) }