use std::collections::{BTreeMap, HashMap};
use std::sync::{OnceLock, RwLock};
type Registry = HashMap<String, HashMap<String, String>>;
fn store() -> &'static RwLock<Registry> {
static STORE: OnceLock<RwLock<Registry>> = OnceLock::new();
STORE.get_or_init(|| RwLock::new(HashMap::new()))
}
pub struct MetadataRegistry;
impl MetadataRegistry {
pub fn set(handler: impl Into<String>, key: impl Into<String>, value: impl Into<String>) {
let mut guard = store().write().expect("metadata lock poisoned");
let entry = guard.entry(handler.into()).or_default();
entry.insert(key.into(), value.into());
}
pub fn get(handler: &str, key: &str) -> Option<String> {
let guard = store().read().expect("metadata lock poisoned");
guard.get(handler).and_then(|m| m.get(key)).cloned()
}
#[cfg(feature = "test-hooks")]
pub fn clear_for_tests() {
let mut guard = store().write().expect("metadata lock poisoned");
guard.clear();
}
pub fn snapshot() -> BTreeMap<String, BTreeMap<String, String>> {
let guard = store().read().expect("metadata lock poisoned");
guard
.iter()
.map(|(k, v)| {
(
k.clone(),
v.iter().map(|(k2, v2)| (k2.clone(), v2.clone())).collect(),
)
})
.collect()
}
}