publicsuffix/
errors.rs

1/// Errors returned by this library
2use std::fmt;
3use std::io;
4use url::ParseError;
5
6#[cfg(feature = "remote_list")]
7use std::net::TcpStream;
8
9#[cfg(feature = "remote_list")]
10use native_tls::{Error as TlsError, HandshakeError};
11
12pub type Result<T> = ::std::result::Result<T, Error>;
13
14#[derive(Debug)]
15pub struct Error(
16    /// The kind of the error.
17    pub ErrorKind,
18);
19impl std::error::Error for Error {}
20
21impl Error {
22    pub fn kind(&self) -> &ErrorKind {
23        &self.0
24    }
25}
26
27impl fmt::Display for Error {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "{}", self.0)
30    }
31}
32
33#[derive(Debug)]
34/// The kind of an error.
35pub enum ErrorKind {
36    Io(::std::io::Error),
37    Url(::url::ParseError),
38    #[cfg(feature = "remote_list")]
39    Tls(::native_tls::Error),
40    #[cfg(feature = "remote_list")]
41    Handshake(HandshakeError<TcpStream>),
42    /// A convenient variant for String.
43    Msg(String),
44    UnsupportedScheme,
45    InvalidList,
46    NoHost,
47    NoPort,
48    InvalidHost,
49    InvalidEmail,
50    InvalidRule(String),
51    InvalidDomain(String),
52    Uts46(::idna::Errors),
53    #[doc(hidden)]
54    __Nonexhaustive {},
55}
56
57impl fmt::Display for ErrorKind {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match self {
60            ErrorKind::Io(e) => write!(f, "{}", e),
61            ErrorKind::Url(e) => write!(f, "{}", e),
62            #[cfg(feature = "remote_list")]
63            ErrorKind::Tls(e) => write!(f, "{}", e),
64            #[cfg(feature = "remote_list")]
65            ErrorKind::Handshake(e) => write!(f, "{}", e),
66            ErrorKind::Msg(e) => write!(f, "{}", e),
67            ErrorKind::UnsupportedScheme => write!(f, "UnsupportedScheme"),
68            ErrorKind::InvalidList => write!(f, "InvalidList"),
69            ErrorKind::NoHost => write!(f, "NoHost"),
70            ErrorKind::NoPort => write!(f, "NoPort"),
71            ErrorKind::InvalidHost => write!(f, "InvalidHost"),
72            ErrorKind::InvalidEmail => write!(f, "InvalidEmail"),
73            ErrorKind::InvalidRule(t) => write!(f, "invalid rule: '{}'", t),
74            ErrorKind::InvalidDomain(t) => write!(f, "invalid domain: '{}'", t),
75            ErrorKind::Uts46(e) => write!(f, "UTS #46 processing error: '{:?}'", e),
76            ErrorKind::__Nonexhaustive {} => write!(f, "__Nonexhaustive"),
77        }
78    }
79}
80
81impl From<ErrorKind> for Error {
82    fn from(kind: ErrorKind) -> Self {
83        Error(kind)
84    }
85}
86
87impl From<io::Error> for Error {
88    fn from(e: io::Error) -> Self {
89        Error(ErrorKind::Io(e))
90    }
91}
92
93impl From<ParseError> for Error {
94    fn from(e: url::ParseError) -> Self {
95        Error(ErrorKind::Url(e))
96    }
97}
98
99#[cfg(feature = "remote_list")]
100impl From<TlsError> for Error {
101    fn from(e: TlsError) -> Self {
102        Error(ErrorKind::Tls(e))
103    }
104}
105
106#[cfg(feature = "remote_list")]
107impl From<HandshakeError<TcpStream>> for Error {
108    fn from(e: HandshakeError<TcpStream>) -> Self {
109        Error(ErrorKind::Handshake(e))
110    }
111}