h3o_mvt/
error.rs

1use geozero::error::GeozeroError;
2use h3o::error::DissolutionError;
3use std::{error::Error, fmt};
4
5/// Error occurring while rendering a set of cell indices to MVT.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum InvalidTileID {
9    /// Invalid X coordinate.
10    InvalidX(u32),
11    /// Invalid Y coordinate.
12    InvalidY(u32),
13    /// Invalid Z coordinate.
14    InvalidZ(u32),
15}
16
17impl fmt::Display for InvalidTileID {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match *self {
20            Self::InvalidX(value) => {
21                write!(f, "invalid x coordinate: {value}")
22            }
23            Self::InvalidY(value) => {
24                write!(f, "invalid y coordinate: {value}")
25            }
26            Self::InvalidZ(value) => {
27                write!(f, "invalid z coordinate: {value}")
28            }
29        }
30    }
31}
32
33impl Error for InvalidTileID {
34    fn source(&self) -> Option<&(dyn Error + 'static)> {
35        match *self {
36            Self::InvalidX(_) | Self::InvalidY(_) | Self::InvalidZ(_) => None,
37        }
38    }
39}
40
41/// Errors occurring while rendering a set of cell indices to MVT.
42#[derive(Debug)]
43#[non_exhaustive]
44pub enum RenderingError {
45    /// Invalid input.
46    InvalidInput(DissolutionError),
47    /// MVT encoding failed.
48    Encoding(GeozeroError),
49}
50
51impl fmt::Display for RenderingError {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match *self {
54            Self::InvalidInput(ref source) => {
55                write!(f, "invalid input: {source}")
56            }
57            Self::Encoding(ref source) => {
58                write!(f, "MVT encoding failed: {source}")
59            }
60        }
61    }
62}
63
64impl Error for RenderingError {
65    fn source(&self) -> Option<&(dyn Error + 'static)> {
66        match *self {
67            Self::InvalidInput(ref source) => Some(source),
68            Self::Encoding(ref source) => Some(source),
69        }
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    // All error must have a non-empty display.
78    #[test]
79    fn display() {
80        assert!(
81            !RenderingError::InvalidInput(DissolutionError::DuplicateInput)
82                .to_string()
83                .is_empty()
84        );
85        assert!(
86            !RenderingError::Encoding(GeozeroError::GeometryFormat)
87                .to_string()
88                .is_empty()
89        );
90        assert!(!InvalidTileID::InvalidX(42).to_string().is_empty());
91        assert!(!InvalidTileID::InvalidY(42).to_string().is_empty());
92        assert!(!InvalidTileID::InvalidZ(42).to_string().is_empty());
93    }
94
95    #[test]
96    fn source() {
97        assert!(
98            RenderingError::InvalidInput(
99                DissolutionError::UnsupportedResolution
100            )
101            .source()
102            .is_some()
103        );
104        assert!(
105            RenderingError::Encoding(GeozeroError::GeometryFormat)
106                .source()
107                .is_some()
108        );
109        assert!(InvalidTileID::InvalidX(42).source().is_none());
110        assert!(InvalidTileID::InvalidY(42).source().is_none());
111        assert!(InvalidTileID::InvalidZ(42).source().is_none());
112    }
113}