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    generated: Option<FieldGeneration>,
65
66    #[serde(skip_serializing_if = "Option::is_none")]
67    write_management: Option<FieldWriteManagement>,
68}
69
70impl Field {
71    #[must_use]
72    pub const fn new(
73        ident: &'static str,
74        value: Value,
75        default: Option<Arg>,
76        generated: Option<FieldGeneration>,
77        write_management: Option<FieldWriteManagement>,
78    ) -> Self {
79        Self {
80            ident,
81            value,
82            default,
83            generated,
84            write_management,
85        }
86    }
87
88    #[must_use]
89    pub const fn ident(&self) -> &'static str {
90        self.ident
91    }
92
93    #[must_use]
94    pub const fn value(&self) -> &Value {
95        &self.value
96    }
97
98    #[must_use]
99    pub const fn default(&self) -> Option<&Arg> {
100        self.default.as_ref()
101    }
102
103    #[must_use]
104    pub const fn generated(&self) -> Option<&FieldGeneration> {
105        self.generated.as_ref()
106    }
107
108    #[must_use]
109    pub const fn write_management(&self) -> Option<FieldWriteManagement> {
110        self.write_management
111    }
112}
113
114impl ValidateNode for Field {
115    fn validate(&self) -> Result<(), ErrorTree> {
116        Ok(())
117    }
118}
119
120impl VisitableNode for Field {
121    fn route_key(&self) -> String {
122        self.ident().to_string()
123    }
124
125    fn drive<V: Visitor>(&self, v: &mut V) {
126        self.value().accept(v);
127        if let Some(node) = self.default() {
128            node.accept(v);
129        }
130        if let Some(FieldGeneration::Insert(node)) = self.generated() {
131            node.accept(v);
132        }
133    }
134}