Skip to main content

openbim_ifc_binding_core/
options.rs

1//! STEP read options, in a host-independent form.
2//!
3//! Mirrors the facade's `ParseOptions`, which is `#[non_exhaustive]` and so
4//! cannot be built field by field outside its crate. Each host fills this
5//! record from its own idiom (a JS object, Python keywords, C flag bits)
6//! and [`ParseOptions::to_facade`] is the one place it becomes the facade's.
7
8use crate::BindingError;
9
10/// What a STEP read does with a data record it cannot parse.
11#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
12pub enum OnMalformed {
13    /// Fail the read. The default: an authoring tool that silently drops
14    /// entities corrupts the file it edits.
15    #[default]
16    Abort,
17    /// Skip the record, report it as a diagnostic, and keep reading.
18    Skip,
19}
20
21impl OnMalformed {
22    /// The host-facing spelling: `abort` or `skip`.
23    pub const fn as_str(self) -> &'static str {
24        match self {
25            Self::Abort => "abort",
26            Self::Skip => "skip",
27        }
28    }
29
30    /// Parse a host-facing spelling; anything else is `invalid-value`.
31    pub fn parse(text: &str) -> Result<Self, BindingError> {
32        match text {
33            "abort" => Ok(Self::Abort),
34            "skip" => Ok(Self::Skip),
35            other => Err(BindingError::InvalidValue(format!(
36                "onMalformed must be \"abort\" or \"skip\", not {other:?}"
37            ))),
38        }
39    }
40}
41
42/// How a STEP read treats damaged or non-conforming input.
43///
44/// The default is [`Self::strict`]. Whatever a lenient read recovers from is
45/// reported, one entry each, by `IfcModel::diagnostics`; header structure
46/// stays fatal either way.
47#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
48pub struct ParseOptions {
49    /// Policy for unparsable data records.
50    pub on_malformed: OnMalformed,
51    /// Report duplicate instance ids and references to ids no record
52    /// defines, as diagnostics. Nothing is dropped or rewritten.
53    pub check_references: bool,
54    /// Read a real written without its decimal point (`1E-05`) as the real
55    /// it spells, with a diagnostic, instead of refusing the file.
56    pub accept_real_without_point: bool,
57}
58
59impl ParseOptions {
60    /// Any malformed record fails the read.
61    pub const fn strict() -> Self {
62        Self {
63            on_malformed: OnMalformed::Abort,
64            check_references: false,
65            accept_real_without_point: false,
66        }
67    }
68
69    /// The facade's lenient preset: skip malformed records and accept reals
70    /// without a decimal point, each reported as a diagnostic.
71    pub const fn lenient() -> Self {
72        Self::from_facade(ifc::ParseOptions::lenient())
73    }
74
75    const fn from_facade(options: ifc::ParseOptions) -> Self {
76        Self {
77            on_malformed: match options.on_malformed_record {
78                ifc::OnMalformed::Skip => OnMalformed::Skip,
79                ifc::OnMalformed::Abort => OnMalformed::Abort,
80            },
81            check_references: options.check_references,
82            accept_real_without_point: options.accept_real_without_point,
83        }
84    }
85
86    /// The facade's options.
87    pub(crate) const fn to_facade(self) -> ifc::ParseOptions {
88        ifc::ParseOptions::strict()
89            .on_malformed_record(match self.on_malformed {
90                OnMalformed::Abort => ifc::OnMalformed::Abort,
91                OnMalformed::Skip => ifc::OnMalformed::Skip,
92            })
93            .check_references(self.check_references)
94            .accept_real_without_point(self.accept_real_without_point)
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn the_presets_match_the_facade() {
104        assert_eq!(ParseOptions::default(), ParseOptions::strict());
105        assert_eq!(
106            ParseOptions::strict().to_facade(),
107            ifc::ParseOptions::strict()
108        );
109        assert_eq!(
110            ParseOptions::lenient().to_facade(),
111            ifc::ParseOptions::lenient()
112        );
113        assert_eq!(ParseOptions::lenient().on_malformed, OnMalformed::Skip);
114    }
115
116    #[test]
117    fn the_policy_spelling_round_trips_and_refuses_anything_else() {
118        for policy in [OnMalformed::Abort, OnMalformed::Skip] {
119            assert_eq!(OnMalformed::parse(policy.as_str()), Ok(policy));
120        }
121        assert!(matches!(
122            OnMalformed::parse("Skip"),
123            Err(BindingError::InvalidValue(_))
124        ));
125    }
126}