ip2location/
error.rs

1use std::{fmt, io};
2
3#[derive(Debug, PartialEq, Eq)]
4pub enum Error {
5    GenericError(String),
6    IoError(String),
7    RecordNotFound,
8    UnknownDb,
9    InvalidBinDatabase(u8, u8),
10}
11
12impl From<io::Error> for Error {
13    fn from(err: io::Error) -> Error {
14        Error::IoError(err.to_string())
15    }
16}
17
18// Use default implementation for `std::error::Error`
19impl std::error::Error for Error {}
20
21impl From<&str> for Error {
22    fn from(err: &str) -> Error {
23        Error::GenericError(err.to_string())
24    }
25}
26
27impl From<std::string::FromUtf8Error> for Error {
28    fn from(err: std::string::FromUtf8Error) -> Error {
29        Error::GenericError(err.to_string())
30    }
31}
32
33impl From<std::array::TryFromSliceError> for Error {
34    fn from(err: std::array::TryFromSliceError) -> Error {
35        Error::GenericError(err.to_string())
36    }
37}
38
39impl From<std::net::AddrParseError> for Error {
40    fn from(err: std::net::AddrParseError) -> Error {
41        Error::GenericError(err.to_string())
42    }
43}
44
45impl std::fmt::Display for Error {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            Error::GenericError(msg) => write!(f, "GenericError: {}", msg)?,
49            Error::IoError(msg) => write!(f, "IoError: {}", msg)?,
50            Error::RecordNotFound => write!(f, "RecordNotFound: no record found")?,
51            Error::UnknownDb => write!(
52                f,
53                "Unknown database: Database type should be Proxy or Location"
54            )?,
55            Error::InvalidBinDatabase(y, p) => write!(f, "Invalid Bin Database: {} {}", y, p)?,
56        }
57        Ok(())
58    }
59}