Skip to main content

cimmodel/
base.rs

1use std::collections::{HashMap, HashSet};
2use std::hash::{BuildHasherDefault, Hasher};
3
4// --- Hashing -----------------------------------------------------------------
5
6/// A fast, non-cryptographic hasher for the decoder's maps.
7///
8/// Every field read during validation and every element lookup goes through a
9/// `String`-keyed map, and std's SipHash made those lookups a measurable share
10/// of a validation run. This is the multiply-rotate scheme of FxHash (as in
11/// `rustc-hash` 2): a word at a time, with the high bits rotated down at the
12/// end because hashbrown takes its bucket index from the low bits.
13///
14/// Written out rather than imported so the core crates carry no dependency for
15/// it. Not resistant to deliberately colliding keys, which is acceptable for a
16/// tool that reads grid model files rather than serving untrusted requests.
17#[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>;
70/// An element's fields, keyed by `Class.attr`. Keys are `&'static`: a declared
71/// attribute's key is its id in the class table, any other goes through
72/// [`intern`], so a key is allocated once per process rather than per field.
73pub type FieldMap = FastMap<&'static str, FieldValue>;
74
75/// The one `&'static` copy of a field key the class tables do not hold — an
76/// attribute the element's class does not declare, as written in the file or
77/// set from JSON. Each distinct key is leaked once and kept for the process;
78/// there are as many as distinct undeclared attribute names, not fields.
79pub 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    /// Repeated text element within one parsed element (e.g. the multiple
108    /// `md:Model.profile` entries of a combined EQ+SC file header).
109    TextList(Vec<String>),
110    Resource(String),
111    ResourceList(Vec<String>),
112}
113
114// --- Elements ----------------------------------------------------------------
115
116/// One decoded CIM object: its class, its mRID, and its fields as written.
117///
118/// Every family decodes into this one type. The fields are the document's
119/// own, keyed by `Class.attr` (the XML element's local name), with values
120/// unparsed: what a class declares lives in its [`ClassDef`], not in a
121/// generated type, so an attribute the class does not declare and a value
122/// that does not parse are kept rather than dropped.
123#[derive(Debug, Clone)]
124pub struct Element {
125    class: &'static ClassDef,
126    mrid: String,
127    fields: FieldMap,
128    /// Fields assigned more than once within the element, which `sh:maxCount`
129    /// reads.
130    duplicate_fields: FastSet<&'static str>,
131    /// The `xml:lang` of each text value written with one, by field, in the
132    /// order read. `None` until a value carries one, so an element without
133    /// language tags — nearly all of them — pays a pointer.
134    langs: Option<Box<Langs>>,
135    /// The other classes files wrote the object under, when merged datasets
136    /// disagree — an SSH file's `cim:Equipment` for an EQ file's
137    /// `cim:ACLineSegment`. In RDF the object has every one of those types;
138    /// `class` is the most specific. `None` for nearly every element, so it
139    /// costs a pointer, like `langs`.
140    also: Option<Box<Types>>,
141    /// Every file that wrote the object only described it (`rdf:about`) and
142    /// none defined it (`rdf:ID`): an SSH file's `inService` for equipment
143    /// the EQ file defines. Elements not decoded from RDF/XML are defined.
144    described: bool,
145}
146
147/// `(field key, xml:lang)` for each tagged text value of an element.
148type Langs = Vec<(&'static str, Box<str>)>;
149
150/// The classes besides its own a merged element was written under.
151type 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    /// Every class the object is typed as: its class, then any other a merged
167    /// file wrote it under ([`CimDataset::merge`](crate::CimDataset::merge)).
168    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    /// Record that a file also typed the object as `other`. Becomes its class
173    /// when it is a subclass of the current one (field keys name each
174    /// attribute's declaring class, so they stay valid); otherwise it is kept
175    /// beside it. Returns whether the class changed.
176    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    /// Whether some file defined the object (`rdf:ID`) rather than only
187    /// describing one defined elsewhere (`rdf:about`).
188    pub fn defined(&self) -> bool {
189        !self.described
190    }
191
192    /// Record that the file only described the object (`rdf:about`).
193    pub fn mark_described(&mut self) {
194        self.described = true;
195    }
196
197    pub fn mrid(&self) -> &str {
198        &self.mrid
199    }
200
201    /// Family-qualified class name, e.g. `"Terminal"` (CGMES) or
202    /// `"nc:Contingency"`. This is the `CimDataset::by_type` key.
203    pub fn type_name(&self) -> &'static str {
204        self.class.qualified
205    }
206
207    /// Bare CIM class name, without the family qualifier.
208    pub fn local_name(&self) -> &'static str {
209        self.class.local
210    }
211
212    /// RDF namespace IRI of the declaring class, trailing delimiter included.
213    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    /// The `xml:lang` tags of `key`'s text values, one per value that had
226    /// one. A value written without a tag has no entry, so a field whose
227    /// values outnumber its tags has an untagged value.
228    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    /// Record the `xml:lang` of a text value just added to `key`.
233    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    /// Referenced MRIDs.
262    ///
263    /// Always a slice, even where the schema says the association is 1:1 — CGMES
264    /// has widened 1:1 to 1:many between versions, and a caller written against
265    /// a slice does not have to change when that happens.
266    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    /// First referenced MRID, for associations known to be single-valued.
275    pub fn get_ref(&self, attr: &str) -> Option<&str> {
276        self.get_refs(attr).first().map(String::as_str)
277    }
278
279    /// Add one value as the decoder reads it: a repeated key becomes a list
280    /// and is recorded in [`Element::duplicate_fields`].
281    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    /// Combine another file's view of the same object: a later scalar wins,
325    /// reference lists are joined. Duplicate tracking stays per file.
326    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                    // The later file's text replaced ours, tags included.
339                    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    /// `{"id": mRID, "Class.attr": value, ...}`: a value is a string, a
351    /// repeated one an array of strings.
352    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    /// The inverse of [`Element::to_json_value`]. Whether a string is a
362    /// reference or text comes from the attribute's declaration; an attribute
363    /// the class does not declare is read as text.
364    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// --- Classes -----------------------------------------------------------------
405
406/// How an attribute's value is carried in RDF/XML.
407#[derive(Debug, Clone, Copy, PartialEq, Eq)]
408pub enum AttrKind {
409    /// A literal, written as element text.
410    Literal,
411    /// A reference to another object, written as `rdf:resource`.
412    Association,
413    /// An enumeration value, written as `rdf:resource` to a vocabulary IRI.
414    Enum,
415}
416
417/// One attribute of a class, as the schema declares it.
418#[derive(Debug)]
419pub struct AttrDef {
420    /// `Class.attr`, matching the key in [`Element::fields`].
421    pub id: &'static str,
422    /// The attribute's RDF namespace: its predicate IRI is `ns` + `id`.
423    pub ns: &'static str,
424    pub kind: AttrKind,
425    /// The CIM datatype of a literal; the class an association or enumeration
426    /// points to.
427    pub range: &'static str,
428    pub is_list: bool,
429    /// Whether a profile exchanges this end of the association — `false` for the
430    /// inverse ends the vocabularies declare but no dataset writes. Literals and
431    /// enumerations are always used.
432    pub used: bool,
433    /// The XSD datatype of a literal, as the local name after
434    /// `http://www.w3.org/2001/XMLSchema#` (`"double"`); `""` otherwise.
435    pub xsd: &'static str,
436    /// The namespace of an enumeration's values (the decoder keeps a value after
437    /// its `#`); `""` otherwise.
438    pub value_ns: &'static str,
439    /// Profile codes that carry this attribute.
440    pub origins: &'static [&'static str],
441    /// The vocabulary's `rdfs:comment`, HTML removed and on one line; `""`
442    /// if it has none.
443    pub comment: &'static str,
444}
445
446/// What one family's schema says, as a table: its classes, the profiles it
447/// defines and the namespaces its vocabularies bind. Generated by `cimgen`, or
448/// read from RDFS at runtime (`schema_source`).
449#[derive(Debug)]
450pub struct Schema {
451    pub classes: &'static [ClassDef],
452    /// Profile code → profile URI, the value a dataset's header declares; sorted
453    /// by code.
454    pub profiles: &'static [(&'static str, &'static str)],
455    /// XML prefix → namespace IRI; sorted by prefix.
456    pub namespaces: &'static [(&'static str, &'static str)],
457    /// Enumerations, sorted by name.
458    pub enums: &'static [EnumDef],
459}
460
461impl Schema {
462    /// The enumeration called `name` (`WindingConnection`).
463    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    /// The enumeration value `id` names, as the decoder keeps it
469    /// (`WindingConnection.D`), with its enumeration.
470    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/// An enumeration of the schema and the values it allows.
477#[derive(Debug)]
478pub struct EnumDef {
479    pub ns: &'static str,
480    pub local: &'static str,
481    /// The vocabulary's `rdfs:comment`, as for [`ClassDef::comment`].
482    pub comment: &'static str,
483    /// In vocabulary order.
484    pub values: &'static [EnumValueDef],
485}
486
487/// One value of an enumeration.
488#[derive(Debug)]
489pub struct EnumValueDef {
490    /// `Enum.value`, as the decoder keeps a value after its `#`.
491    pub id: &'static str,
492    /// The vocabulary's `rdfs:comment`, as for [`ClassDef::comment`].
493    pub comment: &'static str,
494}
495
496/// A class of the schema: what it is called and what it declares.
497#[derive(Debug)]
498pub struct ClassDef {
499    pub ns: &'static str,
500    pub local: &'static str,
501    /// Family-qualified name; the `CimDataset::by_type` key.
502    pub qualified: &'static str,
503    /// Index of the super class within the same table.
504    pub super_class: Option<usize>,
505    pub concrete: bool,
506    /// The attributes this class declares itself, unused association ends
507    /// included ([`AttrDef::used`]); see [`TypeRegistry::attr`] for inherited
508    /// ones.
509    pub attrs: &'static [AttrDef],
510    pub origins: &'static [&'static str],
511    /// The vocabulary's `rdfs:comment`, HTML removed and on one line; `""`
512    /// if it has none.
513    pub comment: &'static str,
514}
515
516impl ClassDef {
517    /// An attribute this class declares itself (not an inherited one).
518    pub fn attr(&self, id: &str) -> Option<&'static AttrDef> {
519        self.attrs.iter().find(|a| a.id == id)
520    }
521}
522
523/// Namespace-aware class lookup, and every attribute each class declares.
524///
525/// RDF/XML identifies a class by `(namespace, local name)`, and the two profile
526/// families overlap on 164 local names — both even spell the prefix `cim`, so
527/// only the xmlns binding distinguishes them. Lookup is therefore keyed on the
528/// resolved namespace, with a bare-name fallback (CGMES only) that preserves
529/// the historical behaviour for files whose prefixes are unbound or unexpected.
530#[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    /// Qualified class name → every attribute it declares, inherited ones
536    /// included. Built once from the class tables.
537    declared: FastMap<&'static str, FastMap<&'static str, &'static AttrDef>>,
538    /// Qualified class name → the class and its ancestors, root first.
539    chain: FastMap<&'static str, Vec<&'static ClassDef>>,
540    /// Each registered family, in registration order.
541    families: Vec<FamilyTable>,
542}
543
544/// One family as registered: its schema, the `by_type` prefix its classes
545/// carry (`""` for CGMES, `"nc:"`), and its attributes by id.
546struct FamilyTable {
547    id: &'static str,
548    prefix: &'static str,
549    schema: &'static Schema,
550    /// Attribute id → its first declaration in table order. An id names one
551    /// predicate, whichever class declares it.
552    attrs: FastMap<&'static str, &'static AttrDef>,
553}
554
555impl TypeRegistry {
556    pub fn new() -> Self {
557        Self::default()
558    }
559
560    /// Register a family's schema under `id`. `bare_fallback` adds its classes
561    /// to the bare-name fallback: an unbound prefix means the families cannot
562    /// be told apart, so only the default family (CGMES) takes it.
563    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    /// Lookup table for one namespace, resolved once per XML prefix per file.
599    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    /// The attribute `id` if `class` declares it, itself or by inheritance.
612    pub fn attr(&self, class: &ClassDef, id: &str) -> Option<&'static AttrDef> {
613        self.declared.get(class.qualified)?.get(id).copied()
614    }
615
616    /// The attributes `class` declares, inherited ones included, by id — for a
617    /// caller that resolves many keys of one element.
618    pub fn declared(&self, class: &ClassDef) -> Option<&FastMap<&'static str, &'static AttrDef>> {
619        self.declared.get(class.qualified)
620    }
621
622    /// The `&'static` key a field `id` of `class` is stored under.
623    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    /// `class` and its ancestors, root first.
628    pub fn chain(&self, class: &ClassDef) -> &[&'static ClassDef] {
629        self.chain.get(class.qualified).map_or(&[], Vec::as_slice)
630    }
631
632    /// The `by_type` keys of the concrete classes at or below `qualified`,
633    /// sorted: what instances of a class can be typed as.
634    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    /// Every attribute `class` declares, inherited ones included.
646    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    /// The schema registered as `family` (`"cgmes"`, `"nc"`).
651    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    /// The enumeration value `id` (`WindingConnection.D`) in the family of
660    /// `class`, which an element of `class` uses it on.
661    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    /// The family a `by_type` key belongs to, by its prefix.
666    pub fn family_of(&self, type_name: &str) -> Option<&'static str> {
667        // The longest matching prefix, so `nc:X` is not taken for CGMES's "".
668        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    /// The attribute `id` as `family` declares it on any class — what a field
676    /// key names when the element's own class does not declare it.
677    pub fn family_attr(&self, family: &str, id: &str) -> Option<&'static AttrDef> {
678        self.family(family)?.attrs.get(id).copied()
679    }
680
681    /// The attribute a field `key` of an element of `class` stands for: the
682    /// class's own declaration, else the family's.
683    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}