feat: config module implemented
This commit is contained in:
36
src/modules/config/mod.rs
Normal file
36
src/modules/config/mod.rs
Normal file
@@ -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
|
||||
}
|
||||
1
src/modules/mod.rs
Normal file
1
src/modules/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod config;
|
||||
Reference in New Issue
Block a user