feat: minor refactor to clarify configs with env + initial thoughs on truth source model

This commit is contained in:
2026-06-03 23:46:49 +02:00
parent cc22d3b719
commit f02ee8fff9
7 changed files with 53 additions and 20 deletions

1
.env Normal file
View File

@@ -0,0 +1 @@
DEFAULT_DB_PATH=test

9
Cargo.lock generated
View File

@@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]] [[package]]
name = "wordsmith" name = "wordsmith"
version = "0.1.0" version = "0.1.0"
dependencies = [
"dotenv",
]

View File

@@ -2,3 +2,6 @@
name = "wordsmith" name = "wordsmith"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies]
dotenv = "0.15.0"

View File

@@ -1,12 +1,33 @@
use crate::modules::config::read_config; use std::*;
mod modules; mod modules;
use modules::config::{Config, read_config};
const DEFAULT_CONFIG_PATH: &'static str = "./src/assets/wordsmith_default.conf";
fn main() { fn main() {
dotenv::dotenv().ok();
let args: Vec<String> = env::args().collect(); let args: Vec<String> = 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 // CREATE DB IF DOES NOT EXISTS
} }

View File

@@ -1,27 +1,14 @@
use std::fs::read_to_string; use std::fs::read_to_string;
#[derive(Clone)] #[derive(Clone, Debug)]
pub struct Config { pub struct Config {
pub database_path: String, pub database_path: String,
} }
const DEFAULT_CONFIG_PATH: &'static str = "./src/assets/wordsmith_default.conf"; pub fn read_config(path: &str, default: Config) -> Config {
const DEFAULT_DATABASE_PATH: &'static str = "~/.config/wordsmith/wordsmith.db"; let mut config = default;
pub fn read_config(path: Option<&str>) -> Config { for line in read_to_string(path).unwrap().lines() {
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() {
if let Some((key, value)) = line.split_once(" ") { if let Some((key, value)) = line.split_once(" ") {
match key { match key {
"database_path" => { "database_path" => {

View File

@@ -1 +1,2 @@
pub mod config; pub mod config;
pub mod database;

11
src/modules/source/mod.rs Normal file
View File

@@ -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<String>;
}