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
use anyhow::{anyhow, Result};
use core::fmt;
use serde::{Deserialize, Serialize};
use std::{convert::TryFrom, str::FromStr};
#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
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 = <Self as FromStr>::Err;
fn try_from(p: &str) -> Result<Protocol> {
p.parse()
}
}
impl FromStr for Protocol {
type Err = anyhow::Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"http" => Ok(Protocol::Http),
"https" => Ok(Protocol::Https),
_ => Err(anyhow!("Invalid protocol, must be http or https")),
}
}
}
impl fmt::Display for Protocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Protocol::Http => "http",
Protocol::Https => "https",
};
s.fmt(f)
}
}