mvt_reader/error.rs
1//! This module provides error types and utilities for the `mvt-reader` crate.
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.
18#[derive(Debug)]
19pub struct ParserError {
20 source: Box<dyn core::error::Error>,
21}
22
23impl 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 }
43}
44
45impl 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 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
63 self.source.fmt(f)
64 }
65}
66
67impl 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 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)]
88pub struct VersionError {
89 layer_name: String,
90
91 version: u32,
92}
93
94impl 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,
115 }
116 }
117}
118
119impl 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
145impl core::error::Error for VersionError {}
146
147/// An error indicating that the tags section of a vector tile contains errors.
148#[derive(Debug, Default)]
149pub struct TagsError;
150
151impl 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
163 }
164}
165
166impl 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
186impl core::error::Error for TagsError {}
187
188/// An error indicating that the geometry section of a vector tile contains errors.
189#[derive(Debug, Default)]
190pub struct GeometryError;
191
192impl 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
207impl 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
227impl core::error::Error for GeometryError {}
228
229/// An error indicating a decoding failure during the parsing of a vector tile.
230#[derive(Debug)]
231pub struct DecodeError {
232 source: Box<dyn core::error::Error>,
233}
234
235impl 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
246impl 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
257impl core::error::Error for DecodeError {}