Skip to main content

ifc_alignment/authoring/
mod.rs

1//! Author IFC4X3 alignment entities from plain numbers.
2//!
3//! Mirrors ADR 0011: this is the write direction of the same bridge that
4//! reads alignments, and it indexes `crate::slot` rather than restating a
5//! single attribute position.
6//!
7//! Authoring records is independent of 3D composition. A file states its
8//! horizontal layout, its vertical profile and its cant as separate
9//! parameter segments; composing them into a centreline is a lowering
10//! concern and does not gate writing the records.
11//!
12//! Values are plain `f64` in the units the caller declares in the file.
13//! Nothing here converts units: the readers apply `AlignmentUnits` on the
14//! way out, so applying a factor here too would double-scale.
15
16mod layout;
17mod linear;
18mod referent;
19mod segment;
20
21pub use layout::{alignment, alignment_segment, cant_layout, horizontal_layout, vertical_layout};
22pub use linear::{linear_element, linear_positioning_element};
23pub use referent::{
24    axis2_placement_linear, cartesian_point, linear_placement, point_by_distance, referent,
25    stationing,
26};
27pub use segment::{
28    cant_segment, horizontal_segment, vertical_segment, CantSegmentDraft, HorizontalSegmentDraft,
29    VerticalSegmentDraft,
30};
31
32use ifc_model::guid::Guid;
33use ifc_model::Value;
34
35use crate::error::AlignmentError;
36
37/// Refuse an authored value before anything is staged.
38pub(crate) fn invalid(
39    type_name: &'static str,
40    attribute: &'static str,
41    detail: impl Into<String>,
42) -> AlignmentError {
43    AlignmentError::InvalidAuthoredValue {
44        type_name,
45        attribute,
46        detail: detail.into(),
47    }
48}
49
50/// Refuse a non-finite measure. NaN and infinity parse back as garbage
51/// rather than failing, so they are rejected at the boundary.
52pub(crate) fn finite(
53    type_name: &'static str,
54    attribute: &'static str,
55    value: f64,
56) -> Result<(), AlignmentError> {
57    if value.is_finite() {
58        Ok(())
59    } else {
60        Err(invalid(
61            type_name,
62            attribute,
63            format!("{value} is not finite"),
64        ))
65    }
66}
67
68/// An IFC GlobalId, checked by [`Guid::parse`]: 22 characters of the IFC
69/// base-64 alphabet whose leading digit is `0`-`3`, so the text names a
70/// 128-bit UUID. A wrong length, an alphabet the encoding does not use and
71/// an out-of-range leading digit are all malformed.
72pub(crate) fn guid(type_name: &'static str, value: &str) -> Result<Value, AlignmentError> {
73    if Guid::parse(value).is_none() {
74        return Err(invalid(
75            type_name,
76            "GlobalId",
77            format!(
78                "{value:?} is not a compressed IFC GlobalId (22 characters of \
79                 0-9 A-Z a-z _ $, the first 0-3)"
80            ),
81        ));
82    }
83    Ok(Value::Text(value.into()))
84}