http_type/protocol/
impl.rs

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