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