feat: basic db connection
This commit is contained in:
@@ -1,60 +1,59 @@
|
||||
#[derive(Clone, Copy)]
|
||||
use rusqlite::Connection;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Word(String, String);
|
||||
|
||||
pub struct FileDatabase {
|
||||
path: String,
|
||||
cached_words: Option<Vec<Word>>,
|
||||
}
|
||||
pub struct FileDatabase {}
|
||||
|
||||
// TruthSource
|
||||
pub trait WordSource {
|
||||
fn load(&self) -> Result<(), ()>;
|
||||
fn find_word(&self, word: &str) -> Result<Word, ()>;
|
||||
fn find_by_word(&self, word: &str, connection: &Connection) -> Result<Vec<Word>, ()>;
|
||||
fn insert_word(&mut self, word: Word) -> Result<(), ()>;
|
||||
fn remove_word(&self, word: &str) -> Result<(), ()>;
|
||||
fn edit_word(&mut self, word: Word) -> Result<(), ()>;
|
||||
fn get_all_words(&self) -> Vec<Word>;
|
||||
}
|
||||
|
||||
impl WordSource for FileDatabase {
|
||||
fn load(&self) -> Result<(), ()> {}
|
||||
fn fuzzy_compare(a: &str, b: &str) -> bool {
|
||||
if a.eq(b) {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
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());
|
||||
impl WordSource for FileDatabase {
|
||||
fn find_by_word(&self, word: &str, connection: &Connection) -> Result<Vec<Word>, ()> {
|
||||
let statement = connection.prepare("select key, description from word");
|
||||
let mut word_list: Vec<Word> = vec![];
|
||||
|
||||
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();
|
||||
if fuzzy_compare(word, &found_word) {
|
||||
let found_word_description: String = row.get(0).unwrap();
|
||||
word_list.push(Word(found_word, found_word_description));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(());
|
||||
}
|
||||
// TODO: implement db alternative access
|
||||
Err(())
|
||||
Ok(word_list)
|
||||
}
|
||||
|
||||
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 remove_word(&self, word: &str) -> Result<(), ()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_all_words(&self) -> Vec<Word> {
|
||||
if let Some(words) = self.cached_words {
|
||||
words
|
||||
} else {
|
||||
// Check db
|
||||
()
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user