1use std::collections::{HashMap, HashSet};
2use std::hash::{BuildHasherDefault, Hasher};
3
4#[derive(Default, Clone, Copy)]
18pub struct FastHasher {
19 hash: u64,
20}
21
22const SEED: u64 = 0xf135_7aea_2e62_a9c5;
23
24impl FastHasher {
25 #[inline]
26 fn add(&mut self, word: u64) {
27 self.hash = self.hash.wrapping_add(word).wrapping_mul(SEED);
28 }
29}
30
31impl Hasher for FastHasher {
32 #[inline]
33 fn write(&mut self, bytes: &[u8]) {
34 let mut chunks = bytes.chunks_exact(8);
35 for c in &mut chunks {
36 self.add(u64::from_le_bytes(c.try_into().expect("chunk of 8")));
37 }
38 let rest = chunks.remainder();
39 if !rest.is_empty() {
40 let mut buf = [0u8; 8];
41 buf[..rest.len()].copy_from_slice(rest);
42 self.add(u64::from_le_bytes(buf));
43 }
44 }
45 #[inline]
46 fn write_u8(&mut self, i: u8) {
47 self.add(i as u64);
48 }
49 #[inline]
50 fn write_u32(&mut self, i: u32) {
51 self.add(i as u64);
52 }
53 #[inline]
54 fn write_u64(&mut self, i: u64) {
55 self.add(i);
56 }
57 #[inline]
58 fn write_usize(&mut self, i: usize) {
59 self.add(i as u64);
60 }
61 #[inline]
62 fn finish(&self) -> u64 {
63 self.hash.rotate_left(26)
64 }
65}
66
67pub type FastBuildHasher = BuildHasherDefault<FastHasher>;
68pub type FastMap<K, V> = HashMap<K, V, FastBuildHasher>;
69pub type FastSet<T> = HashSet<T, FastBuildHasher>;
70pub type FieldMap = FastMap<&'static str, FieldValue>;
74
75pub fn intern(key: &str) -> &'static str {
80 use std::cell::RefCell;
81 use std::sync::{Mutex, OnceLock};
82 thread_local! {
83 static LOCAL: RefCell<FastSet<&'static str>> = RefCell::new(FastSet::default());
84 }
85 static GLOBAL: OnceLock<Mutex<FastSet<&'static str>>> = OnceLock::new();
86 if let Some(k) = LOCAL.with(|l| l.borrow().get(key).copied()) {
87 return k;
88 }
89 let k = {
90 let mut global = GLOBAL.get_or_init(Default::default).lock().unwrap();
91 match global.get(key) {
92 Some(k) => *k,
93 None => {
94 let k: &'static str = Box::leak(key.to_owned().into_boxed_str());
95 global.insert(k);
96 k
97 }
98 }
99 };
100 LOCAL.with(|l| l.borrow_mut().insert(k));
101 k
102}
103
104#[derive(Debug, Clone)]
105pub enum FieldValue {
106 Text(String),
107 TextList(Vec<String>),
110 Resource(String),
111 ResourceList(Vec<String>),
112}
113
114#[derive(Debug, Clone)]
124pub struct Element {
125 class: &'static ClassDef,
126 mrid: String,
127 fields: FieldMap,
128 duplicate_fields: FastSet<&'static str>,
131 langs: Option<Box<Langs>>,
135 also: Option<Box<Types>>,
141 described: bool,
145}
146
147type Langs = Vec<(&'static str, Box<str>)>;
149
150type Types = Vec<&'static ClassDef>;
152
153impl Element {
154 pub fn new(class: &'static ClassDef, mrid: String) -> Self {
155 Self { class, mrid, fields: FieldMap::default(), duplicate_fields: FastSet::default(), langs: None, also: None, described: false }
156 }
157
158 pub fn with_fields(class: &'static ClassDef, mrid: String, fields: FieldMap) -> Self {
159 Self { class, mrid, fields, duplicate_fields: FastSet::default(), langs: None, also: None, described: false }
160 }
161
162 pub fn class(&self) -> &'static ClassDef {
163 self.class
164 }
165
166 pub fn types(&self) -> impl Iterator<Item = &'static ClassDef> + '_ {
169 std::iter::once(self.class).chain(self.also.iter().flat_map(|v| v.iter().copied()))
170 }
171
172 pub(crate) fn add_type(&mut self, other: &'static ClassDef, reg: &TypeRegistry) -> bool {
177 if self.types().any(|c| c.qualified == other.qualified) {
178 return false;
179 }
180 let promote = reg.chain(other).iter().any(|c| c.qualified == self.class.qualified);
181 let kept = if promote { std::mem::replace(&mut self.class, other) } else { other };
182 self.also.get_or_insert_with(Default::default).push(kept);
183 promote
184 }
185
186 pub fn defined(&self) -> bool {
189 !self.described
190 }
191
192 pub fn mark_described(&mut self) {
194 self.described = true;
195 }
196
197 pub fn mrid(&self) -> &str {
198 &self.mrid
199 }
200
201 pub fn type_name(&self) -> &'static str {
204 self.class.qualified
205 }
206
207 pub fn local_name(&self) -> &'static str {
209 self.class.local
210 }
211
212 pub fn type_ns(&self) -> &'static str {
214 self.class.ns
215 }
216
217 pub fn fields(&self) -> &FieldMap {
218 &self.fields
219 }
220
221 pub fn duplicate_fields(&self) -> &FastSet<&'static str> {
222 &self.duplicate_fields
223 }
224
225 pub fn langs(&self, key: &str) -> impl Iterator<Item = &str> {
229 self.langs.iter().flat_map(|l| l.iter()).filter(move |(k, _)| *k == key).map(|(_, l)| &**l)
230 }
231
232 pub fn add_lang(&mut self, key: &'static str, lang: &str) {
234 self.langs.get_or_insert_with(Default::default).push((key, lang.into()));
235 }
236
237 pub fn get(&self, attr: &str) -> Option<&FieldValue> {
238 self.fields.get(attr)
239 }
240
241 pub fn get_str(&self, attr: &str) -> Option<&str> {
242 match self.fields.get(attr) {
243 Some(FieldValue::Text(s)) => Some(s.as_str()),
244 Some(FieldValue::TextList(v)) => v.first().map(String::as_str),
245 _ => None,
246 }
247 }
248
249 pub fn get_f64(&self, attr: &str) -> Option<f64> {
250 self.get_str(attr)?.parse().ok()
251 }
252
253 pub fn get_bool(&self, attr: &str) -> Option<bool> {
254 match self.get_str(attr)? {
255 "true" | "1" => Some(true),
256 "false" | "0" => Some(false),
257 _ => None,
258 }
259 }
260
261 pub fn get_refs(&self, attr: &str) -> &[String] {
267 match self.fields.get(attr) {
268 Some(FieldValue::Resource(s)) => std::slice::from_ref(s),
269 Some(FieldValue::ResourceList(v)) => v.as_slice(),
270 _ => &[],
271 }
272 }
273
274 pub fn get_ref(&self, attr: &str) -> Option<&str> {
276 self.get_refs(attr).first().map(String::as_str)
277 }
278
279 pub fn add_field(&mut self, key: &'static str, val: FieldValue) {
282 match &val {
283 FieldValue::Resource(new_ref) => match self.fields.get_mut(key) {
284 Some(FieldValue::ResourceList(list)) => {
285 list.push(new_ref.clone());
286 self.duplicate_fields.insert(key);
287 return;
288 }
289 Some(existing @ FieldValue::Resource(_)) => {
290 let FieldValue::Resource(old) = std::mem::replace(existing, FieldValue::ResourceList(Vec::new())) else {
291 unreachable!()
292 };
293 *existing = FieldValue::ResourceList(vec![old, new_ref.clone()]);
294 self.duplicate_fields.insert(key);
295 return;
296 }
297 _ => {}
298 },
299 FieldValue::Text(new_text) => match self.fields.get_mut(key) {
300 Some(FieldValue::TextList(list)) => {
301 list.push(new_text.clone());
302 self.duplicate_fields.insert(key);
303 return;
304 }
305 Some(existing @ FieldValue::Text(_)) => {
306 let FieldValue::Text(old) = std::mem::replace(existing, FieldValue::TextList(Vec::new())) else {
307 unreachable!()
308 };
309 *existing = FieldValue::TextList(vec![old, new_text.clone()]);
310 self.duplicate_fields.insert(key);
311 return;
312 }
313 _ => {}
314 },
315 _ => {
316 if self.fields.contains_key(key) {
317 self.duplicate_fields.insert(key);
318 }
319 }
320 }
321 self.fields.insert(key, val);
322 }
323
324 pub fn merge_from(&mut self, other: &Element) {
327 self.described &= other.described;
328 for (k, v) in &other.fields {
329 match v {
330 FieldValue::ResourceList(new_list) => match self.fields.get_mut(k) {
331 Some(FieldValue::ResourceList(existing)) => existing.extend(new_list.iter().cloned()),
332 _ => {
333 self.fields.insert(*k, v.clone());
334 }
335 },
336 _ => {
337 self.fields.insert(*k, v.clone());
338 if let Some(l) = self.langs.as_mut() {
340 l.retain(|(key, _)| key != k);
341 }
342 for lang in other.langs(k) {
343 self.add_lang(k, lang);
344 }
345 }
346 }
347 }
348 }
349
350 pub fn to_json_value(&self) -> serde_json::Value {
353 let mut map = serde_json::Map::with_capacity(self.fields.len() + 1);
354 map.insert("id".to_string(), serde_json::Value::String(self.mrid.clone()));
355 for (k, v) in &self.fields {
356 map.insert(k.to_string(), field_to_json(v));
357 }
358 serde_json::Value::Object(map)
359 }
360
361 pub fn from_json(
365 class: &'static ClassDef,
366 reg: &TypeRegistry,
367 value: &serde_json::Value,
368 ) -> Result<Self, String> {
369 let obj = value.as_object().ok_or("element is not a JSON object")?;
370 let mrid = obj.get("id").and_then(|v| v.as_str()).ok_or("element has no \"id\"")?.to_string();
371 let mut fields = FieldMap::default();
372 for (k, v) in obj {
373 if k == "id" || k == "_type" {
374 continue;
375 }
376 let is_ref = reg.attr(class, k).is_some_and(|a| a.kind != AttrKind::Literal);
377 let value = match v {
378 serde_json::Value::String(s) if is_ref => FieldValue::Resource(s.clone()),
379 serde_json::Value::String(s) => FieldValue::Text(s.clone()),
380 serde_json::Value::Array(items) => {
381 let items: Vec<String> = items
382 .iter()
383 .map(|i| i.as_str().map(str::to_string).ok_or(format!("{k}: list item is not a string")))
384 .collect::<Result<_, _>>()?;
385 if is_ref { FieldValue::ResourceList(items) } else { FieldValue::TextList(items) }
386 }
387 other => return Err(format!("{k}: expected a string or an array, got {other}")),
388 };
389 fields.insert(reg.field_key(class, k), value);
390 }
391 Ok(Self::with_fields(class, mrid, fields))
392 }
393}
394
395fn field_to_json(v: &FieldValue) -> serde_json::Value {
396 match v {
397 FieldValue::Text(s) | FieldValue::Resource(s) => serde_json::Value::String(s.clone()),
398 FieldValue::TextList(v) | FieldValue::ResourceList(v) => {
399 serde_json::Value::Array(v.iter().cloned().map(serde_json::Value::String).collect())
400 }
401 }
402}
403
404#[derive(Debug, Clone, Copy, PartialEq, Eq)]
408pub enum AttrKind {
409 Literal,
411 Association,
413 Enum,
415}
416
417#[derive(Debug)]
419pub struct AttrDef {
420 pub id: &'static str,
422 pub ns: &'static str,
424 pub kind: AttrKind,
425 pub range: &'static str,
428 pub is_list: bool,
429 pub used: bool,
433 pub xsd: &'static str,
436 pub value_ns: &'static str,
439 pub origins: &'static [&'static str],
441 pub comment: &'static str,
444}
445
446#[derive(Debug)]
450pub struct Schema {
451 pub classes: &'static [ClassDef],
452 pub profiles: &'static [(&'static str, &'static str)],
455 pub namespaces: &'static [(&'static str, &'static str)],
457 pub enums: &'static [EnumDef],
459}
460
461impl Schema {
462 pub fn enumeration(&self, name: &str) -> Option<&'static EnumDef> {
464 let enums: &'static [EnumDef] = self.enums;
465 enums.binary_search_by_key(&name, |e| e.local).ok().map(|i| &enums[i])
466 }
467
468 pub fn enum_value(&self, id: &str) -> Option<(&'static EnumDef, &'static EnumValueDef)> {
471 let e = self.enumeration(id.split_once('.')?.0)?;
472 Some((e, e.values.iter().find(|v| v.id == id)?))
473 }
474}
475
476#[derive(Debug)]
478pub struct EnumDef {
479 pub ns: &'static str,
480 pub local: &'static str,
481 pub comment: &'static str,
483 pub values: &'static [EnumValueDef],
485}
486
487#[derive(Debug)]
489pub struct EnumValueDef {
490 pub id: &'static str,
492 pub comment: &'static str,
494}
495
496#[derive(Debug)]
498pub struct ClassDef {
499 pub ns: &'static str,
500 pub local: &'static str,
501 pub qualified: &'static str,
503 pub super_class: Option<usize>,
505 pub concrete: bool,
506 pub attrs: &'static [AttrDef],
510 pub origins: &'static [&'static str],
511 pub comment: &'static str,
514}
515
516impl ClassDef {
517 pub fn attr(&self, id: &str) -> Option<&'static AttrDef> {
519 self.attrs.iter().find(|a| a.id == id)
520 }
521}
522
523#[derive(Default)]
531pub struct TypeRegistry {
532 by_ns: FastMap<&'static str, FastMap<&'static str, &'static ClassDef>>,
533 bare: FastMap<&'static str, &'static ClassDef>,
534 by_type_name: FastMap<&'static str, &'static ClassDef>,
535 declared: FastMap<&'static str, FastMap<&'static str, &'static AttrDef>>,
538 chain: FastMap<&'static str, Vec<&'static ClassDef>>,
540 families: Vec<FamilyTable>,
542}
543
544struct FamilyTable {
547 id: &'static str,
548 prefix: &'static str,
549 schema: &'static Schema,
550 attrs: FastMap<&'static str, &'static AttrDef>,
553}
554
555impl TypeRegistry {
556 pub fn new() -> Self {
557 Self::default()
558 }
559
560 pub fn add_family(&mut self, id: &'static str, schema: &'static Schema, bare_fallback: bool) {
564 let classes = schema.classes;
565 let prefix = classes
566 .first()
567 .and_then(|c| c.qualified.strip_suffix(c.local))
568 .unwrap_or("");
569 let mut by_id = FastMap::default();
570 for class in classes {
571 for a in class.attrs {
572 by_id.entry(a.id).or_insert(a);
573 }
574 }
575 self.families.push(FamilyTable { id, prefix, schema, attrs: by_id });
576 for class in classes {
577 self.by_ns.entry(class.ns).or_default().insert(class.local, class);
578 self.by_type_name.insert(class.qualified, class);
579 if bare_fallback {
580 self.bare.insert(class.local, class);
581 }
582 let mut attrs = FastMap::default();
583 let mut chain = Vec::new();
584 let mut next = Some(class);
585 while let Some(c) = next {
586 for a in c.attrs {
587 attrs.entry(a.id).or_insert(a);
588 }
589 chain.push(c);
590 next = c.super_class.map(|i| &classes[i]);
591 }
592 chain.reverse();
593 self.declared.insert(class.qualified, attrs);
594 self.chain.insert(class.qualified, chain);
595 }
596 }
597
598 pub fn ns_table(&self, ns: &str) -> Option<&FastMap<&'static str, &'static ClassDef>> {
600 self.by_ns.get(ns)
601 }
602
603 pub fn bare(&self) -> &FastMap<&'static str, &'static ClassDef> {
604 &self.bare
605 }
606
607 pub fn by_type_name(&self, name: &str) -> Option<&'static ClassDef> {
608 self.by_type_name.get(name).copied()
609 }
610
611 pub fn attr(&self, class: &ClassDef, id: &str) -> Option<&'static AttrDef> {
613 self.declared.get(class.qualified)?.get(id).copied()
614 }
615
616 pub fn declared(&self, class: &ClassDef) -> Option<&FastMap<&'static str, &'static AttrDef>> {
619 self.declared.get(class.qualified)
620 }
621
622 pub fn field_key(&self, class: &ClassDef, id: &str) -> &'static str {
624 self.attr(class, id).map_or_else(|| intern(id), |a| a.id)
625 }
626
627 pub fn chain(&self, class: &ClassDef) -> &[&'static ClassDef] {
629 self.chain.get(class.qualified).map_or(&[], Vec::as_slice)
630 }
631
632 pub fn concrete_descendants(&self, qualified: &str) -> Vec<&'static str> {
635 let mut out: Vec<&'static str> = self
636 .chain
637 .iter()
638 .filter(|(_, chain)| chain.iter().any(|c| c.qualified == qualified))
639 .filter_map(|(q, _)| self.by_type_name.get(q).filter(|c| c.concrete).map(|c| c.qualified))
640 .collect();
641 out.sort_unstable();
642 out
643 }
644
645 pub fn attrs(&self, class: &ClassDef) -> impl Iterator<Item = &'static AttrDef> + '_ {
647 self.declared.get(class.qualified).into_iter().flat_map(|m| m.values().copied())
648 }
649
650 pub fn schema(&self, family: &str) -> Option<&'static Schema> {
652 self.family(family).map(|f| f.schema)
653 }
654
655 fn family(&self, id: &str) -> Option<&FamilyTable> {
656 self.families.iter().find(|f| f.id == id)
657 }
658
659 pub fn enum_value(&self, class: &ClassDef, id: &str) -> Option<(&'static EnumDef, &'static EnumValueDef)> {
662 self.schema(self.family_of(class.qualified)?)?.enum_value(id)
663 }
664
665 pub fn family_of(&self, type_name: &str) -> Option<&'static str> {
667 self.families
669 .iter()
670 .filter(|f| type_name.starts_with(f.prefix))
671 .max_by_key(|f| f.prefix.len())
672 .map(|f| f.id)
673 }
674
675 pub fn family_attr(&self, family: &str, id: &str) -> Option<&'static AttrDef> {
678 self.family(family)?.attrs.get(id).copied()
679 }
680
681 pub fn attr_of(&self, class: &ClassDef, key: &str) -> Option<&'static AttrDef> {
684 self.attr(class, key)
685 .or_else(|| self.family_attr(self.family_of(class.qualified)?, key))
686 }
687}