sql_cli/config/
global.rs

1use super::config::{BehaviorConfig, Config};
2use once_cell::sync::OnceCell;
3use std::sync::RwLock;
4
5// Global config instance
6static CONFIG: OnceCell<RwLock<Config>> = OnceCell::new();
7
8/// Initialize the global config. Should be called early in application startup.
9pub fn init_config(config: Config) {
10    CONFIG.set(RwLock::new(config)).ok();
11}
12
13/// Get the date notation preference from the global config.
14/// Returns "us" if config is not initialized or on error.
15pub fn get_date_notation() -> String {
16    CONFIG.get().and_then(|c| c.read().ok()).map_or_else(
17        || "us".to_string(),
18        |c| c.behavior.default_date_notation.clone(),
19    )
20}
21
22/// Get a copy of the behavior config
23pub fn get_behavior_config() -> BehaviorConfig {
24    CONFIG
25        .get()
26        .and_then(|c| c.read().ok())
27        .map(|c| c.behavior.clone())
28        .unwrap_or_default()
29}
30
31/// Update the global config
32pub fn update_config(config: Config) {
33    if let Some(global) = CONFIG.get() {
34        if let Ok(mut c) = global.write() {
35            *c = config;
36        }
37    } else {
38        init_config(config);
39    }
40}