ifc_lite_diff/
class_families.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/.
4use crate::JsString;
5use std::collections::HashMap;
6pub const DEFAULT_CLASS_FAMILIES: &[&[&str]] = &[
7    &["IfcWall", "IfcWallStandardCase", "IfcWallElementedCase", "IfcBuildingElementPart"],
8    &["IfcSlab", "IfcSlabStandardCase", "IfcSlabElementedCase"],
9    &["IfcBeam", "IfcBeamStandardCase"], &["IfcColumn", "IfcColumnStandardCase"],
10    &["IfcMember", "IfcMemberStandardCase"], &["IfcPlate", "IfcPlateStandardCase"],
11    &["IfcDoor", "IfcDoorStandardCase"], &["IfcWindow", "IfcWindowStandardCase"],
12    &["IfcOpeningElement", "IfcOpeningStandardCase"],
13    &["IfcFurniture", "IfcFurnishingElement", "IfcSystemFurnitureElement"],
14];
15pub fn normalize_class(value: &JsString) -> JsString {
16    value.normalized_class()
17}
18type FamilyMap = HashMap<JsString, JsString, crate::JsHash>;
19pub struct ClassFamilies(std::borrow::Cow<'static, FamilyMap>);
20impl ClassFamilies {
21    pub fn new(rows: Option<&[Vec<JsString>]>) -> Self {
22        match rows {
23            Some(rows) => Self(std::borrow::Cow::Owned(family_map(rows))),
24            // #8351: every comparison used to rebuild and renormalize the
25            // defaults twice. They are ASCII, so host Unicode overrides never
26            // apply to them and one process-wide map is exact.
27            None => {
28                static DEFAULTS: std::sync::OnceLock<FamilyMap> = std::sync::OnceLock::new();
29                Self(std::borrow::Cow::Borrowed(DEFAULTS.get_or_init(|| {
30                    let rows: Vec<Vec<JsString>> = DEFAULT_CLASS_FAMILIES.iter()
31                        .map(|row| row.iter().map(|s| (*s).into()).collect()).collect();
32                    family_map(&rows)
33                })))
34            }
35        }
36    }
37    pub fn family_of(&self, name: &JsString) -> JsString {
38        let key = normalize_class(name);
39        self.0.get(&key).cloned().unwrap_or(key)
40    }
41}
42fn family_map(rows: &[Vec<JsString>]) -> FamilyMap {
43    let mut map = FamilyMap::default();
44    for row in rows {
45        let members: Vec<_> = row.iter().map(normalize_class).filter(|name| !name.is_empty()).collect();
46        if let Some(canonical) = members.first() {
47            for member in &members { map.entry(member.clone()).or_insert_with(|| canonical.clone()); }
48        }
49    }
50    map
51}