ifc_lite_core/attribute_slot.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Runtime by-name attribute lookup over the generated EXPRESS registries.
6//!
7//! The generator (`@ifc-lite/codegen`) emits, beside every
8//! `IfcType::attribute_names()`, a parallel `attribute_flags()` byte slice:
9//! bits 0-3 the value kind, bit 4 AGGREGATE, bit 5 OPTIONAL. This module is
10//! the one decoder of those bytes and the one by-name lookup over them
11//! ([`SchemaVersion::attribute`]); callers that need "where does attribute X
12//! of entity Y live, and what shape is it" ask here instead of keeping a
13//! second table (F3e-0a, #7780).
14//!
15//! The lookup is ASCII-case-insensitive in both the entity and the attribute
16//! name, per-schema (a position in IFC4 says nothing about IFC2X3), and
17//! answers `None` explicitly for an entity the schema does not declare or an
18//! attribute the entity does not have. It scans the entity's attribute list
19//! (a few dozen names at most) after one registry match; callers on a loop
20//! memoise the result per entity type.
21
22use crate::generated::schema_registry::SchemaVersion;
23
24/// What an attribute's declared EXPRESS type resolves to, through defined
25/// types and selects. Mirrors `KIND` in `packages/codegen/src/rust-attribute-flags.ts`.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27#[non_exhaustive]
28#[repr(u8)]
29pub enum AttrKind {
30 /// An entity, or a select of entities only.
31 Ref = 0,
32 /// An aggregate of entities.
33 RefList = 1,
34 /// Any other aggregate.
35 List = 2,
36 /// REAL or NUMBER.
37 Real = 3,
38 /// INTEGER.
39 Int = 4,
40 /// STRING.
41 Text = 5,
42 /// An ENUMERATION.
43 Enum = 6,
44 /// BOOLEAN or LOGICAL.
45 Logical = 7,
46 /// BINARY.
47 Binary = 8,
48 /// A select mixing entities with value types.
49 Select = 9,
50}
51
52const KIND_MASK: u8 = 0x0F;
53const AGGREGATE: u8 = 0x10;
54const OPTIONAL: u8 = 0x20;
55
56impl AttrKind {
57 pub(crate) fn from_code(code: u8) -> Option<Self> {
58 Some(match code {
59 0 => Self::Ref,
60 1 => Self::RefList,
61 2 => Self::List,
62 3 => Self::Real,
63 4 => Self::Int,
64 5 => Self::Text,
65 6 => Self::Enum,
66 7 => Self::Logical,
67 8 => Self::Binary,
68 9 => Self::Select,
69 _ => return None,
70 })
71 }
72}
73
74/// One declared attribute of an entity in one schema.
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76#[non_exhaustive]
77pub struct AttrSlot {
78 /// Position in the STEP record (supertype attributes first), as
79 /// `DecodedEntity::get` indexes it.
80 pub index: usize,
81 /// The resolved value kind.
82 pub kind: AttrKind,
83 /// The value may be written as a STEP list: a LIST/SET/ARRAY, a defined
84 /// type that is one, or a select with such an alternative (for example
85 /// `IfcRelDefinesByProperties.RelatingPropertyDefinition`).
86 pub aggregate: bool,
87 /// The EXPRESS declaration says OPTIONAL.
88 pub optional: bool,
89}
90
91fn decode(index: usize, flags: u8) -> AttrSlot {
92 AttrSlot {
93 index,
94 // The generator only emits codes 0..=9 (asserted for every entity of
95 // every schema in the tests); Select is the conservative reading of
96 // a code from a newer generator.
97 kind: AttrKind::from_code(flags & KIND_MASK).unwrap_or(AttrKind::Select),
98 aggregate: flags & AGGREGATE != 0,
99 optional: flags & OPTIONAL != 0,
100 }
101}
102
103impl SchemaVersion {
104 /// The attribute `name` of `entity` in this schema, or `None` when the
105 /// schema does not declare the entity or the entity has no such attribute.
106 ///
107 /// Both names compare ASCII-case-insensitively (`IfcWall`, `IFCWALL`,
108 /// `globalid`). A name with non-ASCII characters never matches.
109 pub fn attribute(self, entity: &str, name: &str) -> Option<AttrSlot> {
110 let (names, flags) = self.attribute_layout(entity)?;
111 let index = names.iter().position(|n| n.eq_ignore_ascii_case(name))?;
112 Some(decode(index, flags[index]))
113 }
114
115 /// Every attribute of `entity` in declaration order, or `None` when the
116 /// schema does not declare the entity. A declared entity without
117 /// attributes yields an empty iterator.
118 pub fn attributes(self, entity: &str) -> Option<impl ExactSizeIterator<Item = AttrSlot>> {
119 let (_, flags) = self.attribute_layout(entity)?;
120 Some(
121 flags
122 .iter()
123 .enumerate()
124 .map(|(index, flags)| decode(index, *flags)),
125 )
126 }
127
128}
129
130#[cfg(test)]
131#[path = "attribute_slot_tests.rs"]
132pub(crate) mod tests;