http_type/protocol/
impl.rs

1use crate::*;
2
3impl Default for Protocol {
4    #[inline]
5    fn default() -> Self {
6        Self::HTTP
7    }
8}
9
10/// Provides utility methods for the `Protocol` type.
11impl Protocol {
12    /// Creates a new instance of `Protocol` with the default value of `Self::HTTP`.
13    ///
14    /// This is a shorthand for using the `default` method.
15    #[inline]
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Checks if the current protocol is `HTTP`.
21    ///
22    /// Returns `true` if the protocol is `HTTP`, otherwise returns `false`.    
23    #[inline]
24    pub fn is_http(&self) -> bool {
25        self.to_owned() == Self::HTTP.to_owned()
26    }
27
28    /// Checks if the current protocol is `HTTPS`.
29    ///
30    /// Returns `true` if the protocol is `HTTPS`, otherwise returns `false`.
31    #[inline]
32    pub fn is_https(&self) -> bool {
33        self.to_owned() == Self::HTTPS.to_owned()
34    }
35
36    /// Returns the default port associated with the protocol.
37    ///
38    /// - Returns `80` for `Self::HTTP`.
39    /// - Returns `443` for `Self::HTTPS`.
40    #[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}