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 #[inline]
76 pub fn is_http0_9(&self) -> bool {
77 matches!(self, Self::HTTP0_9)
78 }
79
80 /// Checks if the current version is HTTP/1.0.
81 ///
82 /// # Returns
83 ///
84 /// `true` if the version is HTTP/1.0, `false` otherwise.
85 #[inline]
86 pub fn is_http1_0(&self) -> bool {
87 matches!(self, Self::HTTP1_0)
88 }
89
90 /// Checks if the current version is HTTP/1.1.
91 ///
92 /// # Returns
93 ///
94 /// `true` if the version is HTTP/1.1, `false` otherwise.
95 #[inline]
96 pub fn is_http1_1(&self) -> bool {
97 matches!(self, Self::HTTP1_1)
98 }
99
100 /// Checks if the current version is HTTP/2.
101 ///
102 /// # Returns
103 ///
104 /// `true` if the version is HTTP/2, `false` otherwise.
105 #[inline]
106 pub fn is_http2(&self) -> bool {
107 matches!(self, Self::HTTP2)
108 }
109
110 /// Checks if the current version is HTTP/3.
111 ///
112 /// # Returns
113 ///
114 /// `true` if the version is HTTP/3, `false` otherwise.
115 #[inline]
116 pub fn is_http3(&self) -> bool {
117 matches!(self, Self::HTTP3)
118 }
119
120 /// Checks if the current version is unknown.
121 ///
122 /// # Returns
123 ///
124 /// `true` if the version is unknown, `false` otherwise.
125 #[inline]
126 pub fn is_unknown(&self) -> bool {
127 matches!(self, Self::Unknown(_))
128 }
129
130 /// Checks if the current version is HTTP/1.1 or higher.
131 ///
132 /// # Returns
133 ///
134 /// `true` if the version is HTTP/1.1, HTTP/2, or HTTP/3, `false` otherwise.
135 #[inline]
136 pub fn is_http1_1_or_higher(&self) -> bool {
137 matches!(self, Self::HTTP1_1 | Self::HTTP2 | Self::HTTP3)
138 }
139
140 /// Checks if the current version is a recognized HTTP version (not unknown).
141 ///
142 /// # Returns
143 ///
144 /// `true` if the version is a known HTTP version, `false` otherwise.
145 #[inline]
146 pub fn is_http(&self) -> bool {
147 !self.is_unknown()
148 }
149}