feat: add logger and config structure
This commit is contained in:
1 parent
15abd84501
commit
d9c827b3ef
11 files changed
+906
-19
No files matched your search
@@ -0,0 +1,34 @@
|
||||
use config::{Config as ConfigLoader, ConfigError, File};
|
||||
use dirs_next::config_dir;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use obj::Config;
|
||||
|
||||
pub mod obj;
|
||||
|
||||
fn get_config_path() -> PathBuf {
|
||||
let base_dir = config_dir().unwrap();
|
||||
base_dir.join("mcl/config.toml")
|
||||
}
|
||||
|
||||
pub fn ensure_config_exists(default_path: &str) -> PathBuf {
|
||||
let config_path = get_config_path();
|
||||
|
||||
if !config_path.exists() {
|
||||
if let Some(parent_dir) = config_path.parent() {
|
||||
fs::create_dir_all(parent_dir).expect("Failed to create configuration directory");
|
||||
}
|
||||
|
||||
fs::copy(default_path, &config_path).expect("Failed to copy default configuration file");
|
||||
println!("Default configuration copied to '{}'", config_path.display());
|
||||
}
|
||||
|
||||
config_path
|
||||
}
|
||||
|
||||
pub fn load_config(config_path: &PathBuf) -> Result<Config, ConfigError> {
|
||||
ConfigLoader::builder()
|
||||
.add_source(File::from(config_path.clone()))
|
||||
.build()?
|
||||
.try_deserialize()
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Variables {
|
||||
pub debug: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Paths {
|
||||
pub log_dir: String,
|
||||
pub config_dir: String,
|
||||
pub save_dir: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Colors {
|
||||
pub background: String,
|
||||
pub foreground: String,
|
||||
pub highlight: String,
|
||||
pub focused: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub variables: Variables,
|
||||
pub paths: Paths,
|
||||
pub colors: Colors,
|
||||
}
|
||||
Reference in new issue
Block a user