Skip to main content

fcb_core/
error.rs

1use crate::packed_rtree::Error as PackedRtreeError;
2use cjseq::CjseqError;
3use flatbuffers::InvalidFlatbuffer;
4use serde_json;
5use thiserror::Error;
6
7/// The main error type for the FCB Core library.
8/// This enum represents all possible errors that can occur during FCB operations.
9#[derive(Debug, Error)]
10pub enum Error {
11    // File format errors
12    #[error("Missing magic bytes in FCB file header")]
13    MissingMagicBytes,
14
15    #[error("Required index is missing")]
16    NoIndex,
17
18    #[error("Attribute index not found")]
19    AttributeIndexNotFound,
20
21    #[error("Attribute index size overflow")]
22    AttributeIndexSizeOverflow,
23
24    #[error("No columns found in header")]
25    NoColumnsInHeader,
26
27    #[error("Missing required field of CityJSON: {0}")]
28    MissingRequiredField(String),
29
30    #[error("Invalid header size {0}, expected size between 8 and 1MB")]
31    IllegalHeaderSize(usize),
32
33    #[error("Invalid FlatBuffer format: {0}")]
34    InvalidFlatbuffer(#[from] InvalidFlatbuffer),
35
36    // IO and serialization errors
37    #[error("IO error: {0}")]
38    IoError(#[from] std::io::Error),
39
40    #[error("JSON error: {0}")]
41    JsonError(#[from] serde_json::Error),
42
43    #[error("R-tree error: {0}")]
44    RtreeError(#[from] PackedRtreeError),
45
46    // Validation errors
47    #[error("Unsupported column type: {0}")]
48    UnsupportedColumnType(String),
49
50    #[error("Invalid attribute value: {msg}")]
51    InvalidAttributeValue { msg: String },
52
53    /// A stored FlatBuffers enumeration tag with no CityJSON spelling. The
54    /// permitted values are fixed by `appearance.schema.json`, so an
55    /// unrecognised tag means the file was written by a newer writer or is
56    /// corrupt. Reported rather than defaulted: a silent default is exactly
57    /// how a texture written `"wrapMode": "wrap"` came back as `"None"`.
58    #[error("Unknown FlatCityBuf `{0}` tag {1}")]
59    UnknownEnumTag(&'static str, String),
60
61    // Index and query errors
62    #[error("Failed to create index: {0}")]
63    IndexCreationError(String),
64
65    #[error("Failed to execute query: {0}")]
66    QueryExecutionError(String),
67
68    // HTTP errors (when http feature is enabled)
69    #[cfg(feature = "http")]
70    #[error("HTTP client error: {0}")]
71    HttpClient(#[from] http_range_client::HttpError),
72
73    // CityJSON specific errors
74    #[error("CityJSON error: {source}")]
75    CityJson {
76        #[from]
77        source: crate::cjerror::CjError,
78    },
79
80    #[error("Cjseq error: {source}")]
81    CjseqError {
82        #[from]
83        source: CjseqError,
84    },
85
86    #[error("StaticBTree error: {source}")]
87    StaticBTree {
88        #[from]
89        source: crate::static_btree::Error,
90    },
91}
92
93impl Error {
94    /// Returns true if the error is related to IO operations
95    pub fn is_io_error(&self) -> bool {
96        matches!(self, Error::IoError(_))
97    }
98
99    /// Returns true if the error is related to data format
100    pub fn is_format_error(&self) -> bool {
101        matches!(
102            self,
103            Error::MissingMagicBytes | Error::InvalidFlatbuffer(_) | Error::IllegalHeaderSize(_)
104        )
105    }
106
107    /// Returns true if the error is related to validation
108    pub fn is_validation_error(&self) -> bool {
109        matches!(
110            self,
111            Error::UnsupportedColumnType(_) | Error::InvalidAttributeValue { .. }
112        )
113    }
114
115    /// Returns true if the error is related to index or query operations
116    pub fn is_index_error(&self) -> bool {
117        matches!(
118            self,
119            Error::IndexCreationError(_) | Error::QueryExecutionError(_)
120        )
121    }
122}
123
124pub type Result<T> = std::result::Result<T, Error>;