Skip to main content

http_type/http_version/
impl.rs

1use super::*;
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    /// - `&mut Formatter<'_>` - 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, data: &mut 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!(data, "{version_str}")
24    }
25}
26
27impl HttpVersion {
28    /// Returns the version's text without copying it.
29    ///
30    /// The text of a known version is a constant, so a caller that only reads
31    /// it borrows it rather than allocating a copy for every response.
32    ///
33    /// # Returns
34    ///
35    /// - `&str` - The version's text.
36    pub fn as_str(&self) -> &str {
37        match self {
38            Self::Http0_9 => HTTP_VERSION_0_9,
39            Self::Http1_0 => HTTP_VERSION_1_0,
40            Self::Http1_1 => HTTP_VERSION_1_1,
41            Self::Http2 => HTTP_VERSION_2,
42            Self::Http3 => HTTP_VERSION_3,
43            Self::Unknown(version) => version,
44        }
45    }
46}
47
48/// Implements the `FromStr` trait for `HttpVersion`, allowing conversion from a string slice.
49impl FromStr for HttpVersion {
50    /// The error type returned when conversion fails.
51    type Err = String;
52
53    /// Converts a string slice into an `HttpVersion` variant.
54    ///
55    /// This method attempts to parse the input string into a known `HttpVersion` variant.
56    /// If the string does not match any known version, it returns an `Unknown` variant
57    /// containing the original string.
58    ///
59    /// # Arguments
60    ///
61    /// - `&str` - The string slice to convert.
62    ///
63    /// # Returns
64    ///
65    /// A `Result` containing the `HttpVersion` variant if successful, or `Self::Err` on failure.
66    fn from_str(version_str: &str) -> Result<Self, Self::Err> {
67        match version_str {
68            HTTP_VERSION_0_9 => Ok(Self::Http0_9),
69            HTTP_VERSION_1_0 => Ok(Self::Http1_0),
70            HTTP_VERSION_1_1 => Ok(Self::Http1_1),
71            HTTP_VERSION_2 => Ok(Self::Http2),
72            HTTP_VERSION_3 => Ok(Self::Http3),
73            _ => Ok(Self::Unknown(version_str.to_string())),
74        }
75    }
76}
77
78impl HttpVersion {
79    /// Checks if the current version is HTTP/0.9.
80    ///
81    /// # Returns
82    ///
83    /// `true` if the version is HTTP/0.9, `false` otherwise.
84    #[inline(always)]
85    pub fn is_http0_9(&self) -> bool {
86        matches!(self, Self::Http0_9)
87    }
88
89    /// Checks if the current version is HTTP/1.0.
90    ///
91    /// # Returns
92    ///
93    /// `true` if the version is HTTP/1.0, `false` otherwise.
94    #[inline(always)]
95    pub fn is_http1_0(&self) -> bool {
96        matches!(self, Self::Http1_0)
97    }
98
99    /// Checks if the current version is HTTP/1.1.
100    ///
101    /// # Returns
102    ///
103    /// `true` if the version is HTTP/1.1, `false` otherwise.
104    #[inline(always)]
105    pub fn is_http1_1(&self) -> bool {
106        matches!(self, Self::Http1_1)
107    }
108
109    /// Checks if the current version is HTTP/2.
110    ///
111    /// # Returns
112    ///
113    /// `true` if the version is HTTP/2, `false` otherwise.
114    #[inline(always)]
115    pub fn is_http2(&self) -> bool {
116        matches!(self, Self::Http2)
117    }
118
119    /// Checks if the current version is HTTP/3.
120    ///
121    /// # Returns
122    ///
123    /// `true` if the version is HTTP/3, `false` otherwise.
124    #[inline(always)]
125    pub fn is_http3(&self) -> bool {
126        matches!(self, Self::Http3)
127    }
128
129    /// Checks if the current version is unknown.
130    ///
131    /// # Returns
132    ///
133    /// `true` if the version is unknown, `false` otherwise.
134    #[inline(always)]
135    pub fn is_unknown(&self) -> bool {
136        matches!(self, Self::Unknown(_))
137    }
138
139    /// Checks if the current version is HTTP/1.1 or higher.
140    ///
141    /// # Returns
142    ///
143    /// `true` if the version is HTTP/1.1, HTTP/2, or HTTP/3, `false` otherwise.
144    #[inline(always)]
145    pub fn is_http1_1_or_higher(&self) -> bool {
146        matches!(self, Self::Http1_1 | Self::Http2 | Self::Http3)
147    }
148
149    /// Checks if the current version is a recognized HTTP version (not unknown).
150    ///
151    /// # Returns
152    ///
153    /// `true` if the version is a known HTTP version, `false` otherwise.
154    #[inline(always)]
155    pub fn is_http(&self) -> bool {
156        !self.is_unknown()
157    }
158}