Skip to content

Commit

Permalink
feat(config): implement log as feature
Browse files Browse the repository at this point in the history
  • Loading branch information
SkuldNorniern committed Mar 13, 2024
1 parent d554e0d commit 1c0b850
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 4 additions & 0 deletions fluere-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
33 changes: 28 additions & 5 deletions fluere-config/src/init.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Expand All @@ -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()
}
Expand Down

0 comments on commit 1c0b850

Please sign in to comment.