openbim_ifc_binding_core/
options.rs1use crate::BindingError;
9
10#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
12pub enum OnMalformed {
13 #[default]
16 Abort,
17 Skip,
19}
20
21impl OnMalformed {
22 pub const fn as_str(self) -> &'static str {
24 match self {
25 Self::Abort => "abort",
26 Self::Skip => "skip",
27 }
28 }
29
30 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#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
48pub struct ParseOptions {
49 pub on_malformed: OnMalformed,
51 pub check_references: bool,
54 pub accept_real_without_point: bool,
57}
58
59impl ParseOptions {
60 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 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 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}