1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pub const SLASH: f32 = 0.9;
pub const WORD: f32 = 0.85;
pub const DOT: f32 = 0.6;
pub const CAPITAL: f32 = 0.7;

pub fn compute(choice_chars: &[char]) -> Vec<f32> {
    let mut last_char = '/';

    choice_chars
        .iter()
        .map(|&cchar| {
            let bonus = for_char(last_char, cchar);
            last_char = cchar;
            bonus
        })
        .collect()
}

fn for_char(prev: char, current: char) -> f32 {
    match current {
        'a'..='z' | '0'..='9' => for_previous(prev),
        'A'..='Z' => match prev {
            'a'..='z' => CAPITAL,
            _ => for_previous(prev),
        },
        _ => 0.0,
    }
}

fn for_previous(ch: char) -> f32 {
    match ch {
        '/' => SLASH,
        '-' | '_' | ' ' => WORD,
        '.' => DOT,
        _ => 0.0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compute() {
        assert_eq!(
            compute(&"a/b/c/d".chars().collect::<Vec<char>>()),
            vec![0.9, 0.0, 0.9, 0.0, 0.9, 0.0, 0.9]
        );
        assert_eq!(
            compute(&"aTestString".chars().collect::<Vec<char>>()),
            vec![0.9, 0.7, 0.0, 0.0, 0.0, 0.7, 0.0, 0.0, 0.0, 0.0, 0.0]
        );
    }
}