diff --git a/src/assets/wordsmith_default.conf b/src/assets/wordsmith_default.conf index ef493e6..2364106 100644 --- a/src/assets/wordsmith_default.conf +++ b/src/assets/wordsmith_default.conf @@ -1,5 +1 @@ -database_source localhost -database_port 666 -database_user wordsmith -database_password wordsmith -database_name wordsmith +database_path localhost diff --git a/src/main.rs b/src/main.rs index 3f9689b..c117cf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,12 @@ -use std::{env, fs::read_to_string}; +use crate::modules::config::read_config; -const DEFAULT_CONFIG_PATH: &'static str = "./src/assets/wordsmith_default.conf"; +mod modules; fn main() { let args: Vec = env::args().collect(); + let config = read_config(None); // TODO: read config from param + println!("Hello, world!"); - - for line in read_to_string(DEFAULT_CONFIG_PATH).unwrap().lines() { - if let Some((key, value)) = line.split_once(" ") { - println!("Config {0} {1}", key, value); - } - } - // CREATE DB IF DOES NOT EXISTS } diff --git a/src/modules/config/mod.rs b/src/modules/config/mod.rs new file mode 100644 index 0000000..88c6993 --- /dev/null +++ b/src/modules/config/mod.rs @@ -0,0 +1,36 @@ +use std::fs::read_to_string; + +#[derive(Clone)] +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: 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() { + if let Some((key, value)) = line.split_once(" ") { + match key { + "database_path" => { + config.database_path = value.to_string(); + } + _ => {} + } + } + } + + config +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs new file mode 100644 index 0000000..ef68c36 --- /dev/null +++ b/src/modules/mod.rs @@ -0,0 +1 @@ +pub mod config;