icydb_schema/node/
enum.rs1use crate::prelude::*;
2use std::ops::Not;
3
4#[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 TypeNode for Enum {
44 fn ty(&self) -> &Type {
45 self.ty()
46 }
47}
48
49impl ValidateNode for Enum {
50 fn validate(&self) -> Result<(), ErrorTree> {
51 Ok(())
52 }
53}
54
55impl VisitableNode for Enum {
56 fn route_key(&self) -> String {
57 self.def().path()
58 }
59
60 fn drive<V: Visitor>(&self, v: &mut V) {
61 self.def().accept(v);
62 for node in self.variants() {
63 node.accept(v);
64 }
65 self.ty().accept(v);
66 }
67}
68
69#[derive(Clone, Debug, Serialize)]
74pub struct EnumVariant {
75 ident: &'static str,
76
77 #[serde(default, skip_serializing_if = "Option::is_none")]
78 value: Option<Value>,
79
80 #[serde(default, skip_serializing_if = "Not::not")]
81 default: bool,
82
83 #[serde(default, skip_serializing_if = "Not::not")]
84 unspecified: bool,
85}
86
87impl EnumVariant {
88 #[must_use]
89 pub const fn new(
90 ident: &'static str,
91 value: Option<Value>,
92 default: bool,
93 unspecified: bool,
94 ) -> Self {
95 Self {
96 ident,
97 value,
98 default,
99 unspecified,
100 }
101 }
102
103 #[must_use]
104 pub const fn ident(&self) -> &'static str {
105 self.ident
106 }
107
108 #[must_use]
109 pub const fn value(&self) -> Option<&Value> {
110 self.value.as_ref()
111 }
112
113 #[must_use]
114 pub const fn default(&self) -> bool {
115 self.default
116 }
117
118 #[must_use]
119 pub const fn unspecified(&self) -> bool {
120 self.unspecified
121 }
122}
123
124impl ValidateNode for EnumVariant {
125 fn validate(&self) -> Result<(), ErrorTree> {
126 Ok(())
127 }
128}
129
130impl VisitableNode for EnumVariant {
131 fn drive<V: Visitor>(&self, v: &mut V) {
132 if let Some(node) = self.value() {
133 node.accept(v);
134 }
135 }
136}