|
1 | | -//! This module provides error types and utilities for the `mvt-reader` crate. |
| 1 | +//! This module provides error types for the `mvt-reader` crate. |
2 | 2 | //! |
3 | | -//! # Errors |
4 | | -//! |
5 | | -//! The `error` module defines the following error types: |
6 | | -//! |
7 | | -//! - `ParserError`: Represents an error that occurs during parsing of a vector tile. |
8 | | -//! - `GeometryError`: Represents an error related to the geometry of a feature in a vector tile. |
9 | | -//! - `TagsError`: Represents an error related to the tags of a feature in a vector tile. |
10 | | -//! - `VersionError`: Represents an error related to the version of a vector tile. |
11 | | -//! - `DecodeError`: Represents an error indicating a decoding failure during the parsing of a vector tile. |
12 | | -//! |
13 | | -//! # Utilities |
14 | | -//! |
15 | | -//! The `error` module also provides utility functions and traits for working with errors, such as formatting and error chaining. |
16 | | -
|
17 | | -/// A structure representing a parser error. |
| 3 | +//! The [`ParserError`] enum represents all possible errors that can occur during |
| 4 | +//! parsing of a vector tile. |
| 5 | +
|
| 6 | +/// An error that can occur during parsing of a vector tile. |
| 7 | +/// |
| 8 | +/// # Examples |
| 9 | +/// |
| 10 | +/// ``` |
| 11 | +/// use mvt_reader::error::ParserError; |
| 12 | +/// |
| 13 | +/// fn example() -> Result<(), ParserError> { |
| 14 | +/// Err(ParserError::InvalidTags) |
| 15 | +/// } |
| 16 | +/// ``` |
18 | 17 | #[derive(Debug)] |
19 | | -pub struct ParserError { |
20 | | - source: Box<dyn core::error::Error>, |
21 | | -} |
| 18 | +pub enum ParserError { |
| 19 | + /// A protobuf decoding failure. |
| 20 | + Decode(prost::DecodeError), |
22 | 21 |
|
23 | | -impl ParserError { |
24 | | - /// Creates a new `ParserError` instance with the provided error source. |
25 | | - /// |
26 | | - /// # Arguments |
27 | | - /// |
28 | | - /// * `source` - The underlying error source. |
29 | | - /// |
30 | | - /// # Examples |
31 | | - /// |
32 | | - /// ``` |
33 | | - /// use mvt_reader::error::ParserError; |
34 | | - /// |
35 | | - /// let source_error = std::io::Error::new(std::io::ErrorKind::Other, "Custom error"); |
36 | | - /// let parser_error = ParserError::new(source_error); |
37 | | - /// ``` |
38 | | - pub fn new<T: core::error::Error + 'static>(source: T) -> Self { |
39 | | - Self { |
40 | | - source: Box::new(source), |
41 | | - } |
42 | | - } |
| 22 | + /// The layer has an unsupported vector tile version. |
| 23 | + UnsupportedVersion { |
| 24 | + layer_name: String, |
| 25 | + version: u32, |
| 26 | + }, |
| 27 | + |
| 28 | + /// The tags section of a feature is malformed. |
| 29 | + InvalidTags, |
| 30 | + |
| 31 | + /// The geometry section of a feature is malformed. |
| 32 | + InvalidGeometry, |
43 | 33 | } |
44 | 34 |
|
45 | 35 | impl core::fmt::Display for ParserError { |
46 | | - /// Formats the error message associated with the `ParserError`. |
47 | | - /// |
48 | | - /// # Arguments |
49 | | - /// |
50 | | - /// * `f` - The formatter to write the output to. |
51 | | - /// |
52 | | - /// # Examples |
53 | | - /// |
54 | | - /// ``` |
55 | | - /// use std::error::Error; |
56 | | - /// use mvt_reader::error::ParserError; |
57 | | - /// |
58 | | - /// let source_error = std::io::Error::new(std::io::ErrorKind::Other, "Custom error"); |
59 | | - /// let parser_error = ParserError::new(source_error); |
60 | | - /// println!("{}", parser_error); |
61 | | - /// ``` |
62 | 36 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
63 | | - self.source.fmt(f) |
| 37 | + match self { |
| 38 | + Self::Decode(source) => write!(f, "Decode error: {source}"), |
| 39 | + Self::UnsupportedVersion { layer_name, version } => write!( |
| 40 | + f, |
| 41 | + "Vector tile version not supported for layer `{layer_name}` (found version: {version})" |
| 42 | + ), |
| 43 | + Self::InvalidTags => write!(f, "Tags section contains errors"), |
| 44 | + Self::InvalidGeometry => write!(f, "Geometry section contains errors"), |
| 45 | + } |
64 | 46 | } |
65 | 47 | } |
66 | 48 |
|
67 | 49 | impl core::error::Error for ParserError { |
68 | | - /// Returns the underlying source of the `ParserError`. |
69 | | - /// |
70 | | - /// # Examples |
71 | | - /// |
72 | | - /// ``` |
73 | | - /// use std::error::Error; |
74 | | - /// use mvt_reader::error::ParserError; |
75 | | - /// |
76 | | - /// let source_error = std::io::Error::new(std::io::ErrorKind::Other, "Custom error"); |
77 | | - /// let parser_error = ParserError::new(source_error); |
78 | | - /// let source = parser_error.source(); |
79 | | - /// println!("Source: {}", source.unwrap()); |
80 | | - /// ``` |
81 | 50 | fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { |
82 | | - Some(self.source.as_ref()) |
83 | | - } |
84 | | -} |
85 | | - |
86 | | -/// A structure representing a version error in a vector tile. |
87 | | -#[derive(Debug)] |
88 | | -pub struct VersionError { |
89 | | - layer_name: String, |
90 | | - |
91 | | - version: u32, |
92 | | -} |
93 | | - |
94 | | -impl VersionError { |
95 | | - /// Creates a new `VersionError` instance with the provided layer name and version. |
96 | | - /// |
97 | | - /// # Arguments |
98 | | - /// |
99 | | - /// * `layer_name` - The name of the layer. |
100 | | - /// * `version` - The unsupported version number. |
101 | | - /// |
102 | | - /// # Examples |
103 | | - /// |
104 | | - /// ``` |
105 | | - /// use mvt_reader::error::VersionError; |
106 | | - /// |
107 | | - /// let layer_name = String::from("my_layer"); |
108 | | - /// let version = 3; |
109 | | - /// let version_error = VersionError::new(layer_name, version); |
110 | | - /// ``` |
111 | | - pub fn new(layer_name: String, version: u32) -> Self { |
112 | | - Self { |
113 | | - layer_name, |
114 | | - version, |
| 51 | + match self { |
| 52 | + Self::Decode(source) => Some(source), |
| 53 | + _ => None, |
115 | 54 | } |
116 | 55 | } |
117 | 56 | } |
118 | 57 |
|
119 | | -impl core::fmt::Display for VersionError { |
120 | | - /// Formats the error message associated with the `VersionError`. |
121 | | - /// |
122 | | - /// # Arguments |
123 | | - /// |
124 | | - /// * `f` - The formatter to write the output to. |
125 | | - /// |
126 | | - /// # Examples |
127 | | - /// |
128 | | - /// ``` |
129 | | - /// use mvt_reader::error::VersionError; |
130 | | - /// |
131 | | - /// let layer_name = String::from("my_layer"); |
132 | | - /// let version = 3; |
133 | | - /// let version_error = VersionError::new(layer_name, version); |
134 | | - /// println!("{}", version_error); |
135 | | - /// ``` |
136 | | - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
137 | | - write!( |
138 | | - f, |
139 | | - "Vector tile version not supported for layer `{}` (found version: {})", |
140 | | - self.layer_name, self.version |
141 | | - ) |
142 | | - } |
143 | | -} |
144 | | - |
145 | | -impl core::error::Error for VersionError {} |
146 | | - |
147 | | -/// An error indicating that the tags section of a vector tile contains errors. |
148 | | -#[derive(Debug, Default)] |
149 | | -pub struct TagsError; |
150 | | - |
151 | | -impl TagsError { |
152 | | - /// Creates a new `TagsError` instance. |
153 | | - /// |
154 | | - /// # Examples |
155 | | - /// |
156 | | - /// ``` |
157 | | - /// use mvt_reader::error::TagsError; |
158 | | - /// |
159 | | - /// let tags_error = TagsError::new(); |
160 | | - /// ``` |
161 | | - pub fn new() -> Self { |
162 | | - Self |
| 58 | +impl From<prost::DecodeError> for ParserError { |
| 59 | + fn from(error: prost::DecodeError) -> Self { |
| 60 | + Self::Decode(error) |
163 | 61 | } |
164 | 62 | } |
165 | | - |
166 | | -impl core::fmt::Display for TagsError { |
167 | | - /// Formats the error message associated with the `TagsError`. |
168 | | - /// |
169 | | - /// # Arguments |
170 | | - /// |
171 | | - /// * `f` - The formatter to write the output to. |
172 | | - /// |
173 | | - /// # Examples |
174 | | - /// |
175 | | - /// ``` |
176 | | - /// use mvt_reader::error::TagsError; |
177 | | - /// |
178 | | - /// let tags_error = TagsError::new(); |
179 | | - /// println!("{}", tags_error); |
180 | | - /// ``` |
181 | | - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
182 | | - write!(f, "Tags section contains errors") |
183 | | - } |
184 | | -} |
185 | | - |
186 | | -impl core::error::Error for TagsError {} |
187 | | - |
188 | | -/// An error indicating that the geometry section of a vector tile contains errors. |
189 | | -#[derive(Debug, Default)] |
190 | | -pub struct GeometryError; |
191 | | - |
192 | | -impl GeometryError { |
193 | | - /// Creates a new `GeometryError` instance. |
194 | | - /// |
195 | | - /// # Examples |
196 | | - /// |
197 | | - /// ``` |
198 | | - /// use mvt_reader::error::GeometryError; |
199 | | - /// |
200 | | - /// let geometry_error = GeometryError::new(); |
201 | | - /// ``` |
202 | | - pub fn new() -> Self { |
203 | | - Self |
204 | | - } |
205 | | -} |
206 | | - |
207 | | -impl core::fmt::Display for GeometryError { |
208 | | - /// Formats the error message associated with the `GeometryError`. |
209 | | - /// |
210 | | - /// # Arguments |
211 | | - /// |
212 | | - /// * `f` - The formatter to write the output to. |
213 | | - /// |
214 | | - /// # Examples |
215 | | - /// |
216 | | - /// ``` |
217 | | - /// use mvt_reader::error::GeometryError; |
218 | | - /// |
219 | | - /// let geometry_error = GeometryError::new(); |
220 | | - /// println!("{}", geometry_error); |
221 | | - /// ``` |
222 | | - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
223 | | - write!(f, "Geometry section contains errors") |
224 | | - } |
225 | | -} |
226 | | - |
227 | | -impl core::error::Error for GeometryError {} |
228 | | - |
229 | | -/// An error indicating a decoding failure during the parsing of a vector tile. |
230 | | -#[derive(Debug)] |
231 | | -pub struct DecodeError { |
232 | | - source: Box<dyn core::error::Error>, |
233 | | -} |
234 | | - |
235 | | -impl DecodeError { |
236 | | - /// Creates a new DecodeError instance with the provided decoding error from prost. |
237 | | - /// |
238 | | - /// # Arguments |
239 | | - /// |
240 | | - /// * source - The underlying decoding error from prost. |
241 | | - pub fn new(source: Box<dyn core::error::Error>) -> Self { |
242 | | - Self { source } |
243 | | - } |
244 | | -} |
245 | | - |
246 | | -impl core::fmt::Display for DecodeError { |
247 | | - /// Formats the error message associated with the `DecodeError`. |
248 | | - /// |
249 | | - /// # Arguments |
250 | | - /// |
251 | | - /// * `f` - The formatter to write the output to. |
252 | | - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
253 | | - write!(f, "Decode error: {}", self.source) |
254 | | - } |
255 | | -} |
256 | | - |
257 | | -impl core::error::Error for DecodeError {} |
0 commit comments