feat: final DB and trait structure
This commit is contained in:
47
src/main.rs
47
src/main.rs
@@ -34,52 +34,7 @@ fn main() {
|
|||||||
|
|
||||||
match word_source_result {
|
match word_source_result {
|
||||||
Ok(word_source) => {
|
Ok(word_source) => {
|
||||||
// NOTE: test initial state
|
// TODO: interface and loop
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(word_source_error) => {
|
Err(word_source_error) => {
|
||||||
println!("!! {0}", word_source_error);
|
println!("!! {0}", word_source_error);
|
||||||
|
|||||||
@@ -48,10 +48,12 @@ impl FileDatabase {
|
|||||||
|
|
||||||
pub trait WordSource {
|
pub trait WordSource {
|
||||||
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()>;
|
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()>;
|
||||||
|
fn find_by_word_and_lang(&self, word: &str, lang: &str) -> Result<Vec<Word>, ()>;
|
||||||
fn insert_word(&self, word: Word) -> Result<(), ()>;
|
fn insert_word(&self, word: Word) -> Result<(), ()>;
|
||||||
fn remove_word(&self, word_key: &str) -> Result<(), ()>;
|
fn remove_word(&self, word_key: &str) -> Result<(), ()>;
|
||||||
fn edit_word(&self, word: Word) -> Result<Word, ()>;
|
fn edit_word(&self, word: Word) -> Result<Word, ()>;
|
||||||
fn get_all_words(&self) -> Vec<Word>;
|
fn get_all_words(&self) -> Vec<Word>;
|
||||||
|
fn get_all_words_by_lang(&self, lang: &str) -> Vec<Word>;
|
||||||
fn normalize_word_key(word_key: &str) -> String;
|
fn normalize_word_key(word_key: &str) -> String;
|
||||||
fn normalize_lang_key(word_key: &str) -> String;
|
fn normalize_lang_key(word_key: &str) -> String;
|
||||||
}
|
}
|
||||||
@@ -91,6 +93,32 @@ impl WordSource for FileDatabase {
|
|||||||
Ok(word_list)
|
Ok(word_list)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_by_word_and_lang(&self, word: &str, lang: &str) -> Result<Vec<Word>, ()> {
|
||||||
|
let statement = self
|
||||||
|
.connection
|
||||||
|
.prepare("SELECT key, description, lang FROM word where lang = ?1");
|
||||||
|
let mut word_list: Vec<Word> = 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<(), ()> {
|
fn insert_word(&self, word: Word) -> Result<(), ()> {
|
||||||
let result = self.connection.execute(
|
let result = self.connection.execute(
|
||||||
"INSERT INTO word (key, description, lang) VALUES (?1, ?2, ?3)",
|
"INSERT INTO word (key, description, lang) VALUES (?1, ?2, ?3)",
|
||||||
@@ -166,6 +194,30 @@ impl WordSource for FileDatabase {
|
|||||||
word_list
|
word_list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_all_words_by_lang(&self, lang: &str) -> Vec<Word> {
|
||||||
|
let statement = self
|
||||||
|
.connection
|
||||||
|
.prepare("select key, description, lang from word where lang = ?1");
|
||||||
|
let mut word_list: Vec<Word> = 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 {
|
fn normalize_word_key(word_key: &str) -> String {
|
||||||
// TODO: real normalization, not just lowercase, but keep in mind it must be kept as the
|
// 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
|
// final and real word -> Maybe full refactor to include a word id
|
||||||
|
|||||||
Reference in New Issue
Block a user