http_type/http_version/
impl.rs

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