oxigdal_drivers_advanced/
error.rs1use std::io;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, thiserror::Error)]
10pub enum Error {
11 #[error("I/O error: {0}")]
13 Io(#[from] io::Error),
14
15 #[error("JPEG2000 error: {0}")]
17 Jpeg2000(String),
18
19 #[error("GeoPackage error: {0}")]
21 GeoPackage(String),
22
23 #[error("KML error: {0}")]
25 Kml(String),
26
27 #[error("KMZ error: {0}")]
29 Kmz(String),
30
31 #[error("GML error: {0}")]
33 Gml(String),
34
35 #[error("XML parsing error: {0}")]
37 XmlParse(#[from] quick_xml::Error),
38
39 #[error("SQLite error: {0}")]
41 Sqlite(#[from] rusqlite::Error),
42
43 #[error("ZIP error: {0}")]
45 Zip(#[from] oxiarc_core::error::OxiArcError),
46
47 #[error("Invalid format: {0}")]
49 InvalidFormat(String),
50
51 #[error("Unsupported feature: {0}")]
53 UnsupportedFeature(String),
54
55 #[error("Validation error: {0}")]
57 Validation(String),
58
59 #[error("Encoding error: {0}")]
61 Encoding(String),
62
63 #[error("Decoding error: {0}")]
65 Decoding(String),
66
67 #[error("Compression error: {0}")]
69 Compression(String),
70
71 #[error("Decompression error: {0}")]
73 Decompression(String),
74
75 #[error("Missing required field: {0}")]
77 MissingField(String),
78
79 #[error("Invalid CRS: {0}")]
81 InvalidCrs(String),
82
83 #[error("Geometry error: {0}")]
85 Geometry(String),
86
87 #[error("Metadata error: {0}")]
89 Metadata(String),
90
91 #[error("UTF-8 error: {0}")]
93 Utf8(#[from] std::string::FromUtf8Error),
94
95 #[error("String conversion error: {0}")]
97 Utf8Str(#[from] std::str::Utf8Error),
98
99 #[error("Base64 decode error: {0}")]
101 Base64Decode(#[from] base64::DecodeError),
102
103 #[error("Parse integer error: {0}")]
105 ParseInt(#[from] std::num::ParseIntError),
106
107 #[error("Parse float error: {0}")]
109 ParseFloat(#[from] std::num::ParseFloatError),
110
111 #[error("JSON error: {0}")]
113 Json(#[from] serde_json::Error),
114
115 #[error("{context}: {source}")]
117 WithContext {
118 context: String,
120 source: Box<Error>,
122 },
123}
124
125impl Error {
126 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 pub fn jpeg2000<S: Into<String>>(msg: S) -> Self {
136 Self::Jpeg2000(msg.into())
137 }
138
139 pub fn geopackage<S: Into<String>>(msg: S) -> Self {
141 Self::GeoPackage(msg.into())
142 }
143
144 pub fn kml<S: Into<String>>(msg: S) -> Self {
146 Self::Kml(msg.into())
147 }
148
149 pub fn kmz<S: Into<String>>(msg: S) -> Self {
151 Self::Kmz(msg.into())
152 }
153
154 pub fn gml<S: Into<String>>(msg: S) -> Self {
156 Self::Gml(msg.into())
157 }
158
159 pub fn invalid_format<S: Into<String>>(msg: S) -> Self {
161 Self::InvalidFormat(msg.into())
162 }
163
164 pub fn unsupported<S: Into<String>>(msg: S) -> Self {
166 Self::UnsupportedFeature(msg.into())
167 }
168
169 pub fn validation<S: Into<String>>(msg: S) -> Self {
171 Self::Validation(msg.into())
172 }
173
174 pub fn encoding<S: Into<String>>(msg: S) -> Self {
176 Self::Encoding(msg.into())
177 }
178
179 pub fn decoding<S: Into<String>>(msg: S) -> Self {
181 Self::Decoding(msg.into())
182 }
183
184 pub fn missing_field<S: Into<String>>(field: S) -> Self {
186 Self::MissingField(field.into())
187 }
188
189 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}