Skip to main content

tor_geoip/
err.rs

1//! Error types for GeoIP parsing.
2
3use std::borrow::Cow;
4use std::net::AddrParseError;
5use std::num::ParseIntError;
6use thiserror::Error;
7
8/// An error type from the tor-geoip crate.
9#[derive(Clone, Debug, Error)]
10#[non_exhaustive]
11pub 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
25impl From<ParseIntError> for Error {
26    fn from(_e: ParseIntError) -> Error {
27        Error::BadFormat("can't parse number".into())
28    }
29}
30
31impl From<AddrParseError> for Error {
32    fn from(_e: AddrParseError) -> Error {
33        Error::BadFormat("can't parse IPv6 address".into())
34    }
35}
36
37impl 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}