http_type/upgrade_type/
impl.rs

1use crate::*;
2
3impl Default for UpgradeType {
4    fn default() -> Self {
5        Self::Unknown(String::new())
6    }
7}
8
9impl fmt::Display for UpgradeType {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        match self {
12            Self::WebSocket => write!(f, "{}", WEBSOCKET),
13            Self::H2c => write!(f, "{}", H2C_LOWERCASE),
14            Self::Tls(version) => write!(f, "{}", version),
15            Self::Unknown(tmp_str) => write!(f, "{}", tmp_str),
16        }
17    }
18}
19
20impl FromStr for UpgradeType {
21    type Err = ();
22
23    fn from_str(from_str: &str) -> Result<Self, Self::Err> {
24        match from_str.to_ascii_lowercase().as_str() {
25            val if val == WEBSOCKET => Ok(Self::WebSocket),
26            H2C_LOWERCASE => Ok(Self::H2c),
27            val if val.starts_with(TLS_LOWERCASE) => Ok(Self::Tls(val.to_string())),
28            other => Ok(Self::Unknown(other.to_string())),
29        }
30    }
31}
32
33impl UpgradeType {
34    /// Checks if the current upgrade type is `WebSocket`.
35    ///
36    /// - `&self` - The current instance of the enum.
37    ///
38    /// - Returns `true` if `self` is `Self::WebSocket`, otherwise `false`.
39    pub fn is_ws(&self) -> bool {
40        matches!(self, &Self::WebSocket)
41    }
42
43    /// Checks if the current upgrade type is HTTP/2 cleartext (`h2c`).
44    ///
45    /// - `&self` - The current instance of the enum.
46    ///
47    /// - Returns `true` if `self` is `Self::H2c`, otherwise `false`.
48    pub fn is_h2c(&self) -> bool {
49        matches!(self, &Self::H2c)
50    }
51
52    /// Checks if the current upgrade type is a TLS variant (any version).
53    ///
54    /// - `&self` - The current instance of the enum.
55    ///
56    /// - Returns `true` if `self` matches `Self::Tls(_)`, otherwise `false`.
57    pub fn is_tls(&self) -> bool {
58        matches!(self, Self::Tls(_))
59    }
60
61    /// Checks if the current upgrade type is unknown (neither `WebSocket`, `H2c`, nor `Tls`).
62    ///
63    /// - `&self` - The current instance of the enum.
64    ///
65    /// - Returns `true` if `self` is none of the known upgrade types, otherwise `false`.
66    pub fn is_unknown(&self) -> bool {
67        !self.is_ws() && !self.is_h2c() && !self.is_tls()
68    }
69}