Skip to content

Commit

Permalink
feat(config): implement config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
QaidVoid committed Oct 3, 2024
1 parent 11f6214 commit abbaaf6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/core/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::{env, fs, path::PathBuf, sync::LazyLock};

use serde::{Deserialize, Serialize};

/// Application's configuration
#[derive(Deserialize, Serialize)]
pub struct Config {
/// Path to the directory where app data is stored.
pub soar_path: String,
}

impl Config {
/// Creates a new configuration by loading it from the configuration file.
/// If the configuration file is not found, it generates a new default configuration.
pub fn new() -> Self {
let home_config = env::var("XDG_CONFIG_HOME").unwrap_or_else(|_| {
env::var("HOME").map_or_else(
|_| panic!("Failed to retrieve HOME environment variable"),
|home| format!("{}/.config", home),
)
});
let pkg_config = PathBuf::from(home_config).join(env!("CARGO_PKG_NAME"));
let config_path = pkg_config.join("config.json");
let content = match fs::read(&config_path) {
Ok(content) => content,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
fs::create_dir_all(pkg_config).unwrap();
Config::generate(config_path)
}
Err(e) => {
panic!("Error reading config file: {:?}", e);
}
};
serde_json::from_slice(&content)
.unwrap_or_else(|e| panic!("Failed to parse config file: {:?}", e))
}

fn generate(config_path: PathBuf) -> Vec<u8> {
let def_config = Self {
soar_path: "$HOME/.soar".to_string(),
};
let serialized = serde_json::to_vec_pretty(&def_config).unwrap();
fs::write(config_path, &serialized).unwrap();
serialized
}
}

impl Default for Config {
fn default() -> Self {
Self::new()
}
}

pub static CONFIG: LazyLock<Config> = LazyLock::new(Config::default);
1 change: 1 addition & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod config;

0 comments on commit abbaaf6

Please sign in to comment.