1
//! Error types for GeoIP parsing.
2

            
3
use std::borrow::Cow;
4
use std::net::AddrParseError;
5
use std::num::ParseIntError;
6
use thiserror::Error;
7

            
8
/// An error type from the tor-geoip crate.
9
#[derive(Clone, Debug, Error)]
10
#[non_exhaustive]
11
pub enum Error {
12
    /// The GeoIP file is formatted wrong.
13
    #[error("Invalid GeoIP data file: {0}")]
14
    BadFormat(Cow<'static, str>),
15

            
16
    /// We got a country code that isn't 2 ASCII letters.
17
    #[error("Unsupported country code in file: {0}")]
18
    BadCountryCode(String),
19

            
20
    /// Tried to use ?? somewhere that expected a country code.
21
    #[error("The 'nowhere' country code ('??') is not supported in this context.")]
22
    NowhereNotSupported,
23
}
24

            
25
impl From<ParseIntError> for Error {
26
    fn from(_e: ParseIntError) -> Error {
27
        Error::BadFormat("can't parse number".into())
28
    }
29
}
30

            
31
impl From<AddrParseError> for Error {
32
    fn from(_e: AddrParseError) -> Error {
33
        Error::BadFormat("can't parse IPv6 address".into())
34
    }
35
}
36

            
37
impl From<crate::dense_range_map::Error> for Error {
38
    fn from(value: crate::dense_range_map::Error) -> Self {
39
        Error::BadFormat(value.to_string().into())
40
    }
41
}