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 fn find(&self, name: &str) -> Option<KindId> {
151 self.kinds
152 .iter()
153 .position(|k| k.name == name)
154 .map(|i| KindId(i as u8))
155 }
156
157 pub fn get(&self, id: KindId) -> Option<&KindInfo> {
158 self.kinds.get(id.index())
159 }
160
161 pub fn archetype(&self, id: KindId) -> EntityKind {
163 self.get(id).map_or(EntityKind::Variable, |k| k.archetype)
164 }
165
166 pub fn len(&self) -> usize {
167 self.kinds.len()
168 }
169
170 pub fn is_empty(&self) -> bool {
171 self.kinds.is_empty()
172 }
173
174 pub fn iter(&self) -> impl ExactSizeIterator<Item = (KindId, &KindInfo)> {
176 self.kinds
177 .iter()
178 .enumerate()
179 .map(|(i, k)| (KindId(i as u8), k))
180 }
181
182 pub(crate) fn custom(&self) -> impl Iterator<Item = &KindInfo> {
184 self.kinds.iter().skip(EntityKind::ALL.len())
185 }
186}