use super::config::{BehaviorConfig, Config};
use once_cell::sync::OnceCell;
use std::sync::RwLock;
static CONFIG: OnceCell<RwLock<Config>> = OnceCell::new();
pub fn init_config(config: Config) {
CONFIG.set(RwLock::new(config)).ok();
}
pub fn get_date_notation() -> String {
if let Ok(notation) = std::env::var("SQL_CLI_DATE_NOTATION") {
return notation;
}
CONFIG.get().and_then(|c| c.read().ok()).map_or_else(
|| "us".to_string(),
|c| c.behavior.default_date_notation.clone(),
)
}
pub fn get_behavior_config() -> BehaviorConfig {
CONFIG
.get()
.and_then(|c| c.read().ok())
.map(|c| c.behavior.clone())
.unwrap_or_default()
}
pub fn update_config(config: Config) {
if let Some(global) = CONFIG.get() {
if let Ok(mut c) = global.write() {
*c = config;
}
} else {
init_config(config);
}
}