1use std::fmt;
13
14#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum BindingError {
17 Parse(String),
19 Write(String),
21 MissingEntity(u64),
23 InvalidValue(String),
25 OutOfRange(String),
27 UnsupportedSchema(String),
31 Io(String),
33 UnsupportedProfile(String),
36 FeatureDisabled(&'static str),
39 InvalidModel(String),
46 MissingReference(String),
49 BudgetExceeded(String),
52 Unsupported(String),
56 WrongEntityType(String),
60 TemplateViolation(String),
65 MissingProperty(String),
68 CatalogNotLoaded(String),
72 UnknownAttribute(String),
76 DerivedAttribute(String),
79 MissingAttribute(String),
82 StillReferenced(String),
85 TypeMismatch(String),
90 AmbiguousValue(String),
94 InvalidSelector {
97 detail: String,
99 span: (usize, usize),
101 },
102 InverseAttribute(String),
105 ExpressMismatch(String),
109 InvalidExpress(String),
112 HistoryEmpty(String),
115 HistoryStale(String),
118}
119
120impl BindingError {
121 pub const fn code(&self) -> &'static str {
123 match self {
124 Self::Parse(_) => "parse",
125 Self::Write(_) => "write",
126 Self::MissingEntity(_) => "missing-entity",
127 Self::InvalidValue(_) => "invalid-value",
128 Self::OutOfRange(_) => "out-of-range",
129 Self::UnsupportedSchema(_) => "unsupported-schema",
130 Self::Io(_) => "io",
131 Self::UnsupportedProfile(_) => "unsupported-profile",
132 Self::FeatureDisabled(_) => "feature-disabled",
133 Self::InvalidModel(_) => "invalid-model",
134 Self::MissingReference(_) => "missing-reference",
135 Self::BudgetExceeded(_) => "budget-exceeded",
136 Self::Unsupported(_) => "unsupported",
137 Self::WrongEntityType(_) => "wrong-entity-type",
138 Self::TemplateViolation(_) => "template-violation",
139 Self::MissingProperty(_) => "missing-property",
140 Self::CatalogNotLoaded(_) => "catalog-not-loaded",
141 Self::UnknownAttribute(_) => "unknown-attribute",
142 Self::DerivedAttribute(_) => "derived-attribute",
143 Self::MissingAttribute(_) => "missing-attribute",
144 Self::StillReferenced(_) => "still-referenced",
145 Self::TypeMismatch(_) => "type-mismatch",
146 Self::AmbiguousValue(_) => "ambiguous-value",
147 Self::InverseAttribute(_) => "inverse-attribute",
148 Self::InvalidSelector { .. } => "invalid-selector",
149 Self::ExpressMismatch(_) => "express-mismatch",
150 Self::InvalidExpress(_) => "invalid-express",
151 Self::HistoryEmpty(_) => "history-empty",
152 Self::HistoryStale(_) => "history-stale",
153 }
154 }
155
156 pub const fn span(&self) -> Option<(usize, usize)> {
158 match self {
159 Self::InvalidSelector { span, .. } => Some(*span),
160 _ => None,
161 }
162 }
163}
164
165impl fmt::Display for BindingError {
166 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167 match self {
168 Self::Parse(detail) => write!(f, "cannot parse {detail}"),
170 Self::Write(detail) => write!(f, "cannot write {detail}"),
171 Self::MissingEntity(id) => write!(f, "no entity #{id}"),
172 Self::InvalidValue(detail) => write!(f, "invalid IFC value: {detail}"),
173 Self::OutOfRange(detail) => write!(f, "out of range: {detail}"),
174 Self::UnsupportedSchema(detail) => write!(f, "unsupported schema: {detail:?}"),
177 Self::Io(detail) => write!(f, "cannot read file: {detail}"),
178 Self::UnsupportedProfile(token) => write!(
179 f,
180 "no ifcXML XSD profile for {token:?} (IFC4 or IFC4X3_ADD2)"
181 ),
182 Self::FeatureDisabled(feature) => {
183 write!(f, "this build leaves out the `{feature}` feature")
184 }
185 Self::InvalidModel(detail) => write!(f, "invalid model: {detail}"),
186 Self::MissingReference(detail) => write!(f, "missing reference: {detail}"),
187 Self::BudgetExceeded(detail) => write!(f, "budget exceeded: {detail}"),
188 Self::Unsupported(detail) => write!(f, "unsupported: {detail}"),
189 Self::WrongEntityType(detail) => write!(f, "wrong entity type: {detail}"),
190 Self::TemplateViolation(detail) => write!(f, "template violation: {detail}"),
191 Self::MissingProperty(detail) => write!(f, "missing property: {detail}"),
192 Self::CatalogNotLoaded(detail) => write!(
193 f,
194 "catalog not loaded: {detail}; load the release's catalog first \
195 (JavaScript: `await IfcModel.loadCatalog(release)`)"
196 ),
197 Self::UnknownAttribute(detail) => write!(f, "unknown attribute: {detail}"),
198 Self::DerivedAttribute(detail) => write!(f, "derived attribute: {detail}"),
199 Self::MissingAttribute(detail) => write!(f, "missing attribute: {detail}"),
200 Self::StillReferenced(detail) => write!(f, "still referenced: {detail}"),
201 Self::TypeMismatch(detail) => write!(f, "type mismatch: {detail}"),
202 Self::AmbiguousValue(detail) => write!(
203 f,
204 "ambiguous value: {detail}; pass an explicit typed value instead"
205 ),
206 Self::InvalidSelector { detail, span } => write!(
207 f,
208 "invalid selector: {detail} at bytes {}..{}",
209 span.0, span.1
210 ),
211 Self::InverseAttribute(detail) => write!(f, "inverse attribute: {detail}"),
212 Self::ExpressMismatch(detail) => write!(f, "EXPRESS mismatch: {detail}"),
213 Self::InvalidExpress(detail) => write!(f, "invalid EXPRESS: {detail}"),
214 Self::HistoryEmpty(detail) => write!(f, "history empty: {detail}"),
215 Self::HistoryStale(detail) => write!(f, "history stale: {detail}"),
216 }
217 }
218}
219
220impl std::error::Error for BindingError {}
221
222#[cfg(test)]
223mod tests {
224 use std::collections::BTreeSet;
225
226 use super::BindingError;
227
228 const RELEASED_CODES: &[&str] = &[
231 "parse",
232 "write",
233 "missing-entity",
234 "invalid-value",
235 "out-of-range",
236 "unsupported-schema",
237 "io",
238 "unsupported-profile",
239 "feature-disabled",
240 "invalid-model",
241 "missing-reference",
242 "budget-exceeded",
243 "unsupported",
244 "wrong-entity-type",
245 "template-violation",
246 "missing-property",
247 "catalog-not-loaded",
248 "unknown-attribute",
249 "derived-attribute",
250 "missing-attribute",
251 "still-referenced",
252 "type-mismatch",
253 "ambiguous-value",
254 "invalid-selector",
255 "inverse-attribute",
256 "express-mismatch",
257 "invalid-express",
258 "history-empty",
259 "history-stale",
260 ];
261
262 fn one_of_each() -> Vec<BindingError> {
264 let all = vec![
265 BindingError::Parse(String::new()),
266 BindingError::Write(String::new()),
267 BindingError::MissingEntity(0),
268 BindingError::InvalidValue(String::new()),
269 BindingError::OutOfRange(String::new()),
270 BindingError::UnsupportedSchema(String::new()),
271 BindingError::Io(String::new()),
272 BindingError::UnsupportedProfile(String::new()),
273 BindingError::FeatureDisabled(""),
274 BindingError::InvalidModel(String::new()),
275 BindingError::MissingReference(String::new()),
276 BindingError::BudgetExceeded(String::new()),
277 BindingError::Unsupported(String::new()),
278 BindingError::WrongEntityType(String::new()),
279 BindingError::TemplateViolation(String::new()),
280 BindingError::MissingProperty(String::new()),
281 BindingError::CatalogNotLoaded(String::new()),
282 BindingError::UnknownAttribute(String::new()),
283 BindingError::DerivedAttribute(String::new()),
284 BindingError::MissingAttribute(String::new()),
285 BindingError::StillReferenced(String::new()),
286 BindingError::TypeMismatch(String::new()),
287 BindingError::AmbiguousValue(String::new()),
288 BindingError::InvalidSelector {
289 detail: String::new(),
290 span: (0, 0),
291 },
292 BindingError::InverseAttribute(String::new()),
293 BindingError::ExpressMismatch(String::new()),
294 BindingError::InvalidExpress(String::new()),
295 BindingError::HistoryEmpty(String::new()),
296 BindingError::HistoryStale(String::new()),
297 ];
298 for error in &all {
301 match error {
302 BindingError::Parse(_)
303 | BindingError::Write(_)
304 | BindingError::MissingEntity(_)
305 | BindingError::InvalidValue(_)
306 | BindingError::OutOfRange(_)
307 | BindingError::UnsupportedSchema(_)
308 | BindingError::Io(_)
309 | BindingError::UnsupportedProfile(_)
310 | BindingError::FeatureDisabled(_)
311 | BindingError::InvalidModel(_)
312 | BindingError::MissingReference(_)
313 | BindingError::BudgetExceeded(_)
314 | BindingError::Unsupported(_)
315 | BindingError::WrongEntityType(_)
316 | BindingError::TemplateViolation(_)
317 | BindingError::MissingProperty(_)
318 | BindingError::CatalogNotLoaded(_)
319 | BindingError::UnknownAttribute(_)
320 | BindingError::DerivedAttribute(_)
321 | BindingError::MissingAttribute(_)
322 | BindingError::StillReferenced(_)
323 | BindingError::TypeMismatch(_)
324 | BindingError::AmbiguousValue(_)
325 | BindingError::InvalidSelector { .. }
326 | BindingError::InverseAttribute(_)
327 | BindingError::ExpressMismatch(_)
328 | BindingError::InvalidExpress(_)
329 | BindingError::HistoryEmpty(_)
330 | BindingError::HistoryStale(_) => {}
331 }
332 }
333 all
334 }
335
336 #[test]
337 fn released_codes_are_never_renamed_or_reused() {
338 let codes: Vec<&str> = one_of_each().iter().map(BindingError::code).collect();
339 assert!(
340 codes.len() >= RELEASED_CODES.len(),
341 "a BindingError variant was removed: {codes:?}"
342 );
343 assert_eq!(
344 &codes[..RELEASED_CODES.len()],
345 RELEASED_CODES,
346 "a released BindingError code changed; add a new code instead"
347 );
348 let unique: BTreeSet<&str> = codes.iter().copied().collect();
349 assert_eq!(
350 unique.len(),
351 codes.len(),
352 "two variants share a code: {codes:?}"
353 );
354 }
355}