Skip to main content

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/// The error type of this crate.
13#[derive(Debug)]
14#[non_exhaustive]
15pub enum WhoIsError {
16    /// The server list is not valid JSON.
17    SerdeJsonError(serde_json::Error),
18    /// Reading the server list, or talking to a WHOIS server, failed.
19    IOError(io::Error),
20    /// The target of a lookup is not a valid domain or IP.
21    HostError(HostError),
22    /// An asynchronous operation did not finish before its timeout.
23    #[cfg(feature = "tokio")]
24    Elapsed(tokio::time::error::Elapsed),
25    /// This kind of errors is recommended to be panic!
26    MapError(&'static str),
27}
28
29impl From<serde_json::Error> for WhoIsError {
30    #[inline]
31    fn from(error: serde_json::Error) -> Self {
32        WhoIsError::SerdeJsonError(error)
33    }
34}
35
36impl From<io::Error> for WhoIsError {
37    #[inline]
38    fn from(error: io::Error) -> Self {
39        WhoIsError::IOError(error)
40    }
41}
42
43impl From<HostError> for WhoIsError {
44    #[inline]
45    fn from(error: HostError) -> Self {
46        WhoIsError::HostError(error)
47    }
48}
49
50#[cfg(feature = "tokio")]
51impl From<tokio::time::error::Elapsed> for WhoIsError {
52    #[inline]
53    fn from(error: tokio::time::error::Elapsed) -> Self {
54        WhoIsError::Elapsed(error)
55    }
56}
57
58impl Display for WhoIsError {
59    #[inline]
60    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
61        match self {
62            WhoIsError::SerdeJsonError(error) => Display::fmt(error, f),
63            WhoIsError::IOError(error) => Display::fmt(error, f),
64            WhoIsError::HostError(error) => Display::fmt(error, f),
65            #[cfg(feature = "tokio")]
66            WhoIsError::Elapsed(error) => Display::fmt(error, f),
67            WhoIsError::MapError(text) => f.write_str(text),
68        }
69    }
70}
71
72impl Error for WhoIsError {}