[go: up one dir, main page]

url2/
error.rs

1/// Url2 Result Type
2pub type Url2Result<T> = Result<T, Url2Error>;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5/// Represents a Url2 Error
6pub struct Url2Error(Box<Url2ErrorKind>);
7
8impl Url2Error {
9    /// access the Url2ErrorKind for this error
10    pub fn kind(&self) -> &Url2ErrorKind {
11        &self.0
12    }
13
14    /// convert this error into a raw kind
15    pub fn into_kind(self) -> Url2ErrorKind {
16        *self.0
17    }
18}
19
20impl std::error::Error for Url2Error {
21    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
22        match *self.0 {
23            Url2ErrorKind::UrlParseError(ref err) => Some(err),
24        }
25    }
26}
27
28impl std::fmt::Display for Url2Error {
29    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30        match *self.0 {
31            Url2ErrorKind::UrlParseError(ref err) => err.fmt(f),
32        }
33    }
34}
35
36impl std::convert::From<Url2ErrorKind> for Url2Error {
37    fn from(kind: Url2ErrorKind) -> Url2Error {
38        Url2Error(Box::new(kind))
39    }
40}
41
42impl std::convert::From<url::ParseError> for Url2Error {
43    fn from(err: url::ParseError) -> Self {
44        Url2ErrorKind::UrlParseError(err).into()
45    }
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
49#[non_exhaustive]
50/// enum representing the type of Url2Error
51pub enum Url2ErrorKind {
52    /// Url Parsing Error
53    UrlParseError(url::ParseError),
54}