Skip to main content

icydb_model/node/
map.rs

1//! Module: node::map
2//! Responsibility: schema graph metadata for map collection nodes.
3//! Does not own: runtime map encoding, validation policy, or visitor execution.
4//! Boundary: stores canonical key/value descriptors for downstream schema visitors.
5
6use crate::prelude::*;
7
8///
9/// Map
10///
11/// Schema node describing a map collection with key/value descriptors and one
12/// canonical runtime type.
13///
14
15#[derive(Clone, Debug, Serialize)]
16pub struct Map {
17    def: Def,
18    source_key: &'static str,
19    key: Item,
20    value: Value,
21    ty: Type,
22}
23
24impl Map {
25    /// Creates a map node from its canonical schema parts.
26    #[must_use]
27    pub const fn new(
28        def: Def,
29        source_key: &'static str,
30        key: Item,
31        value: Value,
32        ty: Type,
33    ) -> Self {
34        Self {
35            def,
36            source_key,
37            key,
38            value,
39            ty,
40        }
41    }
42
43    /// Returns the definition metadata for this map node.
44    #[must_use]
45    pub const fn def(&self) -> &Def {
46        &self.def
47    }
48
49    /// Returns the immutable type source key.
50    #[must_use]
51    pub const fn source_key(&self) -> &'static str {
52        self.source_key
53    }
54
55    /// Returns the key descriptor.
56    #[must_use]
57    pub const fn key(&self) -> &Item {
58        &self.key
59    }
60
61    /// Returns the value descriptor.
62    #[must_use]
63    pub const fn value(&self) -> &Value {
64        &self.value
65    }
66
67    /// Returns the canonical runtime type descriptor.
68    #[must_use]
69    pub const fn ty(&self) -> &Type {
70        &self.ty
71    }
72}
73
74impl MacroNode for Map {
75    fn as_any(&self) -> &dyn std::any::Any {
76        self
77    }
78}
79
80impl ValidateNode for Map {
81    fn validate(&self) -> Result<(), ErrorTree> {
82        let mut errs = ErrorTree::new();
83        validate_source_key(
84            &mut errs,
85            "map type",
86            self.source_key(),
87            icydb_schema::TypeSourceKey::try_new,
88        );
89        errs.result()
90    }
91}
92
93impl VisitableNode for Map {
94    fn route_key(&self) -> String {
95        self.def().path()
96    }
97
98    fn drive<V: Visitor>(&self, v: &mut V) {
99        self.def().accept(v);
100        self.key().accept(v);
101        self.value().accept(v);
102        self.ty().accept(v);
103    }
104}