use crate::Identifier;
use std::{any::type_name, collections::BTreeMap, fmt};
#[derive(Debug, Eq, PartialEq, Clone, Hash, Default)]
pub struct CustomTypeInfo {
pub display_name: Identifier,
}
#[derive(Clone, Hash, Default)]
pub struct CustomTypesCollection(BTreeMap<Identifier, CustomTypeInfo>);
impl fmt::Debug for CustomTypesCollection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("CustomTypesCollection ")?;
f.debug_map().entries(self.0.iter()).finish()
}
}
impl CustomTypesCollection {
#[inline(always)]
pub fn new() -> Self {
Self(BTreeMap::new())
}
#[inline(always)]
pub fn add(&mut self, type_name: impl Into<Identifier>, name: impl Into<Identifier>) {
self.add_raw(
type_name,
CustomTypeInfo {
display_name: name.into(),
},
);
}
#[inline(always)]
pub fn add_type<T>(&mut self, name: &str) {
self.add_raw(
type_name::<T>(),
CustomTypeInfo {
display_name: name.into(),
},
);
}
#[inline(always)]
pub fn add_raw(&mut self, type_name: impl Into<Identifier>, custom_type: CustomTypeInfo) {
self.0.insert(type_name.into(), custom_type);
}
#[inline(always)]
pub fn get(&self, key: &str) -> Option<&CustomTypeInfo> {
self.0.get(key)
}
}