imgurs/src/config/toml.rs

32 lines
740 B
Rust
Raw Normal View History

2022-01-22 21:02:51 +00:00
use dirs::config_dir;
2022-01-22 21:14:26 +00:00
use serde_derive::Deserialize;
2022-01-22 21:02:51 +00:00
use std::fs::read_to_string;
2022-01-22 21:14:26 +00:00
use toml::from_str;
2022-01-22 21:02:51 +00:00
#[derive(Debug, Deserialize)]
pub struct Config {
2022-01-22 21:14:26 +00:00
pub imgur: ConfigImgur,
2022-01-24 19:49:27 +00:00
pub notification: ConfigNotification,
2022-01-22 21:02:51 +00:00
}
#[derive(Debug, Deserialize)]
pub struct ConfigImgur {
2022-01-22 21:14:26 +00:00
pub id: String,
2022-01-22 21:02:51 +00:00
}
#[derive(Debug, Deserialize)]
pub struct ConfigNotification {
pub enable: bool,
}
2022-01-22 21:02:51 +00:00
pub fn parse() -> Result<Config, String> {
let config_dir = config_dir().unwrap();
let file_dir: String = String::from(config_dir.to_string_lossy()) + "/imgurs/config.toml";
2022-01-22 21:02:51 +00:00
let toml_str = read_to_string(file_dir).map_err(|err| err.to_string())?;
let decode = from_str(&toml_str).map_err(|err| err.to_string())?;
Ok(decode)
}