icydb_schema/node/
entity.rs1use crate::prelude::*;
2use std::any::Any;
3
4#[derive(Clone, Debug, Serialize)]
9pub struct Entity {
10 def: Def,
11 store: &'static str,
12 primary_key: PrimaryKey,
13
14 #[serde(default, skip_serializing_if = "Option::is_none")]
15 name: Option<&'static str>,
16
17 #[serde(default, skip_serializing_if = "<[_]>::is_empty")]
18 indexes: &'static [Index],
19
20 fields: FieldList,
21 ty: Type,
22}
23
24impl Entity {
25 #[must_use]
26 pub const fn new(
27 def: Def,
28 store: &'static str,
29 primary_key: PrimaryKey,
30 name: Option<&'static str>,
31 indexes: &'static [Index],
32 fields: FieldList,
33 ty: Type,
34 ) -> Self {
35 Self {
36 def,
37 store,
38 primary_key,
39 name,
40 indexes,
41 fields,
42 ty,
43 }
44 }
45
46 #[must_use]
47 pub const fn def(&self) -> &Def {
48 &self.def
49 }
50
51 #[must_use]
52 pub const fn store(&self) -> &'static str {
53 self.store
54 }
55
56 #[must_use]
57 pub const fn primary_key(&self) -> &PrimaryKey {
58 &self.primary_key
59 }
60
61 #[must_use]
62 pub const fn name(&self) -> Option<&'static str> {
63 self.name
64 }
65
66 #[must_use]
67 pub const fn indexes(&self) -> &'static [Index] {
68 self.indexes
69 }
70
71 #[must_use]
72 pub const fn fields(&self) -> &FieldList {
73 &self.fields
74 }
75
76 #[must_use]
77 pub const fn ty(&self) -> &Type {
78 &self.ty
79 }
80
81 #[must_use]
83 pub fn get_pk_field(&self) -> Option<&Field> {
84 self.fields().get(self.primary_key().field())
85 }
86
87 #[must_use]
89 pub fn resolved_name(&self) -> &'static str {
90 self.name().unwrap_or_else(|| self.def().ident())
91 }
92}
93
94impl MacroNode for Entity {
95 fn as_any(&self) -> &dyn Any {
96 self
97 }
98}
99
100impl TypeNode for Entity {
101 fn ty(&self) -> &Type {
102 self.ty()
103 }
104}
105
106impl ValidateNode for Entity {
107 fn validate(&self) -> Result<(), ErrorTree> {
108 let mut errs = ErrorTree::new();
109 let schema = schema_read();
110
111 match schema.cast_node::<Store>(self.store()) {
113 Ok(_) => {}
114 Err(e) => errs.add(e),
115 }
116
117 errs.result()
118 }
119}
120
121impl VisitableNode for Entity {
122 fn route_key(&self) -> String {
123 self.def().path()
124 }
125
126 fn drive<V: Visitor>(&self, v: &mut V) {
127 self.def().accept(v);
128 self.fields().accept(v);
129 self.ty().accept(v);
130 }
131}