use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::OnceLock;
use std::sync::RwLock;
use crate::caches::BaseCache;
static VERBOSE: AtomicBool = AtomicBool::new(false);
static DEBUG: AtomicBool = AtomicBool::new(false);
static LLM_CACHE: OnceLock<RwLock<Option<Box<dyn BaseCache>>>> = OnceLock::new();
pub fn set_verbose(value: bool) {
VERBOSE.store(value, Ordering::Relaxed);
}
pub fn get_verbose() -> bool {
VERBOSE.load(Ordering::Relaxed)
}
pub fn set_debug(value: bool) {
DEBUG.store(value, Ordering::Relaxed);
}
pub fn get_debug() -> bool {
DEBUG.load(Ordering::Relaxed)
}
fn cache_lock() -> &'static RwLock<Option<Box<dyn BaseCache>>> {
LLM_CACHE.get_or_init(|| RwLock::new(None))
}
pub fn set_llm_cache(value: Option<Box<dyn BaseCache>>) {
let mut guard = cache_lock().write().expect("cache lock poisoned");
*guard = value;
}
pub fn has_llm_cache() -> bool {
let guard = cache_lock().read().expect("cache lock poisoned");
guard.is_some()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_verbose_default() {
set_verbose(false);
assert!(!get_verbose());
}
#[test]
fn test_set_verbose() {
set_verbose(true);
assert!(get_verbose());
set_verbose(false);
assert!(!get_verbose());
}
#[test]
fn test_debug_set_and_reset() {
set_debug(false);
assert!(!get_debug());
set_debug(true);
assert!(get_debug());
set_debug(false);
assert!(!get_debug());
}
}