Skip to main content

icydb_schema/node/
type.rs

1use crate::prelude::*;
2
3///
4/// Type
5///
6/// Canonical runtime type descriptor for one schema node's attached sanitizers
7/// and validators.
8///
9
10#[derive(Clone, Debug, Serialize)]
11pub struct Type {
12    #[serde(skip_serializing_if = "<[_]>::is_empty")]
13    sanitizers: &'static [TypeSanitizer],
14
15    #[serde(skip_serializing_if = "<[_]>::is_empty")]
16    validators: &'static [TypeValidator],
17}
18
19impl Type {
20    #[must_use]
21    pub const fn new(
22        sanitizers: &'static [TypeSanitizer],
23        validators: &'static [TypeValidator],
24    ) -> Self {
25        Self {
26            sanitizers,
27            validators,
28        }
29    }
30
31    #[must_use]
32    pub const fn sanitizers(&self) -> &'static [TypeSanitizer] {
33        self.sanitizers
34    }
35
36    #[must_use]
37    pub const fn validators(&self) -> &'static [TypeValidator] {
38        self.validators
39    }
40}
41
42impl ValidateNode for Type {}
43
44impl VisitableNode for Type {
45    fn drive<V: Visitor>(&self, v: &mut V) {
46        for node in self.sanitizers() {
47            node.accept(v);
48        }
49        for node in self.validators() {
50            node.accept(v);
51        }
52    }
53}
54
55///
56/// TypeSanitizer
57///
58/// Reference to one sanitizer node plus its bound argument list.
59///
60
61#[derive(Clone, Debug, Serialize)]
62pub struct TypeSanitizer {
63    path: &'static str,
64    args: Args,
65}
66
67impl TypeSanitizer {
68    #[must_use]
69    pub const fn new(path: &'static str, args: Args) -> Self {
70        Self { path, args }
71    }
72
73    #[must_use]
74    pub const fn path(&self) -> &'static str {
75        self.path
76    }
77
78    #[must_use]
79    pub const fn args(&self) -> &Args {
80        &self.args
81    }
82}
83
84impl ValidateNode for TypeSanitizer {
85    fn validate(&self) -> Result<(), ErrorTree> {
86        let mut errs = ErrorTree::new();
87
88        // Resolve the referenced sanitizer path against the schema graph.
89        let res = schema_read().check_node_as::<Sanitizer>(self.path());
90        if let Err(e) = res {
91            errs.add(e.to_string());
92        }
93
94        errs.result()
95    }
96}
97
98impl VisitableNode for TypeSanitizer {}
99
100///
101/// TypeValidator
102///
103/// Reference to one validator node plus its bound argument list.
104///
105
106#[derive(Clone, Debug, Serialize)]
107pub struct TypeValidator {
108    path: &'static str,
109    args: Args,
110}
111
112impl TypeValidator {
113    #[must_use]
114    pub const fn new(path: &'static str, args: Args) -> Self {
115        Self { path, args }
116    }
117
118    #[must_use]
119    pub const fn path(&self) -> &'static str {
120        self.path
121    }
122
123    #[must_use]
124    pub const fn args(&self) -> &Args {
125        &self.args
126    }
127}
128
129impl ValidateNode for TypeValidator {
130    fn validate(&self) -> Result<(), ErrorTree> {
131        let mut errs = ErrorTree::new();
132
133        // Resolve the referenced validator path against the schema graph.
134        let res = schema_read().check_node_as::<Validator>(self.path());
135        if let Err(e) = res {
136            errs.add(e.to_string());
137        }
138
139        errs.result()
140    }
141}
142
143impl VisitableNode for TypeValidator {}