aws_smithy_schema/schema/
trait_map.rs1use crate::{ShapeId, Trait};
7use std::collections::HashMap;
8
9#[derive(Debug)]
13pub struct TraitMap {
14 traits: Option<HashMap<ShapeId, Box<dyn Trait>>>,
17}
18
19impl Default for TraitMap {
20 fn default() -> Self {
21 Self::new()
22 }
23}
24
25impl TraitMap {
26 pub const EMPTY: Self = Self { traits: None };
28
29 pub fn new() -> Self {
31 Self {
32 traits: Some(HashMap::new()),
33 }
34 }
35
36 pub fn insert(&mut self, trait_obj: Box<dyn Trait>) {
38 let id = *trait_obj.trait_id();
39 self.traits
40 .get_or_insert_with(HashMap::new)
41 .insert(id, trait_obj);
42 }
43
44 pub fn get(&self, id: &ShapeId) -> Option<&dyn Trait> {
46 self.traits.as_ref()?.get(id).map(|t| t.as_ref())
47 }
48
49 pub fn contains(&self, id: &ShapeId) -> bool {
51 self.traits.as_ref().is_some_and(|m| m.contains_key(id))
52 }
53
54 pub fn len(&self) -> usize {
56 self.traits.as_ref().map_or(0, |m| m.len())
57 }
58
59 pub fn is_empty(&self) -> bool {
61 self.traits.as_ref().is_none_or(|m| m.is_empty())
62 }
63}