feat: minor refactor to clarify configs with env + initial thoughs on truth source model
This commit is contained in:
9
Cargo.lock
generated
9
Cargo.lock
generated
@@ -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",
|
||||
]
|
||||
|
||||
@@ -2,3 +2,6 @@
|
||||
name = "wordsmith"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
dotenv = "0.15.0"
|
||||
|
||||
27
src/main.rs
27
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<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
|
||||
}
|
||||
|
||||
@@ -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" => {
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod config;
|
||||
pub mod database;
|
||||
|
||||
11
src/modules/source/mod.rs
Normal file
11
src/modules/source/mod.rs
Normal 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>;
|
||||
}
|
||||
Reference in New Issue
Block a user