Skip to main content

reverse_geocoder_cn/
error.rs

1//! Error types returned by the library and data builder.
2
3/// Errors returned while opening or decoding a local binary data file.
4#[derive(Debug)]
5pub enum Error {
6    Io(std::io::Error),
7    InvalidData(&'static str),
8    Decode(bincode::error::DecodeError),
9}
10
11impl std::fmt::Display for Error {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        match self {
14            Self::Io(error) => write!(f, "{error}"),
15            Self::InvalidData(message) => f.write_str(message),
16            Self::Decode(error) => write!(f, "cannot decode data: {error}"),
17        }
18    }
19}
20
21impl std::error::Error for Error {}
22impl From<std::io::Error> for Error {
23    fn from(value: std::io::Error) -> Self {
24        Self::Io(value)
25    }
26}
27
28/// Errors returned while converting source GeoJSON into a binary data file.
29#[derive(Debug)]
30pub enum BuildError {
31    Io(std::io::Error),
32    Json(serde_json::Error),
33    Encode(bincode::error::EncodeError),
34    InvalidGeoJson(String),
35}
36
37impl std::fmt::Display for BuildError {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            Self::Io(error) => write!(f, "{error}"),
41            Self::Json(error) => write!(f, "invalid GeoJSON: {error}"),
42            Self::Encode(error) => write!(f, "cannot encode data: {error}"),
43            Self::InvalidGeoJson(message) => f.write_str(message),
44        }
45    }
46}
47
48impl std::error::Error for BuildError {}
49impl From<std::io::Error> for BuildError {
50    fn from(value: std::io::Error) -> Self {
51        Self::Io(value)
52    }
53}
54impl From<serde_json::Error> for BuildError {
55    fn from(value: serde_json::Error) -> Self {
56        Self::Json(value)
57    }
58}
59impl From<bincode::error::EncodeError> for BuildError {
60    fn from(value: bincode::error::EncodeError) -> Self {
61        Self::Encode(value)
62    }
63}
64
65/// A degree-minute coordinate string could not be parsed.
66#[derive(Debug, Clone, PartialEq, Eq)]
67pub struct CoordinateParseError(pub(crate) &'static str);
68
69impl std::fmt::Display for CoordinateParseError {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        f.write_str(self.0)
72    }
73}
74
75impl std::error::Error for CoordinateParseError {}