diff --git a/Cargo.toml b/Cargo.toml index 2c323b1..1f65aea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ serde = { version = "1.0.193", features = ["derive"] } toml = "0.8.8" fluere_plugin = { version = "0.2.0", path = "./fluere-plugin", features = ["log"] } -fluere-config = { version = "0.1.3", path = "./fluere-config" } +fluere-config = { version = "0.1.3", path = "./fluere-config", features = ["log"] } fluereflow = { version = "0.3.2", path = "./fluereflow" } ratatui = { version = "0.25.0", features = ["all-widgets"] } diff --git a/fluere-config/Cargo.toml b/fluere-config/Cargo.toml index d736838..3d9b27d 100644 --- a/fluere-config/Cargo.toml +++ b/fluere-config/Cargo.toml @@ -13,7 +13,11 @@ repository = "https://github.com/SkuldNorniern/fluere" dirs = "5.0.1" serde = { version = "1.0.193", features = ["derive"]} toml = "0.8.8" +log = { version = "0.4.21", features = ["std"], optional = true } [lib] name = "fluere_config" + +[features] +log = ["dep:log"] \ No newline at end of file diff --git a/fluere-config/src/init.rs b/fluere-config/src/init.rs index a654d02..648218a 100644 --- a/fluere-config/src/init.rs +++ b/fluere-config/src/init.rs @@ -1,20 +1,35 @@ -use dirs::config_dir; +use std::{default::Default, env, fs, path::Path, path::PathBuf}; use crate::Config; -use std::{default::Default, env, fs, path::Path, path::PathBuf}; +use dirs::config_dir; + +#[cfg(feature = "log")] +use log::{debug, error, info, warn}; impl Config { pub fn new() -> Self { let path_base = home_config_path(); let path_file = path_base.join(Path::new("fluere.toml")); - println!("path_file: {:?}", path_file); + + #[cfg(feature = "log")] + debug!("Using config file from: {:?}", path_file); + #[cfg(not(feature = "log"))] + println!("Using config file from: {:?}", path_file); if !path_base.exists() { match fs::create_dir_all(&path_base) { - Ok(_) => (), + Ok(_) => { + #[cfg(feature = "log")] + debug!("Created directory at {:?}", path_base); + () + } Err(e) => { + #[cfg(feature = "log")] + error!("Failed to create directory at {:?}: {}", path_base, e); + #[cfg(not(feature = "log"))] eprintln!("Failed to create directory at {:?}: {}", path_base, e); + return Config::default(); } } @@ -25,8 +40,16 @@ impl Config { } match Self::load(path_file.to_str().unwrap().to_string()) { - Ok(config) => config, + Ok(config) => { + #[cfg(feature = "log")] + debug!("Loaded configuration from: {:?}", path_file); + + config + } Err(_) => { + #[cfg(feature = "log")] + warn!("failed to load configuration, using default config"); + #[cfg(not(feature = "log"))] println!("failed to load configuration, using default config"); Config::default() }