Skip to main content

ifc_model/
error.rs

1//! Why a model operation failed.
2
3use crate::value::EntityId;
4use thiserror::Error;
5
6/// Errors from reading, writing, or querying a model.
7///
8/// Codec-specific detail is carried as a message rather than a nested error
9/// type: this crate must not depend on any codec, so it cannot name their
10/// error types.
11#[derive(Debug, Error)]
12#[non_exhaustive]
13pub enum ModelError {
14    /// Underlying I/O failure.
15    #[error("io error: {0}")]
16    Io(String),
17
18    /// The bytes are not this format at all.
19    #[error("not a {expected} file: {detail}")]
20    WrongFormat {
21        /// Format the codec expected.
22        expected: &'static str,
23        /// What was seen instead.
24        detail: String,
25    },
26
27    /// Syntax error at a known location.
28    #[error("syntax error at byte {offset}: {detail}")]
29    Syntax {
30        /// Byte offset into the source.
31        offset: usize,
32        /// What went wrong.
33        detail: String,
34    },
35
36    /// An entity referenced an id that does not exist.
37    #[error("entity {from} references missing entity {to}")]
38    DanglingReference {
39        /// The referring entity.
40        from: EntityId,
41        /// The missing target.
42        to: EntityId,
43    },
44
45    /// The same id was defined twice.
46    #[error("duplicate entity id {0}")]
47    DuplicateId(EntityId),
48
49    /// Serialization failed.
50    #[error("write error: {0}")]
51    Write(String),
52}
53
54/// Convenience alias.
55pub type ModelResult<T> = Result<T, ModelError>;