You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
4 years ago
|
use std::collections::BTreeMap;
|
||
|
use std::collections::LinkedList;
|
||
|
use std::fs::File;
|
||
|
use std::io::Read;
|
||
|
|
||
|
use serde_derive::Deserialize;
|
||
|
use serde_derive::Serialize;
|
||
|
use toml;
|
||
|
|
||
|
type WordStorage = BTreeMap<char, LinkedList<String>>;
|
||
|
|
||
|
#[derive(Debug, Serialize, Deserialize)]
|
||
4 years ago
|
pub struct WordList {
|
||
4 years ago
|
adjectives: WordStorage,
|
||
|
metals: WordStorage,
|
||
|
}
|
||
|
|
||
4 years ago
|
pub enum WordListError {
|
||
4 years ago
|
InvalidFormat,
|
||
|
}
|
||
|
|
||
4 years ago
|
impl std::convert::From<std::io::Error> for WordListError {
|
||
|
fn from(_error: std::io::Error) -> WordListError {
|
||
|
WordListError::InvalidFormat
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
impl std::convert::From<toml::de::Error> for WordListError {
|
||
|
fn from(_error: toml::de::Error) -> WordListError {
|
||
|
WordListError::InvalidFormat
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
impl WordList {
|
||
4 years ago
|
/// Create an empty database
|
||
|
fn empty() -> Self {
|
||
4 years ago
|
WordList {
|
||
4 years ago
|
adjectives: WordStorage::new(),
|
||
|
metals: WordStorage::new(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Load the database
|
||
4 years ago
|
pub fn load() -> Result<Self, WordListError> {
|
||
4 years ago
|
if let Ok(mut fp) = File::open("database.toml") {
|
||
|
let mut content = String::new();
|
||
|
fp.read_to_string(&mut content)?;
|
||
|
let data = toml::from_str(&content)?;
|
||
|
Ok(data)
|
||
|
} else {
|
||
4 years ago
|
Ok(WordList::empty())
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|