From 11978af86329d64c0503473ea040320753d388ee Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Sun, 26 Jul 2026 12:23:07 +0200 Subject: [PATCH] feat: final DB and trait structure --- src/main.rs | 47 +---------------------------------- src/modules/source/mod.rs | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index a1697ec..cfb4a8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,52 +34,7 @@ fn main() { match word_source_result { Ok(word_source) => { - // NOTE: test initial state - println!(">> Test word init"); - if let Ok(w_list) = word_source.find_by_word("test") { - println!("Words! {:?}", w_list); - } - - // NOTE: test insert - println!(">> Test word insert"); - let _ = word_source.insert_word(Word { - key: "test".to_string(), - description: "This is a test".to_string(), - lang: "en".to_string(), - }); - let _ = word_source.insert_word(Word { - key: "test 2".to_string(), - description: "This is a test".to_string(), - lang: "es".to_string(), - }); - let _ = word_source.insert_word(Word { - key: "another test".to_string(), - description: "This is a test".to_string(), - lang: "es".to_string(), - }); - - if let Ok(w_list) = word_source.find_by_word("test") { - println!("Words! {:?}", w_list); - } - - // NOTE: test edit - println!(">> Test word edit"); - let _ = word_source.edit_word(Word { - key: "test".to_string(), - description: "This is a test".to_string(), - lang: "es".to_string(), - }); - if let Ok(w_list) = word_source.find_by_word("test") { - println!("Words! {:?}", w_list); - } - - // NOTE: test remove - println!(">> Test word remove"); - let _ = word_source.remove_word("test2"); - let _ = word_source.remove_word("test"); - if let Ok(w_list) = word_source.find_by_word("test") { - println!("Words! {:?}", w_list); - } + // TODO: interface and loop } Err(word_source_error) => { println!("!! {0}", word_source_error); diff --git a/src/modules/source/mod.rs b/src/modules/source/mod.rs index 35494a6..007d3ae 100644 --- a/src/modules/source/mod.rs +++ b/src/modules/source/mod.rs @@ -48,10 +48,12 @@ impl FileDatabase { pub trait WordSource { fn find_by_word(&self, word: &str) -> Result, ()>; + fn find_by_word_and_lang(&self, word: &str, lang: &str) -> Result, ()>; fn insert_word(&self, word: Word) -> Result<(), ()>; fn remove_word(&self, word_key: &str) -> Result<(), ()>; fn edit_word(&self, word: Word) -> Result; fn get_all_words(&self) -> Vec; + fn get_all_words_by_lang(&self, lang: &str) -> Vec; fn normalize_word_key(word_key: &str) -> String; fn normalize_lang_key(word_key: &str) -> String; } @@ -91,6 +93,32 @@ impl WordSource for FileDatabase { Ok(word_list) } + fn find_by_word_and_lang(&self, word: &str, lang: &str) -> Result, ()> { + let statement = self + .connection + .prepare("SELECT key, description, lang FROM word where lang = ?1"); + let mut word_list: Vec = vec![]; + + if let Ok(mut query) = statement { + let mut rows = query.query([lang]).unwrap(); + while let Some(row) = rows.next().unwrap() { + let found_word: String = row.get(0).unwrap(); + if fuzzy_compare(word, &found_word) { + let found_word_description: String = row.get(1).unwrap(); + let found_word_lang: String = row.get(2).unwrap(); + word_list.push(Word { + key: found_word, + description: found_word_description, + lang: found_word_lang, + }); + } + } + } else { + return Err(()); + } + Ok(word_list) + } + fn insert_word(&self, word: Word) -> Result<(), ()> { let result = self.connection.execute( "INSERT INTO word (key, description, lang) VALUES (?1, ?2, ?3)", @@ -166,6 +194,30 @@ impl WordSource for FileDatabase { word_list } + fn get_all_words_by_lang(&self, lang: &str) -> Vec { + let statement = self + .connection + .prepare("select key, description, lang from word where lang = ?1"); + let mut word_list: Vec = vec![]; + + // TODO: make this pretty with functional programming + if let Ok(mut query) = statement { + let mut rows = query.query([lang]).unwrap(); + while let Some(row) = rows.next().unwrap() { + let found_word: String = row.get(0).unwrap(); + let found_word_description: String = row.get(1).unwrap(); + let found_word_lang: String = row.get(2).unwrap(); + word_list.push(Word { + key: found_word, + description: found_word_description, + lang: found_word_lang, + }); + } + } + + word_list + } + fn normalize_word_key(word_key: &str) -> String { // TODO: real normalization, not just lowercase, but keep in mind it must be kept as the // final and real word -> Maybe full refactor to include a word id