http_type/http_version/
impl.rs

1use crate::*;
2
3/// Implements the `Display` trait for `HttpVersion`, allowing it to be formatted as a string.
4impl fmt::Display for HttpVersion {
5    /// Formats the `HttpVersion` variant into its string representation.
6    ///
7    /// # Arguments
8    ///
9    /// - `f` - The formatter to write the string into.
10    ///
11    /// # Returns
12    ///
13    /// A `fmt::Result` indicating success or failure of the formatting operation.
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        let version_str: &str = match self {
16            Self::HTTP0_9 => HTTP_VERSION_0_9,
17            Self::HTTP1_0 => HTTP_VERSION_1_0,
18            Self::HTTP1_1 => HTTP_VERSION_1_1,
19            Self::HTTP2 => HTTP_VERSION_2,
20            Self::HTTP3 => HTTP_VERSION_3,
21            Self::Unknown(version) => version,
22        };
23        write!(f, "{version_str}")
24    }
25}
26
27/// Implements the `FromStr` trait for `HttpVersion`, allowing conversion from a string slice.
28impl FromStr for HttpVersion {
29    /// The error type returned when conversion fails.
30    type Err = String;
31
32    /// Converts a string slice into an `HttpVersion` variant.
33    ///
34    /// This method attempts to parse the input string into a known `HttpVersion` variant.
35    /// If the string does not match any known version, it returns an `Unknown` variant
36    /// containing the original string.
37    ///
38    /// # Arguments
39    ///
40    /// - `version_str` - The string slice to convert.
41    ///
42    /// # Returns
43    ///
44    /// A `Result` containing the `HttpVersion` variant if successful, or `Self::Err` on failure.
45    fn from_str(version_str: &str) -> Result<Self, Self::Err> {
46        match version_str {
47            HTTP_VERSION_0_9 => Ok(Self::HTTP0_9),
48            HTTP_VERSION_1_0 => Ok(Self::HTTP1_0),
49            HTTP_VERSION_1_1 => Ok(Self::HTTP1_1),
50            HTTP_VERSION_2 => Ok(Self::HTTP2),
51            HTTP_VERSION_3 => Ok(Self::HTTP3),
52            _ => Ok(Self::Unknown(version_str.to_string())),
53        }
54    }
55}
56
57impl HttpVersion {
58    /// Checks if the current version is HTTP/0.9.
59    ///
60    /// # Returns
61    ///
62    /// `true` if the version is HTTP/0.9, `false` otherwise.
63    #[inline(always)]
64    pub fn is_http0_9(&self) -> bool {
65        matches!(self, Self::HTTP0_9)
66    }
67
68    /// Checks if the current version is HTTP/1.0.
69    ///
70    /// # Returns
71    ///
72    /// `true` if the version is HTTP/1.0, `false` otherwise.
73    #[inline(always)]
74    pub fn is_http1_0(&self) -> bool {
75        matches!(self, Self::HTTP1_0)
76    }
77
78    /// Checks if the current version is HTTP/1.1.
79    ///
80    /// # Returns
81    ///
82    /// `true` if the version is HTTP/1.1, `false` otherwise.
83    #[inline(always)]
84    pub fn is_http1_1(&self) -> bool {
85        matches!(self, Self::HTTP1_1)
86    }
87
88    /// Checks if the current version is HTTP/2.
89    ///
90    /// # Returns
91    ///
92    /// `true` if the version is HTTP/2, `false` otherwise.
93    #[inline(always)]
94    pub fn is_http2(&self) -> bool {
95        matches!(self, Self::HTTP2)
96    }
97
98    /// Checks if the current version is HTTP/3.
99    ///
100    /// # Returns
101    ///
102    /// `true` if the version is HTTP/3, `false` otherwise.
103    #[inline(always)]
104    pub fn is_http3(&self) -> bool {
105        matches!(self, Self::HTTP3)
106    }
107
108    /// Checks if the current version is unknown.
109    ///
110    /// # Returns
111    ///
112    /// `true` if the version is unknown, `false` otherwise.
113    #[inline(always)]
114    pub fn is_unknown(&self) -> bool {
115        matches!(self, Self::Unknown(_))
116    }
117
118    /// Checks if the current version is HTTP/1.1 or higher.
119    ///
120    /// # Returns
121    ///
122    /// `true` if the version is HTTP/1.1, HTTP/2, or HTTP/3, `false` otherwise.
123    #[inline(always)]
124    pub fn is_http1_1_or_higher(&self) -> bool {
125        matches!(self, Self::HTTP1_1 | Self::HTTP2 | Self::HTTP3)
126    }
127
128    /// Checks if the current version is a recognized HTTP version (not unknown).
129    ///
130    /// # Returns
131    ///
132    /// `true` if the version is a known HTTP version, `false` otherwise.
133    #[inline(always)]
134    pub fn is_http(&self) -> bool {
135        !self.is_unknown()
136    }
137}