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 struct Field {
46    ident: &'static str,
47    value: Value,
48
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    default: Option<Arg>,
51}
52
53impl Field {
54    #[must_use]
55    pub const fn new(ident: &'static str, value: Value, default: Option<Arg>) -> Self {
56        Self {
57            ident,
58            value,
59            default,
60        }
61    }
62
63    #[must_use]
64    pub const fn ident(&self) -> &'static str {
65        self.ident
66    }
67
68    #[must_use]
69    pub const fn value(&self) -> &Value {
70        &self.value
71    }
72
73    #[must_use]
74    pub const fn default(&self) -> Option<&Arg> {
75        self.default.as_ref()
76    }
77}
78
79impl ValidateNode for Field {
80    fn validate(&self) -> Result<(), ErrorTree> {
81        Ok(())
82    }
83}
84
85impl VisitableNode for Field {
86    fn route_key(&self) -> String {
87        self.ident().to_string()
88    }
89
90    fn drive<V: Visitor>(&self, v: &mut V) {
91        self.value().accept(v);
92        if let Some(node) = self.default() {
93            node.accept(v);
94        }
95    }
96}