Skip to main content

icydb_model/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, name: &str) -> Option<&Field> {
26        self.fields.iter().find(|field| field.name() == name)
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    name: &'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        name: &'static str,
74        value: Value,
75        default: Option<Arg>,
76        generated: Option<FieldGeneration>,
77        write_management: Option<FieldWriteManagement>,
78    ) -> Self {
79        Self {
80            name,
81            value,
82            default,
83            generated,
84            write_management,
85        }
86    }
87
88    /// Borrow the current declared field name.
89    #[must_use]
90    pub const fn name(&self) -> &'static str {
91        self.name
92    }
93
94    #[must_use]
95    pub const fn value(&self) -> &Value {
96        &self.value
97    }
98
99    #[must_use]
100    pub const fn default(&self) -> Option<&Arg> {
101        self.default.as_ref()
102    }
103
104    #[must_use]
105    pub const fn generated(&self) -> Option<&FieldGeneration> {
106        self.generated.as_ref()
107    }
108
109    #[must_use]
110    pub const fn write_management(&self) -> Option<FieldWriteManagement> {
111        self.write_management
112    }
113}
114
115impl ValidateNode for Field {
116    fn validate(&self) -> Result<(), ErrorTree> {
117        let mut errs = ErrorTree::new();
118        validate_source_name(
119            &mut errs,
120            "field",
121            self.name(),
122            icydb_schema::FieldSourceKey::try_new,
123        );
124        errs.result()
125    }
126}
127
128impl VisitableNode for Field {
129    fn route_key(&self) -> String {
130        self.name().to_string()
131    }
132
133    fn drive<V: Visitor>(&self, v: &mut V) {
134        self.value().accept(v);
135        if let Some(node) = self.default() {
136            node.accept(v);
137        }
138        if let Some(FieldGeneration::Insert(node)) = self.generated() {
139            node.accept(v);
140        }
141    }
142}