whois_rust/
who_is_error.rs1use 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)]
14#[non_exhaustive]
15pub enum WhoIsError {
16 SerdeJsonError(serde_json::Error),
18 IOError(io::Error),
20 HostError(HostError),
22 #[cfg(feature = "tokio")]
24 Elapsed(tokio::time::error::Elapsed),
25 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 {}