ifc_lite_processing/data_model/
property.rs1use super::properties::{extract_property, resolve_complex_into, ComplexMemo};
22use super::types::Property;
23use ifc_lite_core::limits::ComplexPropertyBudget;
24use ifc_lite_core::{slot, AttributeValue, DecodedEntity, EntityDecoder, IfcType};
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28#[non_exhaustive]
29pub enum PropertyKind {
30 Single,
31 Enumerated,
32 List,
33 Bounded,
34 Table,
35 Reference,
36 Complex,
37}
38
39impl PropertyKind {
40 pub fn of(ty: &IfcType) -> Option<Self> {
42 if *ty == IfcType::IfcPropertySingleValue {
43 return Some(Self::Single);
44 }
45 Some(match ty.as_str().to_ascii_uppercase().as_str() {
46 "IFCPROPERTYENUMERATEDVALUE" => Self::Enumerated,
47 "IFCPROPERTYLISTVALUE" => Self::List,
48 "IFCPROPERTYBOUNDEDVALUE" => Self::Bounded,
49 "IFCPROPERTYTABLEVALUE" => Self::Table,
50 "IFCPROPERTYREFERENCEVALUE" => Self::Reference,
51 "IFCCOMPLEXPROPERTY" => Self::Complex,
52 _ => return None,
53 })
54 }
55
56 pub fn class_keyword(self) -> &'static str {
59 match self {
60 Self::Single => "IFCPROPERTYSINGLEVALUE",
61 Self::Enumerated => "IFCPROPERTYENUMERATEDVALUE",
62 Self::List => "IFCPROPERTYLISTVALUE",
63 Self::Bounded => "IFCPROPERTYBOUNDEDVALUE",
64 Self::Table => "IFCPROPERTYTABLEVALUE",
65 Self::Reference => "IFCPROPERTYREFERENCEVALUE",
66 Self::Complex => "IFCCOMPLEXPROPERTY",
67 }
68 }
69}
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74#[non_exhaustive]
75pub enum PropertyScope {
76 SingleValueOnly,
79 Flat,
84 AllKinds,
87}
88
89#[derive(Debug, Clone)]
91pub struct PropertyRow {
92 pub name: String,
94 pub kind: PropertyKind,
95 pub nominal: Option<AttributeValue>,
98 pub display: Option<Property>,
102}
103
104#[derive(Debug, Clone)]
106pub struct PropertySetRow {
107 pub id: u32,
108 pub name: Option<String>,
110 pub properties: Vec<PropertyRow>,
111}
112
113pub struct PropertyDecoder {
115 scope: PropertyScope,
116 budget: ComplexPropertyBudget,
117 memo: ComplexMemo,
118}
119
120impl PropertyDecoder {
121 pub fn for_input(input_len: usize, scope: PropertyScope) -> Self {
124 Self { scope, budget: ComplexPropertyBudget::for_input(input_len), memo: ComplexMemo::new() }
125 }
126
127 pub fn scope(&self) -> PropertyScope {
128 self.scope
129 }
130
131 pub fn cuts(&self) -> usize {
133 self.budget.cuts()
134 }
135
136 pub fn decode_set(&mut self, decoder: &mut EntityDecoder, def: &DecodedEntity) -> Option<PropertySetRow> {
144 let name = def.string_at(slot::IfcPropertySet::NAME).map(str::to_string);
145 let members = decoder.resolve_ref_list(def.attr(slot::IfcPropertySet::HAS_PROPERTIES)?).ok()?;
146 let mut properties = Vec::new();
147 for member in &members {
148 let Some(kind) = PropertyKind::of(&member.ifc_type) else { continue };
149 if self.scope == PropertyScope::SingleValueOnly && kind != PropertyKind::Single {
150 continue;
151 }
152 let Some(row) = self.decode_property(decoder, member, kind) else { continue };
153 properties.push(row);
154 }
155 Some(PropertySetRow { id: def.id, name, properties })
156 }
157
158 fn decode_property(
159 &mut self, decoder: &mut EntityDecoder, entity: &DecodedEntity, kind: PropertyKind,
160 ) -> Option<PropertyRow> {
161 if kind == PropertyKind::Single && self.scope != PropertyScope::AllKinds {
162 let name = entity.string_at(slot::IfcPropertySingleValue::NAME)?.to_string();
163 return Some(PropertyRow { name, kind, nominal: entity.attr(slot::IfcPropertySingleValue::NOMINAL_VALUE).cloned(), display: None });
164 }
165 let mut display = extract_property(entity, decoder)?;
166 if kind == PropertyKind::Complex {
167 resolve_complex_into(&mut display, entity.id, decoder, &mut self.budget, &mut self.memo);
168 }
169 let nominal = if kind == PropertyKind::Single { entity.attr(slot::IfcPropertySingleValue::NOMINAL_VALUE).cloned() } else { None };
170 Some(PropertyRow { name: display.property_name.clone(), kind, nominal, display: Some(display) })
171 }
172}
173
174#[cfg(test)]
175#[path = "property_tests.rs"]
176mod tests;