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, 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    source_key: &'static str,
58    ident: &'static str,
59    value: Value,
60
61    #[serde(skip_serializing_if = "Option::is_none")]
62    default: Option<Arg>,
63
64    #[serde(skip_serializing_if = "Option::is_none")]
65    generated: Option<FieldGeneration>,
66
67    #[serde(skip_serializing_if = "Option::is_none")]
68    write_management: Option<FieldWriteManagement>,
69}
70
71impl Field {
72    #[must_use]
73    pub const fn new(
74        source_key: &'static str,
75        ident: &'static str,
76        value: Value,
77        default: Option<Arg>,
78        generated: Option<FieldGeneration>,
79        write_management: Option<FieldWriteManagement>,
80    ) -> Self {
81        Self {
82            source_key,
83            ident,
84            value,
85            default,
86            generated,
87            write_management,
88        }
89    }
90
91    /// Borrow the immutable field source key.
92    #[must_use]
93    pub const fn source_key(&self) -> &'static str {
94        self.source_key
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) -> &Value {
104        &self.value
105    }
106
107    #[must_use]
108    pub const fn default(&self) -> Option<&Arg> {
109        self.default.as_ref()
110    }
111
112    #[must_use]
113    pub const fn generated(&self) -> Option<&FieldGeneration> {
114        self.generated.as_ref()
115    }
116
117    #[must_use]
118    pub const fn write_management(&self) -> Option<FieldWriteManagement> {
119        self.write_management
120    }
121}
122
123impl ValidateNode for Field {
124    fn validate(&self) -> Result<(), ErrorTree> {
125        let mut errs = ErrorTree::new();
126        validate_source_key(
127            &mut errs,
128            "field",
129            self.source_key(),
130            icydb_schema::FieldSourceKey::try_new,
131        );
132        errs.result()
133    }
134}
135
136impl VisitableNode for Field {
137    fn route_key(&self) -> String {
138        self.ident().to_string()
139    }
140
141    fn drive<V: Visitor>(&self, v: &mut V) {
142        self.value().accept(v);
143        if let Some(node) = self.default() {
144            node.accept(v);
145        }
146        if let Some(FieldGeneration::Insert(node)) = self.generated() {
147            node.accept(v);
148        }
149    }
150}