From f02ee8fff9bbcce83af7de8d88fa1459df67dc6e Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Wed, 3 Jun 2026 23:46:49 +0200 Subject: [PATCH] feat: minor refactor to clarify configs with env + initial thoughs on truth source model --- .env | 1 + Cargo.lock | 9 +++++++++ Cargo.toml | 3 +++ src/main.rs | 27 ++++++++++++++++++++++++--- src/modules/config/mod.rs | 21 ++++----------------- src/modules/mod.rs | 1 + src/modules/source/mod.rs | 11 +++++++++++ 7 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 .env create mode 100644 src/modules/source/mod.rs diff --git a/.env b/.env new file mode 100644 index 0000000..644bd22 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DEFAULT_DB_PATH=test diff --git a/Cargo.lock b/Cargo.lock index 0f4c9dd..5b9a963 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "wordsmith" version = "0.1.0" +dependencies = [ + "dotenv", +] diff --git a/Cargo.toml b/Cargo.toml index 1f8cda9..23c1a3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,3 +2,6 @@ name = "wordsmith" version = "0.1.0" edition = "2024" + +[dependencies] +dotenv = "0.15.0" diff --git a/src/main.rs b/src/main.rs index c117cf6..6465625 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,33 @@ -use crate::modules::config::read_config; +use std::*; mod modules; +use modules::config::{Config, read_config}; + +const DEFAULT_CONFIG_PATH: &'static str = "./src/assets/wordsmith_default.conf"; fn main() { + dotenv::dotenv().ok(); + let args: Vec = env::args().collect(); - let config = read_config(None); // TODO: read config from param + let default_config: Config = { + let db_path = { + let dp = env::var("DEFAULT_DB_PATH"); - println!("Hello, world!"); + if let Ok(dp) = dp { + dp + } else { + "~/.config/wordsmith/wordsmith.db".to_string() + } + }; + + Config { + database_path: db_path, + } + }; + + let config = read_config(DEFAULT_CONFIG_PATH, default_config); // TODO: read config from param + + println!("Hello, world! {:?}", config); // CREATE DB IF DOES NOT EXISTS } diff --git a/src/modules/config/mod.rs b/src/modules/config/mod.rs index 88c6993..a567c1c 100644 --- a/src/modules/config/mod.rs +++ b/src/modules/config/mod.rs @@ -1,27 +1,14 @@ use std::fs::read_to_string; -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Config { pub database_path: String, } -const DEFAULT_CONFIG_PATH: &'static str = "./src/assets/wordsmith_default.conf"; -const DEFAULT_DATABASE_PATH: &'static str = "~/.config/wordsmith/wordsmith.db"; +pub fn read_config(path: &str, default: Config) -> Config { + let mut config = default; -pub fn read_config(path: Option<&str>) -> Config { - let target_path: &str = { - if let Some(p) = path { - p - } else { - DEFAULT_CONFIG_PATH - } - }; - - let mut config = Config { - database_path: DEFAULT_DATABASE_PATH.to_string(), - }; - - for line in read_to_string(target_path).unwrap().lines() { + for line in read_to_string(path).unwrap().lines() { if let Some((key, value)) = line.split_once(" ") { match key { "database_path" => { diff --git a/src/modules/mod.rs b/src/modules/mod.rs index ef68c36..2ee385a 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1 +1,2 @@ pub mod config; +pub mod database; diff --git a/src/modules/source/mod.rs b/src/modules/source/mod.rs new file mode 100644 index 0000000..28b5008 --- /dev/null +++ b/src/modules/source/mod.rs @@ -0,0 +1,11 @@ +pub struct Database { + path: String, +} + +// TruthSource +pub trait WordSource { + fn find_word(&self, word: &str) -> Result<(), ()>; + fn insert_word(&self, word: &str) -> Result<(), ()>; + fn remove_word(&self, word: &str) -> Result<(), ()>; + fn get_all_words(&self) -> Vec; +}