use crate::{ShapeId, Trait};
use std::collections::HashMap;
#[derive(Debug)]
pub struct TraitMap {
traits: Option<HashMap<ShapeId<'static>, Box<dyn Trait>>>,
}
impl Default for TraitMap {
fn default() -> Self {
Self::new()
}
}
impl TraitMap {
pub const EMPTY: Self = Self { traits: None };
pub fn new() -> Self {
Self {
traits: Some(HashMap::new()),
}
}
pub fn insert(&mut self, trait_obj: Box<dyn Trait>) {
let id = trait_obj.trait_id().clone();
self.traits
.get_or_insert_with(HashMap::new)
.insert(id, trait_obj);
}
pub fn get(&self, id: &ShapeId<'_>) -> Option<&dyn Trait> {
self.traits.as_ref()?.get(id.as_str()).map(|t| t.as_ref())
}
pub fn get_fqn(&self, fqn: &str) -> Option<&dyn Trait> {
self.traits.as_ref()?.get(fqn).map(|t| t.as_ref())
}
pub fn contains(&self, id: &ShapeId<'_>) -> bool {
self.traits
.as_ref()
.is_some_and(|m| m.contains_key(id.as_str()))
}
pub fn contains_fqn(&self, fqn: &str) -> bool {
self.traits.as_ref().is_some_and(|m| m.contains_key(fqn))
}
pub fn len(&self) -> usize {
self.traits.as_ref().map_or(0, |m| m.len())
}
pub fn is_empty(&self) -> bool {
self.traits.as_ref().is_none_or(|m| m.is_empty())
}
}