icydb_schema/node/
type.rs1use crate::prelude::*;
2
3#[derive(Clone, Debug, Serialize)]
11pub struct Type {
12 #[serde(default, skip_serializing_if = "<[_]>::is_empty")]
13 sanitizers: &'static [TypeSanitizer],
14
15 #[serde(default, 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#[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 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#[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 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 {}