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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
mod dictionary; pub struct Dictionary { internal: dictionary::Trie, } impl Default for Dictionary { fn default() -> Self { Dictionary::new() } } impl Dictionary { pub fn new() -> Dictionary { Dictionary { internal: dictionary::Trie::new(), } } pub fn insert(&mut self, word: String) { self.internal.insert(word) } pub fn find_words_based_on_prefix(&mut self, prefix: String) -> Option<Vec<String>> { self.internal.find_words_based_on_prefix(prefix) } pub fn auto_correct(&mut self, word: String) -> Option<Vec<String>> { self.internal.auto_suggest(word) } } #[cfg(test)] mod tests { use super::*; #[test] fn should_provide_full_words_if_word_based_on_prefix() { let mut dictionary = Dictionary::new(); let word1 = "Dog".to_string(); let word2 = "Dogecoin".to_string(); dictionary.insert(word1); dictionary.insert(word2); let words_available = dictionary .find_words_based_on_prefix("Dog".to_string()) .expect("Words should be present"); assert!(words_available.contains(&"Dog".to_string())); assert!(words_available.contains(&"Dogecoin".to_string())) } #[test] fn should_auto_correct_if_word_provided_isnt_available() { let mut dictionary = Dictionary::new(); let word1 = "Dog".to_string(); let word2 = "Dogecoin".to_string(); dictionary.insert(word1); dictionary.insert(word2); let words_available = dictionary .auto_correct("Dogecoins".to_string()) .expect("Suggestions should be available"); assert_eq!(vec!["Dogecoin".to_string()], words_available) } #[test] fn should_return_none_if_word_provided_isnt_available_and_there_are_no_matching_words() { let mut dictionary = Dictionary::new(); let word1 = "Dog".to_string(); let word2 = "Dogecoin".to_string(); dictionary.insert(word1); dictionary.insert(word2); let words_available = dictionary.auto_correct("Cat".to_string()); assert_eq!(words_available, None) } }