Add configs

This commit is contained in:
Gvidas Juknevičius 2024-06-17 14:07:57 +03:00
parent 85891abba0
commit 208eed348b
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
3 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,6 @@
seperator=" | "
[plugins]
blacklist=["example_c", "example_rust"]
as_whitelist=false
template=["counter", "battery", "clock"]

6
config/main.toml Normal file
View File

@ -0,0 +1,6 @@
seperator=" | "
[plugins]
blacklist=["example_c", "example_rust"]
as_whitelist=false
template=["counter", "battery", "clock"]

55
src/config.rs Normal file
View File

@ -0,0 +1,55 @@
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 path = path.join("main.toml");
if !path.exists() {
println!("ERROR: {:?} doesnt exist", path);
}
let mut cfg: Self = toml::from_str(
&std::fs::read_to_string(&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)
}
}