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        // Phase 1: validate target shape.
38        match &self.target {
39            ItemTarget::Is(path) => {
40                // cannot be an entity
41                if schema.check_node_as::<Entity>(path).is_ok() {
42                    err!(errs, "a non-relation Item cannot reference an Entity");
43                }
44            }
45
46            ItemTarget::Primitive(_) => {}
47        }
48
49        // Phase 2: validate relation target compatibility.
50        if let Some(relation) = &self.relation {
51            // Step 1: Ensure the relation path exists and is an Entity
52            match schema.cast_node::<Entity>(relation) {
53                Ok(entity) => {
54                    // Step 2: Get target of the relation entity (usually from its primary key field)
55                    if let Some(primary_field) = entity.get_pk_field() {
56                        let relation_target = &primary_field.value.item.target;
57
58                        // Step 3: Compare to self.target()
59                        if &self.target != relation_target {
60                            err!(
61                                errs,
62                                "relation target type mismatch: expected {:?}, found {:?}",
63                                relation_target,
64                                self.target
65                            );
66                        }
67                    } else {
68                        err!(
69                            errs,
70                            "relation entity '{relation}' missing primary key field '{0}'",
71                            entity.primary_key
72                        );
73                    }
74                }
75                Err(_) => {
76                    err!(errs, "relation entity '{relation}' not found");
77                }
78            }
79        }
80
81        errs.result()
82    }
83}
84
85impl VisitableNode for Item {
86    fn drive<V: Visitor>(&self, v: &mut V) {
87        for node in self.validators {
88            node.accept(v);
89        }
90    }
91}
92
93///
94/// ItemTarget
95///
96
97#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
98pub enum ItemTarget {
99    Is(&'static str),
100    Primitive(Primitive),
101}