use std::fmt::Display;
use std::ops::Deref;
use std::sync::Arc;
use vortex_utils::aliases::dash_map::DashMap;
#[derive(Clone, Debug)]
pub struct Registry<T>(Arc<DashMap<String, T>>);
impl<T> Default for Registry<T> {
fn default() -> Self {
Self(Default::default())
}
}
impl<T: Clone + Display + Eq> Registry<T> {
pub fn empty() -> Self {
Self(Default::default())
}
pub fn items(&self) -> impl Iterator<Item = T> + '_ {
self.0.iter().map(|i| i.value().clone())
}
pub fn find_many<'a>(
&self,
ids: impl IntoIterator<Item = &'a str>,
) -> impl Iterator<Item = Option<impl Deref<Target = T>>> {
ids.into_iter().map(|id| self.0.get(id))
}
pub fn find(&self, id: &str) -> Option<T> {
self.0.get(id).as_deref().cloned()
}
pub fn register(&self, item: T) {
self.0.insert(item.to_string(), item);
}
pub fn register_many<I: IntoIterator<Item = T>>(&self, items: I) {
for item in items {
self.0.insert(item.to_string(), item);
}
}
}