1use crate::{Error, prelude::*};
2use canic_cdk::utils::time::now_secs;
3use std::{any::Any, collections::BTreeMap};
4
5#[remain::sorted]
10#[derive(Clone, Debug, Serialize)]
11pub enum SchemaNode {
12 Canister(Canister),
13 DataStore(DataStore),
14 Entity(Entity),
15 Enum(Enum),
16 IndexStore(IndexStore),
17 List(List),
18 Map(Map),
19 Newtype(Newtype),
20 Record(Record),
21 Sanitizer(Sanitizer),
22 Set(Set),
23 Tuple(Tuple),
24 Validator(Validator),
25}
26
27impl SchemaNode {
28 #[must_use]
29 pub fn get_type(&self) -> Option<Box<dyn TypeNode>> {
30 match self {
31 Self::Entity(n) => Some(Box::new(n.clone())),
32 Self::Enum(n) => Some(Box::new(n.clone())),
33 Self::List(n) => Some(Box::new(n.clone())),
34 Self::Map(n) => Some(Box::new(n.clone())),
35 Self::Newtype(n) => Some(Box::new(n.clone())),
36 Self::Record(n) => Some(Box::new(n.clone())),
37 Self::Set(n) => Some(Box::new(n.clone())),
38 Self::Tuple(n) => Some(Box::new(n.clone())),
39 _ => None,
40 }
41 }
42}
43
44impl SchemaNode {
45 const fn def(&self) -> &Def {
46 match self {
47 Self::Canister(n) => &n.def,
48 Self::DataStore(n) => &n.def,
49 Self::Entity(n) => &n.def,
50 Self::Enum(n) => &n.def,
51 Self::IndexStore(n) => &n.def,
52 Self::List(n) => &n.def,
53 Self::Map(n) => &n.def,
54 Self::Newtype(n) => &n.def,
55 Self::Record(n) => &n.def,
56 Self::Sanitizer(n) => &n.def,
57 Self::Set(n) => &n.def,
58 Self::Tuple(n) => &n.def,
59 Self::Validator(n) => &n.def,
60 }
61 }
62}
63
64impl MacroNode for SchemaNode {
65 fn as_any(&self) -> &dyn Any {
66 match self {
67 Self::Canister(n) => n.as_any(),
68 Self::DataStore(n) => n.as_any(),
69 Self::Entity(n) => n.as_any(),
70 Self::Enum(n) => n.as_any(),
71 Self::IndexStore(n) => n.as_any(),
72 Self::List(n) => n.as_any(),
73 Self::Map(n) => n.as_any(),
74 Self::Newtype(n) => n.as_any(),
75 Self::Record(n) => n.as_any(),
76 Self::Sanitizer(n) => n.as_any(),
77 Self::Set(n) => n.as_any(),
78 Self::Tuple(n) => n.as_any(),
79 Self::Validator(n) => n.as_any(),
80 }
81 }
82}
83
84impl ValidateNode for SchemaNode {}
85
86impl VisitableNode for SchemaNode {
87 fn drive<V: Visitor>(&self, v: &mut V) {
88 match self {
89 Self::Canister(n) => n.accept(v),
90 Self::DataStore(n) => n.accept(v),
91 Self::Entity(n) => n.accept(v),
92 Self::Enum(n) => n.accept(v),
93 Self::IndexStore(n) => n.accept(v),
94 Self::List(n) => n.accept(v),
95 Self::Map(n) => n.accept(v),
96 Self::Newtype(n) => n.accept(v),
97 Self::Record(n) => n.accept(v),
98 Self::Sanitizer(n) => n.accept(v),
99 Self::Set(n) => n.accept(v),
100 Self::Tuple(n) => n.accept(v),
101 Self::Validator(n) => n.accept(v),
102 }
103 }
104}
105
106#[derive(Clone, Debug, Serialize)]
111pub struct Schema {
112 pub nodes: BTreeMap<String, SchemaNode>,
113 pub hash: &'static str,
114 pub timestamp: u64,
115}
116
117impl Schema {
118 #[must_use]
119 pub fn new() -> Self {
120 Self {
121 nodes: BTreeMap::new(),
122 hash: "",
123 timestamp: now_secs(),
124 }
125 }
126
127 pub fn insert_node(&mut self, node: SchemaNode) {
129 self.nodes.insert(node.def().path(), node);
130 }
131
132 #[must_use]
134 pub fn get_node<'a>(&'a self, path: &str) -> Option<&'a SchemaNode> {
135 self.nodes.get(path)
136 }
137
138 pub fn try_get_node<'a>(&'a self, path: &str) -> Result<&'a SchemaNode, Error> {
140 let node = self
141 .get_node(path)
142 .ok_or_else(|| NodeError::PathNotFound(path.to_string()))?;
143
144 Ok(node)
145 }
146
147 pub fn cast_node<'a, T: 'static>(&'a self, path: &str) -> Result<&'a T, Error> {
149 let node = self.try_get_node(path)?;
150
151 node.as_any()
152 .downcast_ref::<T>()
153 .ok_or_else(|| NodeError::IncorrectNodeType(path.to_string()).into())
154 }
155
156 pub fn check_node_as<T: 'static>(&self, path: &str) -> Result<(), Error> {
158 self.cast_node::<T>(path).map(|_| ())
159 }
160
161 pub fn get_nodes<T: 'static>(&self) -> impl Iterator<Item = (&str, &T)> {
163 self.nodes
164 .iter()
165 .filter_map(|(key, node)| node.as_any().downcast_ref::<T>().map(|n| (key.as_str(), n)))
166 }
167
168 pub fn get_node_values<T: 'static>(&'_ self) -> impl Iterator<Item = &'_ T> + '_ {
170 self.nodes
171 .values()
172 .filter_map(|node| node.as_any().downcast_ref::<T>())
173 }
174
175 pub fn filter_nodes<'a, T: 'static>(
178 &'a self,
179 predicate: impl Fn(&T) -> bool + 'a,
180 ) -> impl Iterator<Item = (&'a str, &'a T)> + 'a {
181 self.nodes.iter().filter_map(move |(key, node)| {
182 node.as_any()
183 .downcast_ref::<T>()
184 .filter(|target| predicate(target))
185 .map(|target| (key.as_str(), target))
186 })
187 }
188}
189
190impl Default for Schema {
191 fn default() -> Self {
192 Self::new()
193 }
194}
195
196impl ValidateNode for Schema {}
197
198impl VisitableNode for Schema {
199 fn drive<V: Visitor>(&self, v: &mut V) {
200 for node in self.nodes.values() {
201 node.accept(v);
202 }
203 }
204}