Skip to main content

icydb_schema/node/
enum.rs

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