ifc_lite_core/
schema_family.rs

1// SPDX-License-Identifier: MPL-2.0
2//! The four schema families this workspace converts between and labels files
3//! with: IFC2X3, IFC4, IFC4X3 and IFC5 (C6-1, D44).
4//!
5//! This is the ONE classifier of a `FILE_SCHEMA` label into a family. Before it
6//! existed the same prefix test was spelled in export's `canon`, step_log's
7//! `schema_family`, processing's `schema_of_identifier` and two `starts_with`
8//! allowlists, each with its own order, trimming and fallback.
9//!
10//! Lookups return `Option`: a label that names no family is `None`, and every
11//! caller states its own default (IFC4 for re-export, fail closed for reads by
12//! attribute name). That is the whole point; there is no default here.
13//!
14//! [`SchemaVersion::from_file_schema`](crate::SchemaVersion::from_file_schema)
15//! stays the registry selector (exact family plus `_ADD`/`_TC`/`_RC` suffixes).
16//! A family is looser by design: any label that begins with the family name.
17
18use crate::parser::header::first_declared_schema;
19use crate::SchemaVersion;
20
21/// Bytes of the source the last-resort scan reads when no declared identifier
22/// names a family. The TS parser's figure (`detectSchemaVersion`).
23const FALLBACK_SCAN_BYTES: usize = 2000;
24
25/// Family names, longest first: `IFC4X3` begins with `IFC4`.
26const FAMILIES: [SchemaFamily; 4] =
27    [SchemaFamily::Ifc5, SchemaFamily::Ifc4x3, SchemaFamily::Ifc4, SchemaFamily::Ifc2x3];
28
29/// An IFC schema family.
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31#[non_exhaustive]
32pub enum SchemaFamily {
33    Ifc2x3,
34    Ifc4,
35    Ifc4x3,
36    Ifc5,
37}
38
39impl SchemaFamily {
40    /// The canonical name: `IFC2X3`, `IFC4`, `IFC4X3` or `IFC5`.
41    pub const fn as_str(self) -> &'static str {
42        match self {
43            Self::Ifc2x3 => "IFC2X3",
44            Self::Ifc4 => "IFC4",
45            Self::Ifc4x3 => "IFC4X3",
46            Self::Ifc5 => "IFC5",
47        }
48    }
49
50    /// The family a `FILE_SCHEMA` label names, by prefix after trimming,
51    /// ASCII-case-insensitively and longest family first (`IFC4X3_ADD2`,
52    /// `IFC2X3_TC1` and `IFC4X1` all occur in the wild). `IFCX` is IFC5.
53    /// `None` for a label naming no family.
54    pub fn from_label(label: &str) -> Option<Self> {
55        Self::from_step_label(label).or_else(|| {
56            let t = label.trim();
57            (t.len() >= 4 && t.as_bytes()[..4].eq_ignore_ascii_case(b"IFCX")).then_some(Self::Ifc5)
58        })
59    }
60
61    /// [`Self::from_label`] without the `IFCX` alias: the labels a STEP header
62    /// can declare. The TS twin (`detectSchemaVersion`) has no `IFCX` either.
63    fn from_step_label(label: &str) -> Option<Self> {
64        let t = label.trim().as_bytes();
65        FAMILIES.into_iter().find(|f| {
66            let name = f.as_str().as_bytes();
67            t.len() >= name.len() && t[..name.len()].eq_ignore_ascii_case(name)
68        })
69    }
70
71    /// The family a STEP file declares, by the rule of the TS parser's
72    /// `detectSchemaVersion`, pinned by
73    /// `tests/fixtures/schema_version_vectors.json`:
74    /// 1. the first `FILE_SCHEMA` identifier that names a family wins (the
75    ///    shared [`first_declared_schema`] selection);
76    /// 2. else the first 2000 bytes are searched for a family token, ASCII
77    ///    case-insensitively (a loose last resort, as the TS twin does);
78    /// 3. else `None`.
79    pub fn detect(content: &[u8]) -> Option<Self> {
80        first_declared_schema(content, Self::from_step_label).or_else(|| {
81            let head = &content[..content.len().min(FALLBACK_SCAN_BYTES)];
82            let text = String::from_utf8_lossy(head).to_ascii_uppercase();
83            FAMILIES.into_iter().find(|f| text.contains(f.as_str()))
84        })
85    }
86
87    /// The family of a `FILE_SCHEMA` label by the registry selector: the
88    /// family of [`SchemaVersion::from_file_schema`]'s version, and `IFC5`
89    /// (which has no bundled registry) for the label `IFC5`. Stricter than
90    /// [`Self::from_label`]: `IFC4FOO` names no family here.
91    pub fn of_file_schema(label: &str) -> Option<Self> {
92        SchemaVersion::from_file_schema(label)
93            .map(Self::of)
94            .or_else(|| label.trim().eq_ignore_ascii_case("IFC5").then_some(Self::Ifc5))
95    }
96
97    /// The family of a bundled registry version (`Ifc4x1`, `Ifc4x2` are IFC4).
98    pub const fn of(version: SchemaVersion) -> Self {
99        match version {
100            SchemaVersion::Ifc2x3 => Self::Ifc2x3,
101            SchemaVersion::Ifc4 | SchemaVersion::Ifc4x1 | SchemaVersion::Ifc4x2 => Self::Ifc4,
102            SchemaVersion::Ifc4x3 => Self::Ifc4x3,
103        }
104    }
105}
106
107#[cfg(test)]
108#[path = "schema_family_tests.rs"]
109mod tests;