Skip to main content

bimifc_model/
error.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Error types for IFC parsing operations
6
7use crate::EntityId;
8use thiserror::Error;
9
10/// Result type alias for parser operations
11pub type Result<T> = std::result::Result<T, ParseError>;
12
13/// Errors that can occur during IFC parsing
14#[derive(Error, Debug)]
15pub enum ParseError {
16    /// Invalid IFC file format
17    #[error("Invalid IFC format: {0}")]
18    InvalidFormat(String),
19
20    /// Failed to parse header section
21    #[error("Invalid header: {0}")]
22    InvalidHeader(String),
23
24    /// Failed to parse entity
25    #[error("Failed to parse entity {0}: {1}")]
26    EntityParse(EntityId, String),
27
28    /// Entity not found
29    #[error("Entity {0} not found")]
30    EntityNotFound(EntityId),
31
32    /// Invalid entity reference
33    #[error("Invalid entity reference at {entity}: attribute {attribute}")]
34    InvalidReference { entity: EntityId, attribute: usize },
35
36    /// Type mismatch when accessing attribute
37    #[error(
38        "Type mismatch at entity {entity} attribute {attribute}: expected {expected}, got {actual}"
39    )]
40    TypeMismatch {
41        entity: EntityId,
42        attribute: usize,
43        expected: String,
44        actual: String,
45    },
46
47    /// Missing required attribute
48    #[error("Missing required attribute {attribute} on entity {entity}")]
49    MissingAttribute { entity: EntityId, attribute: usize },
50
51    /// Unsupported IFC schema version
52    #[error("Unsupported schema version: {0}")]
53    UnsupportedSchema(String),
54
55    /// Geometry processing error
56    #[error("Geometry error for entity {entity}: {message}")]
57    Geometry { entity: EntityId, message: String },
58
59    /// IO error
60    #[error("IO error: {0}")]
61    Io(#[from] std::io::Error),
62
63    /// Generic error with message
64    #[error("{0}")]
65    Other(String),
66}
67
68impl ParseError {
69    /// Create a new format error
70    pub fn format(msg: impl Into<String>) -> Self {
71        ParseError::InvalidFormat(msg.into())
72    }
73
74    /// Create a new entity parse error
75    pub fn entity_parse(id: EntityId, msg: impl Into<String>) -> Self {
76        ParseError::EntityParse(id, msg.into())
77    }
78
79    /// Create a new geometry error
80    pub fn geometry(entity: EntityId, msg: impl Into<String>) -> Self {
81        ParseError::Geometry {
82            entity,
83            message: msg.into(),
84        }
85    }
86
87    /// Create a generic error
88    pub fn other(msg: impl Into<String>) -> Self {
89        ParseError::Other(msg.into())
90    }
91}