use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::RwLock;
pub(crate) static HIPPOX_CORE_CONFIG: Lazy<RwLock<HippoxConfig>> =
Lazy::new(|| RwLock::new(HippoxConfig::default()));
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct IdentityInformation {
pub name: Option<String>,
pub sex: Option<String>,
pub age: Option<String>,
pub species: Option<String>,
pub role: Option<String>,
pub personality: Option<String>,
pub tone_style: Option<String>,
pub knowledge_scope: Option<String>,
pub catchphrase: Option<String>,
pub taboos: Option<String>,
}
impl Default for IdentityInformation {
fn default() -> Self {
Self {
name: Some("Hippox".to_string()),
sex: None,
age: None,
species: None,
role: None,
personality: None,
tone_style: None,
knowledge_scope: None,
catchphrase: None,
taboos: None,
}
}
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct HippoxConfig {
pub lang: String,
pub identity_information: IdentityInformation,
}
impl Default for HippoxConfig {
fn default() -> Self {
Self {
lang: "en".to_string(),
identity_information: IdentityInformation::default(),
}
}
}
impl HippoxConfig {
pub fn get_identity(&self) -> &IdentityInformation {
&self.identity_information
}
pub fn get_identity_mut(&mut self) -> &mut IdentityInformation {
&mut self.identity_information
}
pub fn update_identity<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut IdentityInformation),
{
f(&mut self.identity_information);
self
}
pub fn load_from_toml_file(path: &str) -> anyhow::Result<Self> {
let content = std::fs::read_to_string(path)?;
let config: HippoxConfig = toml::from_str(&content)?;
Ok(config)
}
pub fn load_from_json_file(path: &str) -> anyhow::Result<Self> {
let content = std::fs::read_to_string(path)?;
let config: HippoxConfig = serde_json::from_str(&content)?;
Ok(config)
}
}
pub fn get_config() -> HippoxConfig {
HIPPOX_CORE_CONFIG.read().unwrap().clone()
}
pub fn update_config<F>(f: F) -> anyhow::Result<()>
where
F: FnOnce(&mut HippoxConfig),
{
let mut global = HIPPOX_CORE_CONFIG.write().unwrap();
f(&mut global);
Ok(())
}
pub fn get_lang() -> String {
HIPPOX_CORE_CONFIG.read().unwrap().lang.clone()
}
pub fn set_lang(lang: String) -> anyhow::Result<()> {
let mut config = HIPPOX_CORE_CONFIG.write().unwrap();
config.lang = lang;
Ok(())
}
pub fn get_hippox_core_config() -> HippoxConfig {
HIPPOX_CORE_CONFIG.read().unwrap().clone()
}