Skip to main content

icydb_schema/node/
field.rs

1use crate::prelude::*;
2
3///
4/// FieldList
5///
6
7#[derive(Clone, Debug, Serialize)]
8pub struct FieldList {
9    fields: &'static [Field],
10}
11
12impl FieldList {
13    #[must_use]
14    pub const fn new(fields: &'static [Field]) -> Self {
15        Self { fields }
16    }
17
18    #[must_use]
19    pub const fn fields(&self) -> &'static [Field] {
20        self.fields
21    }
22
23    // get
24    #[must_use]
25    pub fn get(&self, ident: &str) -> Option<&Field> {
26        self.fields.iter().find(|field| field.ident() == ident)
27    }
28}
29
30impl ValidateNode for FieldList {}
31
32impl VisitableNode for FieldList {
33    fn drive<V: Visitor>(&self, v: &mut V) {
34        for node in self.fields() {
35            node.accept(v);
36        }
37    }
38}
39
40///
41/// Field
42///
43
44#[derive(Clone, Debug, Serialize)]
45pub enum FieldGeneration {
46    Insert(Arg),
47}
48
49#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
50pub enum FieldWriteManagement {
51    CreatedAt,
52    UpdatedAt,
53}
54
55#[derive(Clone, Debug, Serialize)]
56pub struct Field {
57    ident: &'static str,
58    value: Value,
59
60    #[serde(skip_serializing_if = "Option::is_none")]
61    default: Option<Arg>,
62
63    #[serde(skip_serializing_if = "Option::is_none")]
64    db_default: Option<Arg>,
65
66    #[serde(skip_serializing_if = "Option::is_none")]
67    generated: Option<FieldGeneration>,
68
69    #[serde(skip_serializing_if = "Option::is_none")]
70    write_management: Option<FieldWriteManagement>,
71}
72
73impl Field {
74    #[must_use]
75    pub const fn new(
76        ident: &'static str,
77        value: Value,
78        default: Option<Arg>,
79        db_default: Option<Arg>,
80        generated: Option<FieldGeneration>,
81        write_management: Option<FieldWriteManagement>,
82    ) -> Self {
83        Self {
84            ident,
85            value,
86            default,
87            db_default,
88            generated,
89            write_management,
90        }
91    }
92
93    #[must_use]
94    pub const fn ident(&self) -> &'static str {
95        self.ident
96    }
97
98    #[must_use]
99    pub const fn value(&self) -> &Value {
100        &self.value
101    }
102
103    #[must_use]
104    pub const fn default(&self) -> Option<&Arg> {
105        self.default.as_ref()
106    }
107
108    #[must_use]
109    pub const fn db_default(&self) -> Option<&Arg> {
110        self.db_default.as_ref()
111    }
112
113    #[must_use]
114    pub const fn generated(&self) -> Option<&FieldGeneration> {
115        self.generated.as_ref()
116    }
117
118    #[must_use]
119    pub const fn write_management(&self) -> Option<FieldWriteManagement> {
120        self.write_management
121    }
122}
123
124impl ValidateNode for Field {
125    fn validate(&self) -> Result<(), ErrorTree> {
126        Ok(())
127    }
128}
129
130impl VisitableNode for Field {
131    fn route_key(&self) -> String {
132        self.ident().to_string()
133    }
134
135    fn drive<V: Visitor>(&self, v: &mut V) {
136        self.value().accept(v);
137        if let Some(node) = self.default() {
138            node.accept(v);
139        }
140        if let Some(node) = self.db_default() {
141            node.accept(v);
142        }
143        if let Some(FieldGeneration::Insert(node)) = self.generated() {
144            node.accept(v);
145        }
146    }
147}