Skip to main content

openbim_ifc_binding_core/
header.rs

1//! The STEP file header: read it whole, replace it whole.
2//!
3//! The facade's `Header` is plain data (`FILE_DESCRIPTION`, `FILE_NAME`,
4//! `FILE_SCHEMA`) and the model hands it out mutably, so the bindings read
5//! and write every field. The JavaScript and Python bindings map the struct
6//! to an object or a frozen dataclass; the C binding carries it as a value
7//! tape in the order the fields appear in a STEP header, which
8//! [`to_tagged`] and [`from_tagged`] define once.
9
10/// The facade's header record, re-exported so hosts need not name the
11/// facade.
12pub use ifc::Header;
13
14use crate::value::Tagged;
15use crate::{BindingError, IfcModel};
16
17/// Number of top-level fields on the tagged form.
18pub const FIELDS: usize = 10;
19
20impl IfcModel {
21    /// The file header: description, name, time stamp, author,
22    /// organization, preprocessor, originating system, authorization and
23    /// the schema tokens, as written.
24    pub fn header(&self) -> Header {
25        self.inner.header().clone()
26    }
27
28    /// Replace the file header. Every field is written back as given; the
29    /// schema tokens decide which tables subtype queries and validation use.
30    pub fn set_header(&mut self, header: Header) {
31        self.tracked(|this| *this.inner.header_mut() = header);
32    }
33}
34
35/// The header as one tagged `list` of ten values, in STEP header order:
36/// description (list of text), implementation level, name, time stamp,
37/// author (list), organization (list), preprocessor version, originating
38/// system, authorization, schema (list).
39pub fn to_tagged(header: &Header) -> Tagged {
40    let text = |s: &String| Tagged::Text(s.clone());
41    let texts = |v: &Vec<String>| Tagged::List(v.iter().map(text).collect());
42    Tagged::List(vec![
43        texts(&header.description),
44        text(&header.implementation_level),
45        text(&header.name),
46        text(&header.time_stamp),
47        texts(&header.author),
48        texts(&header.organization),
49        text(&header.preprocessor_version),
50        text(&header.originating_system),
51        text(&header.authorization),
52        texts(&header.schema),
53    ])
54}
55
56/// The inverse of [`to_tagged`]; any other shape is `invalid-value`.
57pub fn from_tagged(value: Tagged) -> Result<Header, BindingError> {
58    let Tagged::List(fields) = value else {
59        return Err(invalid("a header must be a list of 10 fields"));
60    };
61    let [description, implementation_level, name, time_stamp, author, organization, preprocessor_version, originating_system, authorization, schema]: [Tagged; FIELDS] =
62        fields.try_into().map_err(|fields: Vec<Tagged>| {
63            invalid(&format!("a header has 10 fields, not {}", fields.len()))
64        })?;
65    Ok(Header {
66        description: texts("description", description)?,
67        implementation_level: text("implementation level", implementation_level)?,
68        name: text("name", name)?,
69        time_stamp: text("time stamp", time_stamp)?,
70        author: texts("author", author)?,
71        organization: texts("organization", organization)?,
72        preprocessor_version: text("preprocessor version", preprocessor_version)?,
73        originating_system: text("originating system", originating_system)?,
74        authorization: text("authorization", authorization)?,
75        schema: texts("schema", schema)?,
76    })
77}
78
79fn invalid(detail: &str) -> BindingError {
80    BindingError::InvalidValue(detail.to_owned())
81}
82
83fn text(field: &str, value: Tagged) -> Result<String, BindingError> {
84    match value {
85        Tagged::Text(s) => Ok(s),
86        other => Err(invalid(&format!(
87            "header {field} must be text, not {}",
88            other.kind().as_str()
89        ))),
90    }
91}
92
93fn texts(field: &str, value: Tagged) -> Result<Vec<String>, BindingError> {
94    match value {
95        Tagged::List(items) => items.into_iter().map(|item| text(field, item)).collect(),
96        other => Err(invalid(&format!(
97            "header {field} must be a list of text, not {}",
98            other.kind().as_str()
99        ))),
100    }
101}