openbim_ifc_binding_core/
header.rs1pub use ifc::Header;
13
14use crate::value::Tagged;
15use crate::{BindingError, IfcModel};
16
17pub const FIELDS: usize = 10;
19
20impl IfcModel {
21 pub fn header(&self) -> Header {
25 self.inner.header().clone()
26 }
27
28 pub fn set_header(&mut self, header: Header) {
31 self.tracked(|this| *this.inner.header_mut() = header);
32 }
33}
34
35pub 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
56pub 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}