me-site/src/config.rs

81 lines
2.3 KiB
Rust
Raw Normal View History

2022-11-12 00:41:51 +01:00
use serde_json::Value;
#[derive(Debug)]
pub struct GotifySettings {
pub host: String,
pub token: String,
}
#[derive(Clone)]
pub struct Config {
root: Value,
color: Option<Value>,
}
fn read_json_file(f: &str) -> Option<Value> {
2023-01-25 07:49:21 +01:00
serde_json::from_str(&std::fs::read_to_string(f).ok()?).ok()?
2022-11-12 00:41:51 +01:00
}
impl Config {
pub fn new() -> Config {
2022-11-22 19:48:16 +01:00
let v = read_json_file("./config/config.json").expect("could not read config file");
let c = read_json_file("./config/colors.json");
2022-11-12 00:41:51 +01:00
Config { root: v, color: c }
}
pub fn name(&self) -> Option<String> {
2023-01-25 07:49:21 +01:00
Option::from(self.root.get("name")?.as_str()?.to_string())
2022-11-12 00:41:51 +01:00
}
pub fn email(&self) -> Option<String> {
2023-01-25 07:49:21 +01:00
Option::from(self.root.get("email")?.as_str()?.to_string())
2022-11-12 00:41:51 +01:00
}
pub fn xmr_address(&self) -> Option<String> {
2023-01-25 07:49:21 +01:00
Option::from(self.root.get("xmr_address")?.as_str()?.to_string())
2022-11-12 00:41:51 +01:00
}
fn color_n_fg(&self) -> Option<i64> {
2023-01-25 07:49:21 +01:00
self.root.get("colors")?.get("fg")?.as_i64()
2022-11-12 00:41:51 +01:00
}
fn color_n_bg(&self) -> Option<i64> {
2023-01-25 07:49:21 +01:00
self.root.get("colors")?.get("bg")?.as_i64()
2022-11-12 00:41:51 +01:00
}
pub fn fg_color(&self) -> Option<String> {
if let Some(col) = &self.color {
let fg = col.get("special")?.get("foreground")?.as_str()?;
if let Some(fg_n) = self.color_n_fg() {
let n = fg_n - 1;
2023-01-25 07:49:21 +01:00
let fg = col.get("colors")?.get(format!("color{n}"))?.as_str()?;
2022-11-12 00:41:51 +01:00
return Some(fg.to_string());
}
2023-01-25 07:49:21 +01:00
Some(fg.to_string())
2022-11-12 00:41:51 +01:00
} else {
2023-01-25 07:49:21 +01:00
None
2022-11-12 00:41:51 +01:00
}
}
pub fn bg_color(&self) -> Option<String> {
if let Some(col) = &self.color {
let fg = col.get("special")?.get("background")?.as_str()?;
if let Some(bg_n) = self.color_n_bg() {
let n = bg_n - 1;
2023-01-25 07:49:21 +01:00
let fg = col.get("colors")?.get(format!("color{n}"))?.as_str()?;
2022-11-12 00:41:51 +01:00
return Some(fg.to_string());
}
2023-01-25 07:49:21 +01:00
Some(fg.to_string())
2022-11-12 00:41:51 +01:00
} else {
2023-01-25 07:49:21 +01:00
None
2022-11-12 00:41:51 +01:00
}
}
pub fn gotify_config(&self) -> Option<GotifySettings> {
let settings = self.root.get("notify")?.get("gotify")?;
let host = settings.get("host")?.as_str()?.to_string();
let token = settings.get("token")?.as_str()?.to_string();
2023-01-25 07:49:21 +01:00
Some(GotifySettings { host, token })
2022-11-12 00:41:51 +01:00
}
}