Skip to main content

oxigdal_drivers_advanced/
error.rs

1//! Error types for advanced format drivers.
2
3use std::io;
4
5/// Result type for advanced format operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur when working with advanced geospatial formats.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// I/O error
12    #[error("I/O error: {0}")]
13    Io(#[from] io::Error),
14
15    /// JPEG2000 format error
16    #[error("JPEG2000 error: {0}")]
17    Jpeg2000(String),
18
19    /// GeoPackage format error
20    #[error("GeoPackage error: {0}")]
21    GeoPackage(String),
22
23    /// KML format error
24    #[error("KML error: {0}")]
25    Kml(String),
26
27    /// KMZ format error
28    #[error("KMZ error: {0}")]
29    Kmz(String),
30
31    /// GML format error
32    #[error("GML error: {0}")]
33    Gml(String),
34
35    /// XML parsing error
36    #[error("XML parsing error: {0}")]
37    XmlParse(#[from] quick_xml::Error),
38
39    /// SQLite database error
40    #[error("SQLite error: {0}")]
41    Sqlite(#[from] rusqlite::Error),
42
43    /// ZIP archive error
44    #[error("ZIP error: {0}")]
45    Zip(#[from] oxiarc_core::error::OxiArcError),
46
47    /// Invalid format error
48    #[error("Invalid format: {0}")]
49    InvalidFormat(String),
50
51    /// Unsupported feature error
52    #[error("Unsupported feature: {0}")]
53    UnsupportedFeature(String),
54
55    /// Validation error
56    #[error("Validation error: {0}")]
57    Validation(String),
58
59    /// Encoding error
60    #[error("Encoding error: {0}")]
61    Encoding(String),
62
63    /// Decoding error
64    #[error("Decoding error: {0}")]
65    Decoding(String),
66
67    /// Compression error
68    #[error("Compression error: {0}")]
69    Compression(String),
70
71    /// Decompression error
72    #[error("Decompression error: {0}")]
73    Decompression(String),
74
75    /// Missing required field
76    #[error("Missing required field: {0}")]
77    MissingField(String),
78
79    /// Invalid coordinate reference system
80    #[error("Invalid CRS: {0}")]
81    InvalidCrs(String),
82
83    /// Geometry error
84    #[error("Geometry error: {0}")]
85    Geometry(String),
86
87    /// Metadata error
88    #[error("Metadata error: {0}")]
89    Metadata(String),
90
91    /// UTF-8 conversion error
92    #[error("UTF-8 error: {0}")]
93    Utf8(#[from] std::string::FromUtf8Error),
94
95    /// String conversion error
96    #[error("String conversion error: {0}")]
97    Utf8Str(#[from] std::str::Utf8Error),
98
99    /// Base64 decode error
100    #[error("Base64 decode error: {0}")]
101    Base64Decode(#[from] base64::DecodeError),
102
103    /// Parse integer error
104    #[error("Parse integer error: {0}")]
105    ParseInt(#[from] std::num::ParseIntError),
106
107    /// Parse float error
108    #[error("Parse float error: {0}")]
109    ParseFloat(#[from] std::num::ParseFloatError),
110
111    /// JSON error
112    #[error("JSON error: {0}")]
113    Json(#[from] serde_json::Error),
114
115    /// Custom error with context
116    #[error("{context}: {source}")]
117    WithContext {
118        /// Error context
119        context: String,
120        /// Source error
121        source: Box<Error>,
122    },
123}
124
125impl Error {
126    /// Add context to an error.
127    pub fn with_context<S: Into<String>>(self, context: S) -> Self {
128        Self::WithContext {
129            context: context.into(),
130            source: Box::new(self),
131        }
132    }
133
134    /// Create a JPEG2000 error.
135    pub fn jpeg2000<S: Into<String>>(msg: S) -> Self {
136        Self::Jpeg2000(msg.into())
137    }
138
139    /// Create a GeoPackage error.
140    pub fn geopackage<S: Into<String>>(msg: S) -> Self {
141        Self::GeoPackage(msg.into())
142    }
143
144    /// Create a KML error.
145    pub fn kml<S: Into<String>>(msg: S) -> Self {
146        Self::Kml(msg.into())
147    }
148
149    /// Create a KMZ error.
150    pub fn kmz<S: Into<String>>(msg: S) -> Self {
151        Self::Kmz(msg.into())
152    }
153
154    /// Create a GML error.
155    pub fn gml<S: Into<String>>(msg: S) -> Self {
156        Self::Gml(msg.into())
157    }
158
159    /// Create an invalid format error.
160    pub fn invalid_format<S: Into<String>>(msg: S) -> Self {
161        Self::InvalidFormat(msg.into())
162    }
163
164    /// Create an unsupported feature error.
165    pub fn unsupported<S: Into<String>>(msg: S) -> Self {
166        Self::UnsupportedFeature(msg.into())
167    }
168
169    /// Create a validation error.
170    pub fn validation<S: Into<String>>(msg: S) -> Self {
171        Self::Validation(msg.into())
172    }
173
174    /// Create an encoding error.
175    pub fn encoding<S: Into<String>>(msg: S) -> Self {
176        Self::Encoding(msg.into())
177    }
178
179    /// Create a decoding error.
180    pub fn decoding<S: Into<String>>(msg: S) -> Self {
181        Self::Decoding(msg.into())
182    }
183
184    /// Create a missing field error.
185    pub fn missing_field<S: Into<String>>(field: S) -> Self {
186        Self::MissingField(field.into())
187    }
188
189    /// Create a geometry error.
190    pub fn geometry<S: Into<String>>(msg: S) -> Self {
191        Self::Geometry(msg.into())
192    }
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198
199    #[test]
200    fn test_error_creation() {
201        let err = Error::jpeg2000("test error");
202        assert!(matches!(err, Error::Jpeg2000(_)));
203
204        let err = Error::geopackage("gpkg error");
205        assert!(matches!(err, Error::GeoPackage(_)));
206
207        let err = Error::kml("kml error");
208        assert!(matches!(err, Error::Kml(_)));
209    }
210
211    #[test]
212    fn test_error_with_context() {
213        let err = Error::jpeg2000("base error").with_context("reading file");
214        assert!(matches!(err, Error::WithContext { .. }));
215    }
216
217    #[test]
218    fn test_error_display() {
219        let err = Error::validation("invalid data");
220        let display = format!("{}", err);
221        assert!(display.contains("Validation error"));
222    }
223}