dim/src/config.rs
2024-06-20 23:43:16 +03:00

56 lines
1.2 KiB
Rust

use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Config {
pub seperator: String,
pub plugins: ConfigPlugins,
#[serde(skip)]
pub config_dir: PathBuf,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct ConfigPlugins {
pub blacklist: Vec<String>,
pub as_whitelist: bool,
pub template: Vec<String>,
}
impl Config {
pub fn new(path: &Path) -> Result<Self, ConfigError> {
let cfg_path = path.join("main.toml");
if !cfg_path.exists() {
println!("ERROR: {:?} doesnt exist", path);
}
let mut cfg: Self = toml::from_str(
&std::fs::read_to_string(&cfg_path)?
)?;
cfg.config_dir = path.to_path_buf();
Ok(cfg)
}
}
#[allow(dead_code)]
#[derive(Debug)]
pub enum ConfigError {
IoError(std::io::Error),
TomlDeserialiseError(toml::de::Error)
}
impl From<std::io::Error> for ConfigError {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}
impl From<toml::de::Error> for ConfigError {
fn from(e: toml::de::Error) -> Self {
Self::TomlDeserialiseError(e)
}
}