Browse Source

Listing words

master
Julio Biason 4 years ago
parent
commit
986500da48
  1. 16
      src/main.rs
  2. 21
      src/repository.rs

16
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);
}
}
}

21
src/repository.rs

@ -7,7 +7,7 @@ use serde_derive::Deserialize;
use serde_derive::Serialize;
use toml;
type WordStorage = BTreeMap<String, LinkedList<String>>;
pub type WordStorage = BTreeMap<String, LinkedList<String>>;
#[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<WordStorage, WordListError> {
let repo = Self::load()?;
Ok(repo.adjectives)
}
/// Get the list of all metals
pub fn find_all_metals() -> Result<WordStorage, WordListError> {
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(())
}

Loading…
Cancel
Save