From 1b0bc7b046ae574da71108a06c75623ce7a2e28c Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Fri, 23 Oct 2020 13:07:30 -0300 Subject: [PATCH] Changed the list of words to set of words ... and now we don't need to check if the word is duplicated, and this generates on less error, and that means one less error to manage. WE WIN! --- src/repository.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/repository.rs b/src/repository.rs index dcf3438..fdbecef 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -1,5 +1,5 @@ use std::collections::BTreeMap; -use std::collections::LinkedList; +use std::collections::BTreeSet; use std::fs::File; use std::io::{Read, Write}; @@ -7,7 +7,7 @@ use serde_derive::Deserialize; use serde_derive::Serialize; use toml; -pub type WordStorage = BTreeMap>; +pub type WordStorage = BTreeMap>; #[derive(Debug, Serialize, Deserialize)] pub struct WordList { @@ -19,7 +19,6 @@ pub struct WordList { pub enum WordListError { InvalidFormat, InvalidWord, - WordAlreadyExists, SaveFailure, } @@ -106,18 +105,14 @@ impl WordList { .to_string() .to_lowercase(); let mut list = if !target.contains_key(&initial) { - let empty_list = LinkedList::new(); + let empty_list = BTreeSet::new(); empty_list } else { target.get(&initial).unwrap().to_owned() }; - if list.contains(&word.to_string()) { - Err(WordListError::WordAlreadyExists) - } else { - list.push_back(word.to_string().to_lowercase()); - target.insert(initial, list); - Ok(()) - } + list.insert(word.to_string().to_lowercase()); + target.insert(initial, list); + Ok(()) } }