ifc_lite_core/units_length.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//! The one length-unit admission policy (#7678, CORE-15), split out of
6//! `units.rs` to keep that module inside its module-size budget.
7
8use super::extract_length_unit_scale;
9use crate::decoder::EntityDecoder;
10use crate::error::{Error, Result};
11
12/// A project's length unit under input admission (#7678, CORE-15): the one
13/// home of the "unusable unit factor" policy every caller follows.
14#[derive(Clone, Copy, Debug, PartialEq)]
15pub struct LengthUnit {
16 /// Metres per file unit as the data model reads it, and as main did: an
17 /// overflowing declared factor reads as infinity, any other unusable one
18 /// as metres. Data readers and exporters keep this value.
19 pub scale: f64,
20 /// The `IFCMEASUREWITHUNIT` holding a declared conversion factor that is
21 /// not a positive finite number. Geometry must refuse to mesh against it
22 /// (fail closed and count it): no metre scale is right for it.
23 pub unusable_factor: Option<u32>,
24}
25
26impl LengthUnit {
27 /// The scale geometry may mesh with, or the refusal of the factor.
28 pub fn for_geometry(self) -> Result<f64> {
29 match self.unusable_factor {
30 Some(factor) => Err(Error::UnusableUnitFactor { factor, overflowed: !self.scale.is_finite() }),
31 None => Ok(self.scale),
32 }
33 }
34}
35
36/// [`extract_length_unit_scale`] with its unusable-factor refusal turned into
37/// a [`LengthUnit`], so a data reader keeps main's value and a geometry reader
38/// refuses. Any other error (an undecodable project or assignment) is returned
39/// as before; callers keep their existing defaults for it.
40pub fn resolve_length_unit(decoder: &mut EntityDecoder, project_id: u32) -> Result<LengthUnit> {
41 match extract_length_unit_scale(decoder, project_id) {
42 Ok(scale) => Ok(LengthUnit { scale, unusable_factor: None }),
43 Err(Error::UnusableUnitFactor { factor, overflowed }) => Ok(LengthUnit {
44 scale: if overflowed { f64::INFINITY } else { 1.0 },
45 unusable_factor: Some(factor),
46 }),
47 Err(error) => Err(error),
48 }
49}
50