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 #[allow(warnings)]
58 fn from_str(version_str: &str) -> Result<Self, Self::Err> {
59 match version_str {
60 HTTP_VERSION_0_9 => Ok(Self::HTTP0_9),
61 HTTP_VERSION_1_0 => Ok(Self::HTTP1_0),
62 HTTP_VERSION_1_1 => Ok(Self::HTTP1_1),
63 HTTP_VERSION_2 => Ok(Self::HTTP2),
64 HTTP_VERSION_3 => Ok(Self::HTTP3),
65 _ => Ok(Self::Unknown(version_str.to_string())),
66 }
67 }
68}
69
70impl HttpVersion {
71 /// Checks if the current version is HTTP/0.9.
72 ///
73 /// # Returns
74 ///
75 /// `true` if the version is HTTP/0.9, `false` otherwise.
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 pub fn is_http1_0(&self) -> bool {
86 matches!(self, Self::HTTP1_0)
87 }
88
89 /// Checks if the current version is HTTP/1.1.
90 ///
91 /// # Returns
92 ///
93 /// `true` if the version is HTTP/1.1, `false` otherwise.
94 pub fn is_http1_1(&self) -> bool {
95 matches!(self, Self::HTTP1_1)
96 }
97
98 /// Checks if the current version is HTTP/2.
99 ///
100 /// # Returns
101 ///
102 /// `true` if the version is HTTP/2, `false` otherwise.
103 pub fn is_http2(&self) -> bool {
104 matches!(self, Self::HTTP2)
105 }
106
107 /// Checks if the current version is HTTP/3.
108 ///
109 /// # Returns
110 ///
111 /// `true` if the version is HTTP/3, `false` otherwise.
112 pub fn is_http3(&self) -> bool {
113 matches!(self, Self::HTTP3)
114 }
115
116 /// Checks if the current version is unknown.
117 ///
118 /// # Returns
119 ///
120 /// `true` if the version is unknown, `false` otherwise.
121 pub fn is_unknown(&self) -> bool {
122 matches!(self, Self::Unknown(_))
123 }
124
125 /// Checks if the current version is HTTP/1.1 or higher.
126 ///
127 /// # Returns
128 ///
129 /// `true` if the version is HTTP/1.1, HTTP/2, or HTTP/3, `false` otherwise.
130 pub fn is_http1_1_or_higher(&self) -> bool {
131 match self {
132 Self::HTTP1_1 | Self::HTTP2 | Self::HTTP3 => true,
133 _ => false,
134 }
135 }
136
137 /// Checks if the current version is a recognized HTTP version (not unknown).
138 ///
139 /// # Returns
140 ///
141 /// `true` if the version is a known HTTP version, `false` otherwise.
142 pub fn is_http(&self) -> bool {
143 !self.is_unknown()
144 }
145}