use super::types::Config;
use std::sync::{Arc, OnceLock, RwLock};
static CURRENT: OnceLock<RwLock<Arc<Config>>> = OnceLock::new();
fn cell() -> &'static RwLock<Arc<Config>> {
CURRENT.get_or_init(|| {
let cfg = Config::load().unwrap_or_else(|e| {
tracing::warn!(
"current-config: initial disk load failed ({e}); using embedded defaults"
);
embedded_default()
});
RwLock::new(Arc::new(cfg))
})
}
fn embedded_default() -> Config {
toml::from_str(include_str!("../../config.toml.example"))
.expect("embedded config.toml.example must parse")
}
impl Config {
pub fn current() -> Arc<Config> {
cell().read().unwrap_or_else(|e| e.into_inner()).clone()
}
pub fn set_current(config: Config) {
*cell().write().unwrap_or_else(|e| e.into_inner()) = Arc::new(config);
}
}