1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::{
    error::Error as StdError,
    fmt::{Display, Formatter, Result as FmtResult},
    result::Result as StdResult,
};
use url::{ParseError as UrlError, Url};

/// Result type
pub type Result<T> = StdResult<T, Error>;

/// Error type
#[derive(Debug, Clone)]
pub enum Error {
    /// The backend with same name already registered
    AlreadyRegistered(String),

    /// Invalid URL
    InvalidUrl(UrlError),

    /// The URL cannot be handled by registered backends
    UnsupportedUrl(Url),

    /// Name resolving error
    FailedResolve(String),

    /// Unable to connect
    FailedConnect(String),
}

impl StdError for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        pub use Error::*;
        match self {
            AlreadyRegistered(name) => {
                "Backend already registered: ".fmt(f)?;
                name.fmt(f)
            }
            InvalidUrl(error) => {
                "Unable to parse URL: ".fmt(f)?;
                error.fmt(f)
            }
            UnsupportedUrl(url) => {
                "Unable to handle specified URL: ".fmt(f)?;
                url.fmt(f)
            }
            FailedResolve(error) => {
                "Unable to resolve name: ".fmt(f)?;
                error.fmt(f)
            }
            FailedConnect(error) => {
                "Unable to connect: ".fmt(f)?;
                error.fmt(f)
            }
        }
    }
}

impl From<Url> for Error {
    fn from(url: Url) -> Self {
        Self::UnsupportedUrl(url)
    }
}

impl From<UrlError> for Error {
    fn from(error: UrlError) -> Self {
        Self::InvalidUrl(error)
    }
}

impl From<String> for Error {
    fn from(error: String) -> Self {
        Self::FailedConnect(error)
    }
}