diff --git a/.env b/.env index 644bd22..a396b77 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DEFAULT_DB_PATH=test +DEFAULT_DB_PATH=test.db diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 2ee385a..838bce4 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1,2 +1,2 @@ pub mod config; -pub mod database; +pub mod source; diff --git a/src/modules/source/mod.rs b/src/modules/source/mod.rs index 28b5008..70c4e9e 100644 --- a/src/modules/source/mod.rs +++ b/src/modules/source/mod.rs @@ -1,11 +1,60 @@ -pub struct Database { +#[derive(Clone, Copy)] +pub struct Word(String, String); + +pub struct FileDatabase { path: String, + cached_words: Option>, } // 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; + fn insert_word(&mut self, word: Word) -> Result<(), ()>; fn remove_word(&self, word: &str) -> Result<(), ()>; - fn get_all_words(&self) -> Vec; + fn edit_word(&mut self, word: Word) -> Result<(), ()>; + fn get_all_words(&self) -> Vec; +} + +impl WordSource for FileDatabase { + fn load(&self) -> Result<(), ()> {} + + fn find_word(&self, word: &str) -> Result { + 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 { + if let Some(words) = self.cached_words { + words + } else { + // Check db + () + } + } }