use std::{collections::BTreeMap, fmt};
use crate::{NamedDataType, SpectaID};
#[derive(Default, Clone, PartialEq)]
pub struct TypeMap {
pub(crate) map: BTreeMap<SpectaID, Option<NamedDataType>>,
pub(crate) flatten_stack: Vec<SpectaID>,
}
impl fmt::Debug for TypeMap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("TypeMap").field(&self.map).finish()
}
}
impl TypeMap {
#[track_caller]
pub fn get(&self, sid: SpectaID) -> Option<&NamedDataType> {
#[allow(clippy::bind_instead_of_map)]
self.map.get(&sid).as_ref().and_then(|v| match v {
Some(ndt) => Some(ndt),
None => {
#[cfg(debug_assertions)]
unreachable!("specta: `TypeMap::get` found a type placeholder!");
#[cfg(not(debug_assertions))]
None
}
})
}
pub fn insert(&mut self, sid: SpectaID, dt: NamedDataType) {
self.map.insert(sid, Some(dt));
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn contains_key(&self, sid: SpectaID) -> bool {
self.map.contains_key(&sid)
}
pub fn remove(&mut self, sid: SpectaID) -> Option<NamedDataType> {
self.map.remove(&sid).flatten()
}
pub fn iter(&self) -> impl Iterator<Item = (SpectaID, &NamedDataType)> {
#[allow(clippy::unnecessary_filter_map)]
self.map.iter().filter_map(|(sid, ndt)| match ndt {
Some(ndt) => Some((*sid, ndt)),
None => {
#[cfg(debug_assertions)]
unreachable!("specta: `TypeMap::into_iter` found a type placeholder!");
#[cfg(not(debug_assertions))]
None
}
})
}
}