ifc_lite_processing/
scan.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//! [`ScanClass`]: the one table of STEP keywords a scan loop reacts to.
6//!
7//! Every pre-pass used to spell its own keyword chain: the native scan, the
8//! browser's serial pre-pass, [`PrepassSpans::stash`](crate::prepass::PrepassSpans::stash)
9//! and the sharded scan's `classify_type_name` each compared the same
10//! keywords in their own order. A keyword added to one and not the others was
11//! a silent drift between pipelines (#858/#913 class). This is the table; each
12//! scan matches on the class instead of re-comparing text.
13//!
14//! [`ScanClass::classify`] is a case-insensitive match on the keyword's length
15//! followed by at most three same-length comparisons, with no allocation. It
16//! returns exactly what the chain `keyword_eq(.., A) || keyword_eq(.., B) ..`
17//! returned for the same string (`scan_class_matches_the_keyword_chain` pins
18//! that over every schema type name and the spelling variants).
19
20use ifc_lite_core::keyword_starts_with;
21
22/// What a scan loop does with a record, decided from its keyword alone.
23///
24/// `Other` is everything else: the scan then asks the schema helpers whether
25/// the record is a geometry job or a type product.
26#[non_exhaustive]
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum ScanClass {
29    /// `IFCSTYLEDITEM`.
30    StyledItem,
31    /// `IFCINDEXEDCOLOURMAP`.
32    IndexedColourMap,
33    /// `IFCMATERIALDEFINITIONREPRESENTATION`.
34    MaterialDefinitionRepresentation,
35    /// `IFCRELASSOCIATESMATERIAL`.
36    RelAssociatesMaterial,
37    /// `IFCRELVOIDSELEMENT`.
38    RelVoidsElement,
39    /// `IFCRELFILLSELEMENT`.
40    RelFillsElement,
41    /// `IFCRELAGGREGATES`.
42    RelAggregates,
43    /// `IFCRELDEFINESBYTYPE`.
44    RelDefinesByType,
45    /// `IFCMAPPEDITEM`.
46    MappedItem,
47    /// `IFCPROJECT`.
48    Project,
49    /// `IFCSITE` (also a geometry job).
50    Site,
51    /// `IFCBUILDING` (a geometry job only when its `Representation` is set, #1910).
52    Building,
53    /// `IFCMATERIALLAYERSET` / `IFCMATERIALLAYERSETUSAGE`.
54    MaterialLayerSet,
55    /// `IFCPRESENTATIONLAYERASSIGNMENT`.
56    PresentationLayerAssignment,
57    /// `IFCPROPERTYSET`.
58    PropertySet,
59    /// `IFCRELDEFINESBYPROPERTIES`.
60    RelDefinesByProperties,
61    /// Any other `IFCPROPERTY...` record (single values, enumerations, ...).
62    Property,
63    /// No scan-time reaction by keyword.
64    Other,
65}
66
67/// Case-insensitive equality against an uppercase keyword, on bytes.
68#[inline(always)]
69fn is(raw: &[u8], upper: &str) -> bool {
70    raw.eq_ignore_ascii_case(upper.as_bytes())
71}
72
73impl ScanClass {
74    /// Classify a scanned keyword (any case, as the file spelled it).
75    #[inline]
76    pub fn classify(type_name: &str) -> Self {
77        let raw = type_name.as_bytes();
78        // Length first: all but a few records differ in length from every
79        // keyword, so the common record costs one jump and no comparison.
80        let class = match raw.len() {
81            7 if is(raw, "IFCSITE") => Self::Site,
82            10 if is(raw, "IFCPROJECT") => Self::Project,
83            11 if is(raw, "IFCBUILDING") => Self::Building,
84            13 if is(raw, "IFCSTYLEDITEM") => Self::StyledItem,
85            13 if is(raw, "IFCMAPPEDITEM") => Self::MappedItem,
86            14 if is(raw, "IFCPROPERTYSET") => Self::PropertySet,
87            16 if is(raw, "IFCRELAGGREGATES") => Self::RelAggregates,
88            18 if is(raw, "IFCRELVOIDSELEMENT") => Self::RelVoidsElement,
89            18 if is(raw, "IFCRELFILLSELEMENT") => Self::RelFillsElement,
90            19 if is(raw, "IFCINDEXEDCOLOURMAP") => Self::IndexedColourMap,
91            19 if is(raw, "IFCRELDEFINESBYTYPE") => Self::RelDefinesByType,
92            19 if is(raw, "IFCMATERIALLAYERSET") => Self::MaterialLayerSet,
93            24 if is(raw, "IFCRELASSOCIATESMATERIAL") => Self::RelAssociatesMaterial,
94            24 if is(raw, "IFCMATERIALLAYERSETUSAGE") => Self::MaterialLayerSet,
95            25 if is(raw, "IFCRELDEFINESBYPROPERTIES") => Self::RelDefinesByProperties,
96            30 if is(raw, "IFCPRESENTATIONLAYERASSIGNMENT") => Self::PresentationLayerAssignment,
97            35 if is(raw, "IFCMATERIALDEFINITIONREPRESENTATION") => {
98                Self::MaterialDefinitionRepresentation
99            }
100            _ => Self::Other,
101        };
102        if class == Self::Other && keyword_starts_with(type_name, "IFCPROPERTY") {
103            return Self::Property;
104        }
105        class
106    }
107
108    /// The sharded scan's class code for this keyword, or
109    /// [`PREPASS_CLASS_NONE`](crate::PREPASS_CLASS_NONE) for the classes that scan does not name.
110    pub fn wire_code(self) -> u8 {
111        use crate::shard_classes as w;
112        match self {
113            Self::Project => w::PREPASS_CLASS_PROJECT,
114            Self::Site => w::PREPASS_CLASS_SITE,
115            Self::StyledItem => w::PREPASS_CLASS_STYLED_ITEM,
116            Self::IndexedColourMap => w::PREPASS_CLASS_INDEXED_COLOUR_MAP,
117            Self::MaterialDefinitionRepresentation => w::PREPASS_CLASS_MATERIAL_DEF_REPR,
118            Self::RelAssociatesMaterial => w::PREPASS_CLASS_REL_ASSOCIATES_MATERIAL,
119            Self::RelVoidsElement => w::PREPASS_CLASS_REL_VOIDS,
120            Self::RelFillsElement => w::PREPASS_CLASS_REL_FILLS,
121            Self::RelAggregates => w::PREPASS_CLASS_REL_AGGREGATES,
122            Self::MappedItem => w::PREPASS_CLASS_MAPPED_ITEM,
123            Self::RelDefinesByType => w::PREPASS_CLASS_REL_DEFINES_BY_TYPE,
124            Self::MaterialLayerSet => w::PREPASS_CLASS_MATERIAL_LAYER_SET,
125            Self::Building
126            | Self::PresentationLayerAssignment
127            | Self::PropertySet
128            | Self::RelDefinesByProperties
129            | Self::Property
130            | Self::Other => w::PREPASS_CLASS_NONE,
131        }
132    }
133}
134
135impl From<ScanClass> for u8 {
136    /// The shard wire code ([`ScanClass::wire_code`]).
137    fn from(class: ScanClass) -> u8 {
138        class.wire_code()
139    }
140}
141
142impl TryFrom<u8> for ScanClass {
143    type Error = u8;
144
145    /// The class a shard wire code names; flag bits must already be masked off
146    /// (`PREPASS_CLASS_CODE_MASK`). `PREPASS_CLASS_NONE` and unknown codes are `Err(code)`.
147    fn try_from(code: u8) -> Result<Self, u8> {
148        use crate::shard_classes as w;
149        Ok(match code {
150            w::PREPASS_CLASS_PROJECT => Self::Project,
151            w::PREPASS_CLASS_SITE => Self::Site,
152            w::PREPASS_CLASS_STYLED_ITEM => Self::StyledItem,
153            w::PREPASS_CLASS_INDEXED_COLOUR_MAP => Self::IndexedColourMap,
154            w::PREPASS_CLASS_MATERIAL_DEF_REPR => Self::MaterialDefinitionRepresentation,
155            w::PREPASS_CLASS_REL_ASSOCIATES_MATERIAL => Self::RelAssociatesMaterial,
156            w::PREPASS_CLASS_REL_VOIDS => Self::RelVoidsElement,
157            w::PREPASS_CLASS_REL_FILLS => Self::RelFillsElement,
158            w::PREPASS_CLASS_REL_AGGREGATES => Self::RelAggregates,
159            w::PREPASS_CLASS_MAPPED_ITEM => Self::MappedItem,
160            w::PREPASS_CLASS_REL_DEFINES_BY_TYPE => Self::RelDefinesByType,
161            w::PREPASS_CLASS_MATERIAL_LAYER_SET => Self::MaterialLayerSet,
162            other => return Err(other),
163        })
164    }
165}
166
167#[cfg(test)]
168#[path = "scan_tests.rs"]
169mod scan_tests;