Skip to content

Commit

Permalink
fixed conditional compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kernelPanic0x committed Oct 10, 2024
1 parent 3ccb0f4 commit afa5090
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/core/wordlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ impl Wordlist {
let (prefix_without_last, last_partial) = prefix.rsplit_once('-').unwrap_or(("", prefix));

#[cfg(feature = "fuzzy-complete")]
let matches = self.fuzzy_complete(last_partial, words);
let matches: Vec<String> = self.fuzzy_complete(last_partial, words);

#[cfg(not(feature = "fuzzy-complete"))]
let matches = words
.iter()
.filter(|word| word.starts_with(last_partial))
.cloned()
.collect();
let matches: Vec<String> = self.normal_complete(last_partial, words);

matches
.into_iter()
Expand All @@ -68,7 +64,7 @@ impl Wordlist {

/// Fuzzy wormhole code completion
#[cfg(feature = "fuzzy-complete")]
fn fuzzy_complete(&self, partial: &str, words: &[String]) -> Vec<String> {
pub fn fuzzy_complete(&self, partial: &str, words: &[String]) -> Vec<String> {
// We use Jaro-Winkler algorithm because it emphasizes the beginning of a word
use fuzzt::algorithms::JaroWinkler;

Expand All @@ -80,6 +76,14 @@ impl Wordlist {
.collect()
}

pub fn normal_complete(&self, partial: &str, words: &[String]) -> Vec<String> {
words
.iter()
.filter(|word| word.starts_with(partial))
.cloned()
.collect()
}

/// Choose wormhole code word
pub fn choose_words(&self) -> String {
let mut rng = OsRng;
Expand Down Expand Up @@ -230,18 +234,35 @@ mod test {
}

#[test]
#[cfg(not(feature = "fuzzy-complete"))]
fn test_wormhole_code_normal_completions() {
let list = default_wordlist(2);
#[cfg(feature = "fuzzy-complete")]
fn test_completion_fuzzy() {
let wl = default_wordlist(2);
let list = wl.get_wordlist("22-");

assert_eq!(list.get_completions("22"), Vec::<String>::new());
assert_eq!(list.get_completions("22-"), Vec::<String>::new());
assert_eq!(wl.fuzzy_complete("chck", list), ["checkup", "choking"]);
assert_eq!(wl.fuzzy_complete("checkp", list), ["checkup"]);
assert_eq!(
wl.fuzzy_complete("checkup", list),
["checkup", "lockup", "cleanup"]
);
}

// Invalid wormhole code check
assert_eq!(list.get_completions("tro"), Vec::<String>::new());
#[test]
fn test_completion_normal() {
let wl = default_wordlist(2);
let list = wl.get_wordlist("22-");

assert_eq!(list.get_completions("22-chisel"), ["22-chisel"]);
assert_eq!(wl.normal_complete("che", list), ["checkup"]);
}

#[test]
fn test_full_wormhole_completion() {
let wl = default_wordlist(2);

assert_eq!(list.get_completions("22-chisel-tob"), ["22-chisel-tobacco"]);
assert_eq!(wl.get_completions("22-chec").first().unwrap(), "22-checkup");
assert_eq!(
wl.get_completions("22-checkup-t").first().unwrap(),
"22-checkup-tobacco"
);
}
}

0 comments on commit afa5090

Please sign in to comment.