Skip to main content

ifc_resource/usage/
measure.rs

1//! `IfcMeasureWithUnit` -- the IFC2X3 resource `BaseQuantity` form.
2//!
3//! IFC2X3 TC1 declares `IfcConstructionResource.BaseQuantity` as an
4//! `IfcMeasureWithUnit` (`ValueComponent : IfcValue`, `UnitComponent :
5//! IfcUnit`). The entity exists unchanged in IFC4 and IFC4X3, so the
6//! projection reads it under every supported release. It preserves the
7//! authored value; it converts no units.
8
9use ifc_model::{EntityId, Value};
10
11use crate::error::{ResourceError, ResourceResult};
12use crate::view::Record;
13
14/// A borrowed, schema-resolved `IfcMeasureWithUnit` projection.
15#[derive(Debug, Clone, Copy)]
16pub struct MeasureWithUnit<'m, 's> {
17    record: Record<'m, 's>,
18}
19
20impl<'m, 's> MeasureWithUnit<'m, 's> {
21    pub(crate) fn from_record(record: Record<'m, 's>) -> Self {
22        Self { record }
23    }
24
25    /// The entity id of the projected `IfcMeasureWithUnit`.
26    #[must_use]
27    pub fn id(&self) -> EntityId {
28        self.record.id
29    }
30
31    /// The `ValueComponent` attribute: the typed `IfcValue` as authored, for
32    /// example `IFCCOUNTMEASURE(4.)`.
33    ///
34    /// The result is always a [`Value::Typed`] whose type name the release
35    /// accepts as an `IfcValue`; an untyped literal, or a wrapper naming a
36    /// type outside `IfcValue`, is refused with
37    /// [`ResourceError::InvalidValue`] rather than guessed.
38    pub fn value_component(&self) -> ResourceResult<&'m Value> {
39        let value = self.record.value("ValueComponent")?;
40        match value {
41            Value::Typed { type_name, .. }
42                if self.record.schema.accepts_type("IfcValue", type_name) =>
43            {
44                Ok(value)
45            }
46            _ => Err(ResourceError::InvalidValue {
47                entity: self.record.id,
48                attribute: "ValueComponent",
49                expected: "typed IfcValue",
50            }),
51        }
52    }
53
54    /// The `UnitComponent` attribute: an `IfcDerivedUnit`, `IfcNamedUnit`
55    /// or `IfcMonetaryUnit` reference.
56    pub fn unit_component(&self) -> ResourceResult<EntityId> {
57        self.record.required_ref_select(
58            "UnitComponent",
59            "IfcUnit",
60            &["IfcDerivedUnit", "IfcNamedUnit", "IfcMonetaryUnit"],
61        )
62    }
63}