diff --git a/src/main.rs b/src/main.rs index 3284806..4bb1463 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,12 +23,28 @@ mod repository; fn main() { match args::parse() { Ok(x) => match x { + actions::Action::AdjectiveList => { + show_words(&repository::WordList::find_all_adjectives().unwrap()); + } actions::Action::AdjectiveAdd(word) => { repository::WordList::insert_adjective(&word).unwrap() } + actions::Action::MetalList => { + show_words(&repository::WordList::find_all_metals().unwrap()) + } actions::Action::MetalAdd(word) => repository::WordList::insert_metal(&word).unwrap(), _ => unimplemented!(), }, Err(x) => println!("Error {:?}", x), } } + +fn show_words(list: &repository::WordStorage) { + let blank = String::from(" "); + for (first_letter, word_list) in list { + let heading = first_letter.to_uppercase(); + for (pos, word) in word_list.iter().enumerate() { + println!("{} {}", if pos == 0 { &heading } else { &blank }, word); + } + } +} diff --git a/src/repository.rs b/src/repository.rs index 4683b6c..dcf3438 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -7,7 +7,7 @@ use serde_derive::Deserialize; use serde_derive::Serialize; use toml; -type WordStorage = BTreeMap>; +pub type WordStorage = BTreeMap>; #[derive(Debug, Serialize, Deserialize)] pub struct WordList { @@ -70,7 +70,19 @@ impl WordList { Ok(()) } - /// add an adjective to the word list + /// Get the list of all adjectives + pub fn find_all_adjectives() -> Result { + let repo = Self::load()?; + Ok(repo.adjectives) + } + + /// Get the list of all metals + pub fn find_all_metals() -> Result { + let repo = Self::load()?; + Ok(repo.metals) + } + + /// Add an adjective to the word list pub fn insert_adjective(adjective: &str) -> Result<(), WordListError> { let mut repo = Self::load()?; Self::insert_word(adjective, &mut repo.adjectives)?; @@ -91,7 +103,8 @@ impl WordList { .chars() .nth(0) .ok_or(WordListError::InvalidWord)? - .to_string(); + .to_string() + .to_lowercase(); let mut list = if !target.contains_key(&initial) { let empty_list = LinkedList::new(); empty_list @@ -102,7 +115,7 @@ impl WordList { if list.contains(&word.to_string()) { Err(WordListError::WordAlreadyExists) } else { - list.push_back(word.to_string()); + list.push_back(word.to_string().to_lowercase()); target.insert(initial, list); Ok(()) }