http_type/upgrade_type/
impl.rs

1use crate::*;
2
3/// Implements the `Default` trait for `UpgradeType`.
4///
5/// Provides a default value for `UpgradeType`, which is `Self::Unknown(String::new())`.
6impl Default for UpgradeType {
7    /// Returns the default `UpgradeType`, which is `Self::Unknown(String::new())`.
8    ///
9    /// # Returns
10    ///
11    /// The default `UpgradeType` instance.
12    fn default() -> Self {
13        Self::Unknown(String::new())
14    }
15}
16
17/// Implements the `Display` trait for `UpgradeType`.
18/// This allows `UpgradeType` variants to be formatted into human-readable strings.
19impl fmt::Display for UpgradeType {
20    /// Formats the `UpgradeType` variant into a human-readable string.
21    ///
22    /// # Arguments
23    ///
24    /// - `f`: A mutable reference to a `fmt::Formatter` used for writing the formatted string.
25    ///
26    /// # Returns
27    ///
28    /// A `fmt::Result` indicating whether the formatting was successful.
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            Self::WebSocket => write!(f, "{}", WEBSOCKET),
32            Self::H2c => write!(f, "{}", H2C_LOWERCASE),
33            Self::Tls(version) => write!(f, "{}", version),
34            Self::Unknown(tmp_str) => write!(f, "{}", tmp_str),
35        }
36    }
37}
38
39/// Implements the `FromStr` trait for `UpgradeType`.
40/// This allows parsing string representations into `UpgradeType` variants.
41impl FromStr for UpgradeType {
42    /// The error type for `FromStr` implementation, which is a unit type `()` indicating no specific error information.
43    type Err = ();
44
45    /// Parses a string slice into an `UpgradeType`.
46    ///
47    /// The parsing is case-insensitive. It recognizes "websocket", "h2c", and strings starting with "tls" as specific types.
48    /// Any other string is parsed as `Self::Unknown`.
49    ///
50    /// # Arguments
51    ///
52    /// - `from_str`: The string slice to parse.
53    ///
54    /// # Returns
55    ///
56    /// - `Ok(UpgradeType)`: The parsed `UpgradeType` variant.
57    /// - `Err(())`: If parsing fails (though this implementation always returns `Ok`).
58    fn from_str(from_str: &str) -> Result<Self, Self::Err> {
59        match from_str.to_ascii_lowercase().as_str() {
60            WEBSOCKET => Ok(Self::WebSocket),
61            H2C_LOWERCASE => Ok(Self::H2c),
62            val if val.starts_with(TLS_LOWERCASE) => Ok(Self::Tls(val.to_string())),
63            other => Ok(Self::Unknown(other.to_string())),
64        }
65    }
66}
67
68impl UpgradeType {
69    /// Checks if the current upgrade type is `WebSocket`.
70    ///
71    /// # Returns
72    ///
73    /// `true` if `self` is `Self::WebSocket`, otherwise `false`.
74    pub fn is_ws(&self) -> bool {
75        matches!(self, &Self::WebSocket)
76    }
77
78    /// Checks if the current upgrade type is HTTP/2 cleartext (`h2c`).
79    ///
80    /// # Returns
81    ///
82    /// `true` if `self` is `Self::H2c`, otherwise `false`.
83    pub fn is_h2c(&self) -> bool {
84        matches!(self, &Self::H2c)
85    }
86
87    /// Checks if the current upgrade type is a TLS variant (any version).
88    ///
89    /// # Returns
90    ///
91    /// `true` if `self` matches `Self::Tls(_)`, otherwise `false`.
92    pub fn is_tls(&self) -> bool {
93        matches!(self, Self::Tls(_))
94    }
95
96    /// Checks if the current upgrade type is unknown (neither `WebSocket`, `H2c`, nor `Tls`).
97    ///
98    /// # Returns
99    ///
100    /// `true` if `self` is none of the known upgrade types, otherwise `false`.
101    pub fn is_unknown(&self) -> bool {
102        !self.is_ws() && !self.is_h2c() && !self.is_tls()
103    }
104}