Skip to main content

icydb_schema/node/
enum.rs

1use crate::prelude::*;
2
3///
4/// Enum
5///
6
7#[derive(Clone, Debug, Serialize)]
8pub struct Enum {
9    def: Def,
10    variants: &'static [EnumVariant],
11    ty: Type,
12}
13
14impl Enum {
15    #[must_use]
16    pub const fn new(def: Def, variants: &'static [EnumVariant], ty: Type) -> Self {
17        Self { def, variants, ty }
18    }
19
20    #[must_use]
21    pub const fn def(&self) -> &Def {
22        &self.def
23    }
24
25    #[must_use]
26    pub const fn variants(&self) -> &'static [EnumVariant] {
27        self.variants
28    }
29
30    #[must_use]
31    pub const fn ty(&self) -> &Type {
32        &self.ty
33    }
34}
35
36impl MacroNode for Enum {
37    fn as_any(&self) -> &dyn std::any::Any {
38        self
39    }
40}
41
42impl ValidateNode for Enum {
43    fn validate(&self) -> Result<(), ErrorTree> {
44        Ok(())
45    }
46}
47
48impl VisitableNode for Enum {
49    fn route_key(&self) -> String {
50        self.def().path()
51    }
52
53    fn drive<V: Visitor>(&self, v: &mut V) {
54        self.def().accept(v);
55        for node in self.variants() {
56            node.accept(v);
57        }
58        self.ty().accept(v);
59    }
60}
61
62///
63/// EnumVariant
64///
65
66#[derive(Clone, Debug, Serialize)]
67pub struct EnumVariant {
68    ident: &'static str,
69
70    #[serde(skip_serializing_if = "Option::is_none")]
71    value: Option<Value>,
72}
73
74impl EnumVariant {
75    #[must_use]
76    pub const fn new(ident: &'static str, value: Option<Value>) -> Self {
77        Self { ident, value }
78    }
79
80    #[must_use]
81    pub const fn ident(&self) -> &'static str {
82        self.ident
83    }
84
85    #[must_use]
86    pub const fn value(&self) -> Option<&Value> {
87        self.value.as_ref()
88    }
89}
90
91impl ValidateNode for EnumVariant {
92    fn validate(&self) -> Result<(), ErrorTree> {
93        Ok(())
94    }
95}
96
97impl VisitableNode for EnumVariant {
98    fn drive<V: Visitor>(&self, v: &mut V) {
99        if let Some(node) = self.value() {
100            node.accept(v);
101        }
102    }
103}