Skip to main content

icydb_schema/node/
item.rs

1use crate::prelude::*;
2use std::ops::Not;
3
4///
5/// Item
6///
7
8#[derive(Clone, Debug, Serialize)]
9pub struct Item {
10    pub target: ItemTarget,
11
12    #[serde(default, skip_serializing_if = "Option::is_none")]
13    pub relation: Option<&'static str>,
14
15    #[serde(default, skip_serializing_if = "<[_]>::is_empty")]
16    pub validators: &'static [TypeValidator],
17
18    #[serde(default, skip_serializing_if = "<[_]>::is_empty")]
19    pub sanitizers: &'static [TypeSanitizer],
20
21    #[serde(default, skip_serializing_if = "Not::not")]
22    pub indirect: bool,
23}
24
25impl Item {
26    #[must_use]
27    pub const fn is_relation(&self) -> bool {
28        self.relation.is_some()
29    }
30}
31
32impl ValidateNode for Item {
33    fn validate(&self) -> Result<(), ErrorTree> {
34        let mut errs = ErrorTree::new();
35        let schema = schema_read();
36
37        match &self.target {
38            ItemTarget::Is(path) => {
39                // cannot be an entity
40                if schema.check_node_as::<Entity>(path).is_ok() {
41                    err!(errs, "a non-relation Item cannot reference an Entity");
42                }
43            }
44
45            ItemTarget::Primitive(_) => {}
46        }
47
48        // relation
49        if let Some(relation) = &self.relation {
50            // Step 1: Ensure the relation path exists and is an Entity
51            match schema.cast_node::<Entity>(relation) {
52                Ok(entity) => {
53                    // Step 2: Get target of the relation entity (usually from its primary key field)
54                    if let Some(primary_field) = entity.get_pk_field() {
55                        let relation_target = &primary_field.value.item.target;
56
57                        // Step 3: Compare to self.target()
58                        if &self.target != relation_target {
59                            err!(
60                                errs,
61                                "relation target type mismatch: expected {:?}, found {:?}",
62                                relation_target,
63                                self.target
64                            );
65                        }
66                    } else {
67                        err!(
68                            errs,
69                            "relation entity '{relation}' missing primary key field '{0}'",
70                            entity.primary_key
71                        );
72                    }
73                }
74                Err(_) => {
75                    err!(errs, "relation entity '{relation}' not found");
76                }
77            }
78        }
79
80        errs.result()
81    }
82}
83
84impl VisitableNode for Item {
85    fn drive<V: Visitor>(&self, v: &mut V) {
86        for node in self.validators {
87            node.accept(v);
88        }
89    }
90}
91
92///
93/// ItemTarget
94///
95
96#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
97pub enum ItemTarget {
98    Is(&'static str),
99    Primitive(Primitive),
100}