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
pub use std::convert::TryFrom;

#[derive(Clone, Copy)]
pub enum Protocol {
    Http,
    Https,
}

impl Protocol {
    pub fn is_http(self) -> bool {
        matches!(self, Protocol::Http)
    }

    pub fn is_https(self) -> bool {
        matches!(self, Protocol::Https)
    }
}

impl TryFrom<&str> for Protocol {
    type Error = failure::Error;

    fn try_from(p: &str) -> Result<Protocol, failure::Error> {
        match p {
            "http" => Ok(Protocol::Http),
            "https" => Ok(Protocol::Https),
            _ => failure::bail!("Invalid protocol, must be http or https"),
        }
    }
}