Skip to main content

icydb_schema/node/
type.rs

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