refactor: minor change on source structure
This commit is contained in:
@@ -35,7 +35,9 @@ fn main() {
|
|||||||
// TODO: CREATE FILE.db IF DOES NOT EXISTS
|
// TODO: CREATE FILE.db IF DOES NOT EXISTS
|
||||||
// TODO: CREATE TABLE IF DOES NOT EXISTS
|
// TODO: CREATE TABLE IF DOES NOT EXISTS
|
||||||
let connection = Connection::open(&config.database_path).unwrap();
|
let connection = Connection::open(&config.database_path).unwrap();
|
||||||
let word_source = FileDatabase {};
|
let word_source = FileDatabase {
|
||||||
|
connection: &connection,
|
||||||
|
};
|
||||||
|
|
||||||
// NOTE: testing init
|
// NOTE: testing init
|
||||||
// connection
|
// connection
|
||||||
@@ -73,7 +75,7 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
if let Ok(w_list) = word_source.find_by_word("test", &connection) {
|
if let Ok(w_list) = word_source.find_by_word("test") {
|
||||||
println!("Words! {:?}", w_list);
|
println!("Words! {:?}", w_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ use rusqlite::Connection;
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Word(String, String);
|
pub struct Word(String, String);
|
||||||
|
|
||||||
pub struct FileDatabase {}
|
pub struct FileDatabase<'a> {
|
||||||
|
pub connection: &'a Connection,
|
||||||
|
}
|
||||||
|
|
||||||
pub trait WordSource {
|
pub trait WordSource {
|
||||||
fn find_by_word(&self, word: &str, connection: &Connection) -> Result<Vec<Word>, ()>;
|
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()>;
|
||||||
fn insert_word(&mut self, word: Word) -> Result<(), ()>;
|
fn insert_word(&mut self, word: Word) -> Result<(), ()>;
|
||||||
fn remove_word(&self, word: &str) -> Result<(), ()>;
|
fn remove_word(&self, word: &str) -> Result<(), ()>;
|
||||||
fn edit_word(&mut self, word: Word) -> Result<(), ()>;
|
fn edit_word(&mut self, word: Word) -> Result<(), ()>;
|
||||||
@@ -20,9 +22,9 @@ fn fuzzy_compare(a: &str, b: &str) -> bool {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WordSource for FileDatabase {
|
impl<'a> WordSource for FileDatabase<'a> {
|
||||||
fn find_by_word(&self, word: &str, connection: &Connection) -> Result<Vec<Word>, ()> {
|
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()> {
|
||||||
let statement = connection.prepare("select key, description from word");
|
let statement = self.connection.prepare("select key, description from word");
|
||||||
let mut word_list: Vec<Word> = vec![];
|
let mut word_list: Vec<Word> = vec![];
|
||||||
|
|
||||||
if let Ok(mut query) = statement {
|
if let Ok(mut query) = statement {
|
||||||
|
|||||||
Reference in New Issue
Block a user