http_type/protocol/
impl.rs1use crate::*;
2
3impl Default for Protocol {
4 #[inline]
5 fn default() -> Self {
6 Self::HTTP
7 }
8}
9
10impl Protocol {
12 #[inline]
16 pub fn new() -> Self {
17 Self::default()
18 }
19
20 #[inline]
24 pub fn is_http(&self) -> bool {
25 self.to_owned() == Self::HTTP.to_owned()
26 }
27
28 #[inline]
32 pub fn is_https(&self) -> bool {
33 self.to_owned() == Self::HTTPS.to_owned()
34 }
35
36 #[inline]
41 pub fn get_port(&self) -> u16 {
42 match self {
43 Self::HTTP => 80,
44 Self::HTTPS => 443,
45 Self::Unknown(_) => 80,
46 }
47 }
48}
49
50impl Display for Protocol {
51 #[inline]
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 let http: String = HTTP.to_lowercase();
54 let https: String = HTTPS.to_lowercase();
55 let res: &str = match self {
56 Self::HTTP => http.as_str(),
57 Self::HTTPS => https.as_str(),
58 Self::Unknown(protocol) => protocol,
59 };
60 write!(f, "{}", res)
61 }
62}
63
64impl FromStr for Protocol {
65 type Err = &'static str;
66
67 #[inline]
68 fn from_str(data: &str) -> Result<Self, Self::Err> {
69 match data {
70 _data if _data.eq_ignore_ascii_case(HTTP) => Ok(Self::HTTP),
71 _data if _data.eq_ignore_ascii_case(HTTPS) => Ok(Self::HTTPS),
72 _ => Ok(Self::Unknown(data.to_string())),
73 }
74 }
75}