From 8d8cf4805e4ba6669d407ad2736ecb3cab290e05 Mon Sep 17 00:00:00 2001 From: objz Date: Sat, 5 Jul 2025 14:45:11 +0200 Subject: [PATCH] feat: add paths and defaults config with graceful fallback --- assets/default.toml | 8 +++++ src/config/mod.rs | 79 +++++++++++++++++++++++++++++++++++++-------- src/config/types.rs | 57 ++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 13 deletions(-) diff --git a/assets/default.toml b/assets/default.toml index caa83dc..3c55129 100644 --- a/assets/default.toml +++ b/assets/default.toml @@ -1,6 +1,14 @@ [general] debug = false +[paths] +instances_dir = "~/.local/share/mcl/instances" +# java_path is optional, leave commented out by default + +[defaults] +memory_min = "512M" +memory_max = "2G" + [colors] background = "#1e1e1e" foreground = "white" diff --git a/src/config/mod.rs b/src/config/mod.rs index da874be..7dc18b6 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -8,8 +8,13 @@ use types::Config; pub mod types; pub fn get_config_path() -> PathBuf { - let base_dir = config_dir().unwrap(); - base_dir.join("mcl/") + match config_dir() { + Some(base_dir) => base_dir.join("mcl/"), + None => { + tracing::error!("Could not determine config directory, falling back to ./config/"); + PathBuf::from("./config/") + } + } } fn ensure_config_exists(default_path: &str) -> PathBuf { @@ -17,27 +22,75 @@ fn ensure_config_exists(default_path: &str) -> PathBuf { if !config_path.exists() { if let Some(parent_dir) = config_path.parent() { - fs::create_dir_all(parent_dir).expect("Failed to create configuration directory"); + match fs::create_dir_all(parent_dir) { + Ok(_) => {} + Err(e) => { + tracing::error!("Failed to create configuration directory: {}", e); + } + } } - fs::copy(default_path, &config_path).expect("Failed to copy default configuration file"); - tracing::debug!( - "Default configuration copied to '{}'", - config_path.display() - ); + match fs::copy(default_path, &config_path) { + Ok(_) => { + tracing::debug!( + "Default configuration copied to '{}'", + config_path.display() + ); + } + Err(e) => { + tracing::error!( + "Failed to copy default config from '{}' to '{}': {}", + default_path, + config_path.display(), + e + ); + } + } } config_path } pub fn load_config(config_path: &PathBuf) -> Result { - ConfigLoader::builder() - .add_source(File::from(config_path.clone())) - .build()? - .try_deserialize() + let built = match ConfigLoader::builder() + .add_source(File::from(config_path.clone()).required(false)) + .build() + { + Ok(c) => c, + Err(e) => { + tracing::error!( + "Failed to build config from '{}': {}", + config_path.display(), + e + ); + return Err(e); + } + }; + + match built.try_deserialize() { + Ok(config) => Ok(config), + Err(e) => { + tracing::error!( + "Failed to deserialize config from '{}': {}", + config_path.display(), + e + ); + Err(e) + } + } } pub static SETTINGS: Lazy = Lazy::new(|| { let path: PathBuf = ensure_config_exists("assets/default.toml"); - load_config(&path).expect("Failed to load configuration") + match load_config(&path) { + Ok(config) => config, + Err(e) => { + tracing::error!("Config load failed, using defaults: {}", e); + Config { + general: types::General::default(), + paths: types::Paths::default(), + defaults: types::Defaults::default(), + } + } + } }); diff --git a/src/config/types.rs b/src/config/types.rs index 1a91681..9ed0420 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -2,10 +2,67 @@ use serde::Deserialize; #[derive(Debug, Deserialize)] pub struct General { + #[serde(default)] pub debug: bool, } +impl Default for General { + fn default() -> Self { + General { debug: false } + } +} + +#[derive(Debug, Deserialize)] +pub struct Paths { + #[serde(default = "default_instances_dir")] + pub instances_dir: String, + pub java_path: Option, +} + +fn default_instances_dir() -> String { + "~/.local/share/mcl/instances".to_string() +} + +impl Default for Paths { + fn default() -> Self { + Paths { + instances_dir: default_instances_dir(), + java_path: None, + } + } +} + +#[derive(Debug, Deserialize)] +pub struct Defaults { + #[serde(default = "default_memory_min")] + pub memory_min: String, + #[serde(default = "default_memory_max")] + pub memory_max: String, +} + +fn default_memory_min() -> String { + "512M".to_string() +} + +fn default_memory_max() -> String { + "2G".to_string() +} + +impl Default for Defaults { + fn default() -> Self { + Defaults { + memory_min: default_memory_min(), + memory_max: default_memory_max(), + } + } +} + #[derive(Debug, Deserialize)] pub struct Config { + #[serde(default)] pub general: General, + #[serde(default)] + pub paths: Paths, + #[serde(default)] + pub defaults: Defaults, }