diff --git a/.env b/.env index a396b77..9fddfb2 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DEFAULT_DB_PATH=test.db +DEFAULT_DB_PATH=~/.config/wordsmith/ws.db diff --git a/Cargo.lock b/Cargo.lock index 5b9a963..f171680 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,288 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "bitflags" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8" + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "cc" +version = "1.2.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17dd265a7d0f31ef544e1b20e03add05d3b45b491b633b10d67145d2acc1a38" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + [[package]] name = "dotenv" version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashlink" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32069d97bb81e38fa67eab65e3393bf804bb85969f2bc06bf13f64aef5aba248" +dependencies = [ + "hashbrown 0.17.1", +] + +[[package]] +name = "js-sys" +version = "0.3.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + +[[package]] +name = "libsqlite3-sys" +version = "0.38.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6c19a05435c21ac299d71b6a9c13db3e3f47c520517d58990a462a1397a61db" +dependencies = [ + "pkg-config", + "vcpkg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "pkg-config" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rsqlite-vfs" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51c9ae4df8a7fba42103df5c621fa3c37eccf3a3c650879e90fc48b11cc192c" +dependencies = [ + "hashbrown 0.16.1", + "thiserror", +] + +[[package]] +name = "rusqlite" +version = "0.40.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11438310b19e3109b6446c33d1ed5e889428cf2e278407bc7896bc4aaea43323" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", + "sqlite-wasm-rs", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "smallvec" +version = "1.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" + +[[package]] +name = "sqlite-wasm-rs" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc3efc0da82635d7e1ced0053bbbfa8c7ab9645d0bf36ceb4f7127bb85315d75" +dependencies = [ + "cc", + "js-sys", + "rsqlite-vfs", + "wasm-bindgen", +] + +[[package]] +name = "syn" +version = "2.0.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "wasm-bindgen" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" +dependencies = [ + "unicode-ident", +] + [[package]] name = "wordsmith" version = "0.1.0" dependencies = [ "dotenv", + "rusqlite", ] diff --git a/Cargo.toml b/Cargo.toml index 23c1a3a..f659c97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] dotenv = "0.15.0" +rusqlite = "0.40.1" diff --git a/src/assets/wordsmith_default.conf b/src/assets/wordsmith_default.conf index 2364106..bc9ffc8 100644 --- a/src/assets/wordsmith_default.conf +++ b/src/assets/wordsmith_default.conf @@ -1 +1 @@ -database_path localhost +database_path /home/dqnid/.config/wordsmith/ws.db diff --git a/src/main.rs b/src/main.rs index 6465625..6973198 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,12 @@ use std::*; +use rusqlite::{Connection, OpenFlags}; + mod modules; use modules::config::{Config, read_config}; +use crate::modules::source::{FileDatabase, WordSource}; + const DEFAULT_CONFIG_PATH: &'static str = "./src/assets/wordsmith_default.conf"; fn main() { @@ -28,6 +32,50 @@ fn main() { let config = read_config(DEFAULT_CONFIG_PATH, default_config); // TODO: read config from param + // TODO: CREATE FILE.db IF DOES NOT EXISTS + // TODO: CREATE TABLE IF DOES NOT EXISTS + let connection = Connection::open(&config.database_path).unwrap(); + let word_source = FileDatabase {}; + + // NOTE: testing init + // connection + // .execute( + // "CREATE TABLE word ( + // id INT AUTO_INCREMENT PRIMARY KEY, + // key TEXT NOT NULL, + // description TEXT NOT NULL + // )", + // (), // empty list of parameters. + // ) + // .unwrap(); + connection + .execute( + "INSERT INTO word (key, description) VALUES (?1, ?2)", + ("test", "a description"), + ) + .unwrap(); + connection + .execute( + "INSERT INTO word (key, description) VALUES (?1, ?2)", + ("another", "a description"), + ) + .unwrap(); + connection + .execute( + "INSERT INTO word (key, description) VALUES (?1, ?2)", + ("test yet", "a description"), + ) + .unwrap(); + connection + .execute( + "INSERT INTO word (key, description) VALUES (?1, ?2)", + ("yes yes", "a description"), + ) + .unwrap(); + + if let Ok(w_list) = word_source.find_by_word("test", &connection) { + println!("Words! {:?}", w_list); + } + println!("Hello, world! {:?}", config); - // CREATE DB IF DOES NOT EXISTS } diff --git a/src/modules/source/mod.rs b/src/modules/source/mod.rs index 70c4e9e..11e7943 100644 --- a/src/modules/source/mod.rs +++ b/src/modules/source/mod.rs @@ -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>, -} +pub struct FileDatabase {} -// TruthSource pub trait WordSource { - fn load(&self) -> Result<(), ()>; - fn find_word(&self, word: &str) -> Result; + fn find_by_word(&self, word: &str, connection: &Connection) -> Result, ()>; 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; } -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 { - 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, ()> { + let statement = connection.prepare("select key, description from word"); + let mut word_list: Vec = 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 { - if let Some(words) = self.cached_words { - words - } else { - // Check db - () - } + vec![] } }