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