icydb_schema/node/
mod.rs

1mod arg;
2mod canister;
3mod def;
4mod entity;
5mod r#enum;
6mod field;
7mod index;
8mod item;
9mod list;
10mod map;
11mod newtype;
12mod record;
13mod sanitizer;
14mod schema;
15mod set;
16mod store;
17mod tuple;
18mod r#type;
19mod validator;
20mod value;
21
22pub use self::arg::*;
23pub use self::canister::*;
24pub use self::def::*;
25pub use self::entity::*;
26pub use self::r#enum::*;
27pub use self::field::*;
28pub use self::index::*;
29pub use self::item::*;
30pub use self::list::*;
31pub use self::map::*;
32pub use self::newtype::*;
33pub use self::record::*;
34pub use self::sanitizer::*;
35pub use self::schema::*;
36pub use self::set::*;
37pub use self::store::*;
38pub use self::tuple::*;
39pub use self::r#type::*;
40pub use self::validator::*;
41pub use self::value::*;
42
43use crate::{
44    prelude::*,
45    visit::{Event, Visitor},
46};
47use std::any::Any;
48use thiserror::Error as ThisError;
49
50///
51/// NodeError
52///
53
54#[derive(Debug, ThisError)]
55pub enum NodeError {
56    #[error("error downcasting schema node: {0}")]
57    DowncastFail(String),
58
59    #[error("{0} is an incorrect node type")]
60    IncorrectNodeType(String),
61
62    #[error("path not found: {0}")]
63    PathNotFound(String),
64}
65
66///
67/// NODE TRAITS
68///
69
70///
71/// MacroNode
72/// shared traits for every node that is created via a macro
73/// as_any has to be implemented on each type manually
74///
75
76pub trait MacroNode: Any {
77    fn as_any(&self) -> &dyn Any;
78}
79
80///
81/// TypeNode
82/// shared traits for every type node
83///
84
85pub trait TypeNode: MacroNode {
86    fn ty(&self) -> &Type;
87}
88
89///
90/// ValidateNode
91///
92
93pub trait ValidateNode {
94    fn validate(&self) -> Result<(), ErrorTree> {
95        Ok(())
96    }
97}
98
99///
100/// VisitableNode
101///
102
103pub trait VisitableNode: ValidateNode {
104    // route_key
105    fn route_key(&self) -> String {
106        String::new()
107    }
108
109    // accept
110    fn accept<V: Visitor>(&self, visitor: &mut V) {
111        visitor.push(&self.route_key());
112        visitor.visit(self, Event::Enter);
113        self.drive(visitor);
114        visitor.visit(self, Event::Exit);
115        visitor.pop();
116    }
117
118    // drive
119    fn drive<V: Visitor>(&self, _: &mut V) {}
120}