1use crate::entity::EntityKind;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
12pub struct KindId(pub(crate) u8);
13
14impl KindId {
15 pub fn index(self) -> usize {
16 self.0 as usize
17 }
18
19 pub fn of(archetype: EntityKind) -> KindId {
21 KindId(archetype as u8)
22 }
23}
24
25impl From<EntityKind> for KindId {
26 fn from(archetype: EntityKind) -> Self {
27 KindId::of(archetype)
28 }
29}
30
31#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
34pub struct TraitMask(pub u64);
35
36impl TraitMask {
37 pub const EMPTY: TraitMask = TraitMask(0);
38
39 pub fn contains(self, other: TraitMask) -> bool {
40 self.0 & other.0 == other.0
41 }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct KindInfo {
47 pub name: String,
48 pub archetype: EntityKind,
49 pub traits: TraitMask,
50 pub fm3: Option<String>,
52 pub graphql_type: Option<String>,
56 pub graphql_root: Option<String>,
59}
60
61#[derive(Debug, Clone, PartialEq, Eq)]
64pub struct Metamodel {
65 kinds: Vec<KindInfo>,
66}
67
68impl Default for Metamodel {
69 fn default() -> Self {
70 Metamodel {
71 kinds: EntityKind::ALL
72 .iter()
73 .map(|&a| KindInfo {
74 name: a.as_str().to_string(),
75 archetype: a,
76 traits: TraitMask::EMPTY,
77 fm3: None,
78 graphql_type: None,
79 graphql_root: None,
80 })
81 .collect(),
82 }
83 }
84}
85
86impl Metamodel {
87 pub fn define(
91 &mut self,
92 name: &str,
93 archetype: EntityKind,
94 traits: TraitMask,
95 fm3: Option<&str>,
96 ) -> KindId {
97 if let Some(id) = self.find(name) {
98 return id;
99 }
100 match u8::try_from(self.kinds.len()) {
101 Ok(i) => {
102 self.kinds.push(KindInfo {
103 name: name.to_string(),
104 archetype,
105 traits,
106 fm3: fm3.map(str::to_string),
107 graphql_type: None,
108 graphql_root: None,
109 });
110 KindId(i)
111 }
112 Err(_) => KindId::of(archetype),
113 }
114 }
115
116 pub fn set_traits(&mut self, id: KindId, traits: TraitMask) -> bool {
119 match self.kinds.get_mut(id.index()) {
120 Some(k) => {
121 k.traits = traits;
122 true
123 }
124 None => false,
125 }
126 }
127
128 pub fn set_concept(
132 &mut self,
133 id: KindId,
134 graphql_type: Option<&str>,
135 graphql_root: Option<&str>,
136 ) -> bool {
137 if id.index() < EntityKind::ALL.len() {
138 return false;
139 }
140 match self.kinds.get_mut(id.index()) {
141 Some(k) => {
142 k.graphql_type = graphql_type.map(str::to_string);
143 k.graphql_root = graphql_root.map(str::to_string);
144 true
145 }
146 None => false,
147 }
148 }
149
150 pub(crate) fn from_kinds(kinds: Vec<KindInfo>) -> Option<Metamodel> {
153 let defaults_ok = kinds.len() >= EntityKind::ALL.len()
154 && kinds.len() <= 256
155 && EntityKind::ALL
156 .iter()
157 .zip(&kinds)
158 .all(|(a, k)| k.archetype == *a);
159 defaults_ok.then_some(Metamodel { kinds })
160 }
161
162 pub fn find(&self, name: &str) -> Option<KindId> {
163 self.kinds
164 .iter()
165 .position(|k| k.name == name)
166 .map(|i| KindId(i as u8))
167 }
168
169 pub fn get(&self, id: KindId) -> Option<&KindInfo> {
170 self.kinds.get(id.index())
171 }
172
173 pub fn archetype(&self, id: KindId) -> EntityKind {
175 self.get(id).map_or(EntityKind::Variable, |k| k.archetype)
176 }
177
178 pub fn len(&self) -> usize {
179 self.kinds.len()
180 }
181
182 pub fn is_empty(&self) -> bool {
183 self.kinds.is_empty()
184 }
185
186 pub fn iter(&self) -> impl ExactSizeIterator<Item = (KindId, &KindInfo)> {
188 self.kinds
189 .iter()
190 .enumerate()
191 .map(|(i, k)| (KindId(i as u8), k))
192 }
193
194 pub(crate) fn custom(&self) -> impl Iterator<Item = &KindInfo> {
196 self.kinds.iter().skip(EntityKind::ALL.len())
197 }
198}