Skip to content

Commit

Permalink
fixed config path
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-ciszewski committed May 4, 2024
1 parent 2a79c8a commit 2e778d1
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use std::fs;
use log::debug;
use std::{env, fs};

use serde::{Deserialize, Serialize};
use simplelog::info;
use toml::{value::Table, Value};

const CONFIG_PATH: &str = "config.toml";
const CONFIG_FILENAME: &str = "config.toml";

static mut CONFIG_PATH: String = String::new();
pub trait Config<'de>: Serialize + Deserialize<'de> + Default {}

pub fn get_config<'de, T: Config<'de>>(key: &str) -> T {
ensure_file_exists();

let config: Value = toml::from_str(&fs::read_to_string(CONFIG_PATH).unwrap()).unwrap();
let config: Value = toml::from_str(&fs::read_to_string(get_config_path()).unwrap()).unwrap();
let value = config.get(&key);

if value.is_none() {
Expand All @@ -24,14 +27,31 @@ pub fn update_config<'de, T: Config<'de>>(key: &str, config: &T) {
ensure_file_exists();

let mut whole_config: Table =
toml::from_str(&fs::read_to_string(CONFIG_PATH).unwrap()).unwrap();
toml::from_str(&fs::read_to_string(get_config_path()).unwrap()).unwrap();

whole_config.insert(key.to_string(), Value::try_from(config).unwrap());

save_config_file(&whole_config);
info!("Config updated");
}

fn get_config_path() -> String {
unsafe {
if CONFIG_PATH.is_empty() {
let mut path = env::current_exe()
.expect("Executable should have a path")
.parent()
.unwrap()
.to_path_buf();
path.push(CONFIG_FILENAME);
CONFIG_PATH = path.to_str().unwrap().to_string();
debug!("config path: \"{}\"", CONFIG_PATH);
}

CONFIG_PATH.clone()
}
}

fn initialize_default_config_key<'de, T: Config<'de>>(key: &str) -> T {
let def_config = T::default();

Expand All @@ -41,11 +61,11 @@ fn initialize_default_config_key<'de, T: Config<'de>>(key: &str) -> T {
}

fn save_config_file<T: ?Sized + Serialize>(contents: &T) {
fs::write(CONFIG_PATH, toml::to_string(&contents).unwrap()).unwrap();
fs::write(get_config_path(), toml::to_string(&contents).unwrap()).unwrap();
}

fn ensure_file_exists() {
if fs::metadata(CONFIG_PATH).is_err() {
fs::write(CONFIG_PATH, "").unwrap();
if fs::metadata(get_config_path()).is_err() {
fs::write(get_config_path(), "").unwrap();
}
}

0 comments on commit 2e778d1

Please sign in to comment.