http_type/protocol/
impl.rs1use crate::*;
2
3impl Default for Protocol {
4 fn default() -> Self {
5 Self::HTTP
6 }
7}
8
9impl Protocol {
11 pub fn new() -> Self {
15 Self::default()
16 }
17
18 pub fn is_http(&self) -> bool {
22 self.to_owned() == Self::HTTP.to_owned()
23 }
24
25 pub fn is_https(&self) -> bool {
29 self.to_owned() == Self::HTTPS.to_owned()
30 }
31
32 pub fn get_port(&self) -> u16 {
37 match self {
38 Self::HTTP => 80,
39 Self::HTTPS => 443,
40 Self::Unknown(_) => 80,
41 }
42 }
43}
44
45impl Display for Protocol {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 let res: &str = match self {
48 Self::HTTP => HTTP_LOWERCASE,
49 Self::HTTPS => HTTPS_LOWERCASE,
50 Self::Unknown(protocol) => protocol,
51 };
52 write!(f, "{}", res)
53 }
54}
55
56impl FromStr for Protocol {
57 type Err = &'static str;
58
59 fn from_str(data: &str) -> Result<Self, Self::Err> {
60 match data {
61 _data if _data == HTTP_LOWERCASE => Ok(Self::HTTP),
62 _data if _data == HTTPS_LOWERCASE => Ok(Self::HTTPS),
63 _ => Ok(Self::Unknown(data.to_string())),
64 }
65 }
66}