WIP: word list interactions
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
pub mod config;
|
||||
pub mod database;
|
||||
pub mod source;
|
||||
|
||||
@@ -1,11 +1,60 @@
|
||||
pub struct Database {
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Word(String, String);
|
||||
|
||||
pub struct FileDatabase {
|
||||
path: String,
|
||||
cached_words: Option<Vec<Word>>,
|
||||
}
|
||||
|
||||
// TruthSource
|
||||
pub trait WordSource {
|
||||
fn find_word(&self, word: &str) -> Result<(), ()>;
|
||||
fn insert_word(&self, word: &str) -> Result<(), ()>;
|
||||
fn load(&self) -> Result<(), ()>;
|
||||
fn find_word(&self, word: &str) -> Result<Word, ()>;
|
||||
fn insert_word(&mut self, word: Word) -> Result<(), ()>;
|
||||
fn remove_word(&self, word: &str) -> Result<(), ()>;
|
||||
fn get_all_words(&self) -> Vec<String>;
|
||||
fn edit_word(&mut self, word: Word) -> Result<(), ()>;
|
||||
fn get_all_words(&self) -> Vec<Word>;
|
||||
}
|
||||
|
||||
impl WordSource for FileDatabase {
|
||||
fn load(&self) -> Result<(), ()> {}
|
||||
|
||||
fn find_word(&self, word: &str) -> Result<Word, ()> {
|
||||
if let Some(words) = &self.cached_words {
|
||||
if let Some(w) = words.into_iter().find(|&w| word == w.0) {
|
||||
return Ok(w.clone());
|
||||
}
|
||||
}
|
||||
// TODO: implement db alternative access
|
||||
Err(())
|
||||
}
|
||||
|
||||
fn insert_word(&mut self, word: Word) -> Result<(), ()> {
|
||||
self.cached_words.get_or_insert(vec![]).push(word);
|
||||
// TODO: insert into db
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn edit_word(&mut self, word: Word) -> Result<(), ()> {
|
||||
if let Some(w) = self
|
||||
.cached_words
|
||||
.get_or_insert(vec![])
|
||||
.iter()
|
||||
.find(|&w| w.0 == word.0)
|
||||
{
|
||||
// ...
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove_word(&self, word: &str) -> Result<(), ()> {}
|
||||
|
||||
fn get_all_words(&self) -> Vec<Word> {
|
||||
if let Some(words) = self.cached_words {
|
||||
words
|
||||
} else {
|
||||
// Check db
|
||||
()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user