whois_rust/
who_is_error.rs

1use std::{
2    error::Error,
3    fmt::{self, Display, Formatter},
4    io,
5};
6
7use validators::errors::HostError;
8
9#[cfg(feature = "tokio")]
10use crate::tokio;
11
12#[derive(Debug)]
13pub enum WhoIsError {
14    SerdeJsonError(serde_json::Error),
15    IOError(io::Error),
16    HostError(HostError),
17    #[cfg(feature = "tokio")]
18    Elapsed(tokio::time::error::Elapsed),
19    /// This kind of errors is recommended to be panic!
20    MapError(&'static str),
21}
22
23impl From<serde_json::Error> for WhoIsError {
24    #[inline]
25    fn from(error: serde_json::Error) -> Self {
26        WhoIsError::SerdeJsonError(error)
27    }
28}
29
30impl From<io::Error> for WhoIsError {
31    #[inline]
32    fn from(error: io::Error) -> Self {
33        WhoIsError::IOError(error)
34    }
35}
36
37impl From<HostError> for WhoIsError {
38    #[inline]
39    fn from(error: HostError) -> Self {
40        WhoIsError::HostError(error)
41    }
42}
43
44#[cfg(feature = "tokio")]
45impl From<tokio::time::error::Elapsed> for WhoIsError {
46    #[inline]
47    fn from(error: tokio::time::error::Elapsed) -> Self {
48        WhoIsError::Elapsed(error)
49    }
50}
51
52impl Display for WhoIsError {
53    #[inline]
54    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
55        match self {
56            WhoIsError::SerdeJsonError(error) => Display::fmt(error, f),
57            WhoIsError::IOError(error) => Display::fmt(error, f),
58            WhoIsError::HostError(error) => Display::fmt(error, f),
59            #[cfg(feature = "tokio")]
60            WhoIsError::Elapsed(error) => Display::fmt(error, f),
61            WhoIsError::MapError(text) => f.write_str(text),
62        }
63    }
64}
65
66impl Error for WhoIsError {}