Skip to main content

http_type/upgrade_type/
impl.rs

1use super::*;
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    /// - `&mut Formatter<'_>` - A mutable reference to a `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, data: &mut Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::WebSocket => write!(data, "{WEBSOCKET}"),
34            Self::H2c => write!(data, "{H2C_LOWERCASE}"),
35            Self::Tls(version) => write!(data, "{version}"),
36            Self::Unknown(tmp_str) => write!(data, "{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    /// - `&str` - The string slice to parse.
55    ///
56    /// # Returns
57    ///
58    /// - `Result<Self, Self::Err>` - The parsed `UpgradeType` variant; this
59    ///   implementation always returns `Ok`.
60    #[inline(always)]
61    fn from_str(from_str: &str) -> Result<Self, Self::Err> {
62        if from_str.eq_ignore_ascii_case(WEBSOCKET) {
63            return Ok(Self::WebSocket);
64        }
65        if from_str.eq_ignore_ascii_case(H2C_LOWERCASE) {
66            return Ok(Self::H2c);
67        }
68        let lower: String = from_str.to_ascii_lowercase();
69        if lower.starts_with(TLS_LOWERCASE) {
70            Ok(Self::Tls(lower))
71        } else {
72            Ok(Self::Unknown(lower))
73        }
74    }
75}
76
77/// Inherent implementation of [`UpgradeType`].
78impl UpgradeType {
79    /// Checks if the current upgrade type is `WebSocket`.
80    ///
81    /// # Returns
82    ///
83    /// `bool` - `true` if `self` is `Self::WebSocket`, otherwise `false`.
84    #[inline(always)]
85    pub fn is_ws(&self) -> bool {
86        matches!(self, &Self::WebSocket)
87    }
88
89    /// Checks if the current upgrade type is HTTP/2 cleartext (`h2c`).
90    ///
91    /// # Returns
92    ///
93    /// `bool` - `true` if `self` is `Self::H2c`, otherwise `false`.
94    #[inline(always)]
95    pub fn is_h2c(&self) -> bool {
96        matches!(self, &Self::H2c)
97    }
98
99    /// Checks if the current upgrade type is a TLS variant (any version).
100    ///
101    /// # Returns
102    ///
103    /// `bool` - `true` if `self` matches `Self::Tls(_)`, otherwise `false`.
104    #[inline(always)]
105    pub fn is_tls(&self) -> bool {
106        matches!(self, Self::Tls(_))
107    }
108
109    /// Checks if the current upgrade type is unknown (neither `WebSocket`, `H2c`, nor `Tls`).
110    ///
111    /// # Returns
112    ///
113    /// `bool` - `true` if `self` is none of the known upgrade types, otherwise `false`.
114    #[inline(always)]
115    pub fn is_unknown(&self) -> bool {
116        !self.is_ws() && !self.is_h2c() && !self.is_tls()
117    }
118}