iota/src/util/config_util.rs
Alex Emmet 24431f2167 API mods
2025-11-21 16:37:13 +01:00

51 lines
1.2 KiB
Rust

use crate::util::file_util::{load_file, save_file};
use json::JsonValue;
use once_cell::sync::Lazy;
use tokio::sync::Mutex;
use uuid::Uuid;
pub static CONFIG: Lazy<Mutex<ConfigUtil>> = Lazy::new(|| Mutex::new(ConfigUtil::new()));
pub struct ConfigUtil {
pub config: JsonValue,
pub unique: bool,
}
impl ConfigUtil {
pub fn new() -> Self {
Self {
config: JsonValue::new_object(),
unique: false,
}
}
pub fn load(&mut self) {
let s = load_file("", "config.json");
if !s.is_empty() {
self.config = json::parse(&s).unwrap_or(JsonValue::new_object());
}
}
pub fn get_iota_id(&self) -> Uuid {
self.config["iota_id"]
.as_str()
.unwrap_or_default()
.parse()
.unwrap_or_default()
}
pub fn get_port(&self) -> u16 {
self.config["port"].as_u16().unwrap_or(1984)
}
pub fn change(&mut self, key: &str, value: Uuid) {
self.config[key] = JsonValue::String(value.to_string());
self.unique = true;
}
pub fn update(&mut self) {
if self.unique {
save_file("", "config.json", &self.config.to_string());
}
}
}