use std::{
collections::HashMap,
sync::{Mutex, OnceLock},
};
use crate::ansi::Style;
use crate::errors::LexError;
static REGISTRY: OnceLock<Mutex<HashMap<String, Style>>> = OnceLock::new();
pub fn insert_style(name: impl Into<String>, style: Style) {
REGISTRY
.get_or_init(|| Mutex::new(HashMap::new()))
.lock()
.unwrap()
.insert(name.into(), style);
}
pub fn set_prefix(name: impl Into<String>, prefix: impl Into<String>) -> Result<(), LexError> {
let name = name.into();
let prefix = prefix.into();
let mut map = REGISTRY
.get_or_init(|| Mutex::new(HashMap::new()))
.lock()
.unwrap();
if let Some(style) = map.get_mut(&name) {
style.prefix = Some(prefix);
Ok(())
} else {
Err(LexError::UnknownStyle(name))
}
}
pub(crate) fn search_registry(query: impl Into<String>) -> Result<Style, LexError> {
let map = REGISTRY
.get_or_init(|| Mutex::new(HashMap::new()))
.lock()
.unwrap();
let query = query.into();
match map.get(&query) {
Some(style) => Ok(style.clone()),
None => Err(LexError::InvalidTag(query)),
}
}