WIP: basic wordlist rendering
This commit is contained in:
@@ -47,13 +47,13 @@ impl FileDatabase {
|
||||
}
|
||||
|
||||
pub trait WordSource {
|
||||
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 get_all_words(&self) -> Vec<Word>;
|
||||
fn find_by_word(&self, word: &str) -> Vec<Word>;
|
||||
fn find_by_lang(&self, lang: &str) -> Vec<Word>;
|
||||
fn find_by_word_and_lang(&self, word: &str, lang: &str) -> Vec<Word>;
|
||||
fn insert_word(&self, word: Word) -> Result<(), ()>;
|
||||
fn remove_word(&self, word_key: &str) -> Result<(), ()>;
|
||||
fn edit_word(&self, word: Word) -> Result<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_lang_key(word_key: &str) -> String;
|
||||
}
|
||||
@@ -67,7 +67,31 @@ fn fuzzy_compare(a: &str, b: &str) -> bool {
|
||||
}
|
||||
|
||||
impl WordSource for FileDatabase {
|
||||
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()> {
|
||||
fn get_all_words(&self) -> Vec<Word> {
|
||||
let statement = self
|
||||
.connection
|
||||
.prepare("select key, description, lang from word");
|
||||
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([]).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 find_by_word(&self, word: &str) -> Vec<Word> {
|
||||
let statement = self
|
||||
.connection
|
||||
.prepare("SELECT key, description, lang FROM word");
|
||||
@@ -87,13 +111,35 @@ impl WordSource for FileDatabase {
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(());
|
||||
}
|
||||
Ok(word_list)
|
||||
word_list
|
||||
}
|
||||
|
||||
fn find_by_word_and_lang(&self, word: &str, lang: &str) -> Result<Vec<Word>, ()> {
|
||||
fn find_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 find_by_word_and_lang(&self, word: &str, lang: &str) -> Vec<Word> {
|
||||
let statement = self
|
||||
.connection
|
||||
.prepare("SELECT key, description, lang FROM word where lang = ?1");
|
||||
@@ -113,10 +159,8 @@ impl WordSource for FileDatabase {
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(());
|
||||
}
|
||||
Ok(word_list)
|
||||
word_list
|
||||
}
|
||||
|
||||
fn insert_word(&self, word: Word) -> Result<(), ()> {
|
||||
@@ -170,54 +214,6 @@ impl WordSource for FileDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_all_words(&self) -> Vec<Word> {
|
||||
let statement = self
|
||||
.connection
|
||||
.prepare("select key, description, lang from word");
|
||||
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([]).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 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 {
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user