#[cfg(test)]
mod tests;
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
pub struct FeatureStore {
flags: Mutex<HashMap<String, bool>>,
}
impl FeatureStore {
fn new() -> Self {
FeatureStore { flags: Mutex::new(HashMap::new()) }
}
pub fn set(&self, name: &str, enabled: bool) {
self.flags.lock().unwrap().insert(name.to_string(), enabled);
}
pub fn is_enabled(&self, name: &str) -> bool {
*self.flags.lock().unwrap().get(name).unwrap_or(&false)
}
pub fn list(&self) -> Vec<(String, bool)> {
let mut pairs: Vec<(String, bool)> = self.flags.lock().unwrap()
.iter()
.map(|(k, v)| (k.clone(), *v))
.collect();
pairs.sort_by(|a, b| a.0.cmp(&b.0));
pairs
}
}
static INSTANCE: OnceLock<FeatureStore> = OnceLock::new();
pub fn global() -> &'static FeatureStore {
INSTANCE.get_or_init(FeatureStore::new)
}