80 lines
2.3 KiB
Rust
80 lines
2.3 KiB
Rust
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> {
|
|
serde_json::from_str(&std::fs::read_to_string(f).ok()?).ok()?
|
|
}
|
|
|
|
impl Config {
|
|
pub fn new() -> Config {
|
|
let v = read_json_file("./config/config.json").expect("could not read config file");
|
|
let c = read_json_file("./config/colors.json");
|
|
Config { root: v, color: c }
|
|
}
|
|
|
|
pub fn name(&self) -> Option<String> {
|
|
Option::from(self.root.get("name")?.as_str()?.to_string())
|
|
}
|
|
|
|
pub fn email(&self) -> Option<String> {
|
|
Option::from(self.root.get("email")?.as_str()?.to_string())
|
|
}
|
|
|
|
pub fn xmr_address(&self) -> Option<String> {
|
|
Option::from(self.root.get("xmr_address")?.as_str()?.to_string())
|
|
}
|
|
|
|
fn color_n_fg(&self) -> Option<i64> {
|
|
self.root.get("colors")?.get("fg")?.as_i64()
|
|
}
|
|
|
|
fn color_n_bg(&self) -> Option<i64> {
|
|
self.root.get("colors")?.get("bg")?.as_i64()
|
|
}
|
|
|
|
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;
|
|
let fg = col.get("colors")?.get(format!("color{n}"))?.as_str()?;
|
|
return Some(fg.to_string());
|
|
}
|
|
Some(fg.to_string())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
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;
|
|
let fg = col.get("colors")?.get(format!("color{n}"))?.as_str()?;
|
|
return Some(fg.to_string());
|
|
}
|
|
Some(fg.to_string())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
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();
|
|
Some(GotifySettings { host, token })
|
|
}
|
|
}
|