http_type/http_version/
impl.rs1use crate::*;
2
3impl Default for HttpVersion {
4 fn default() -> Self {
5 Self::Unknown(String::new())
6 }
7}
8
9impl fmt::Display for HttpVersion {
10 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11 let version_str: &str = match self {
12 Self::HTTP0_9 => HTTP_VERSION_0_9,
13 Self::HTTP1_0 => HTTP_VERSION_1_0,
14 Self::HTTP1_1 => HTTP_VERSION_1_1,
15 Self::HTTP2 => HTTP_VERSION_2,
16 Self::HTTP3 => HTTP_VERSION_3,
17 Self::Unknown(version) => version,
18 };
19 write!(f, "{}", version_str)
20 }
21}
22
23impl FromStr for HttpVersion {
24 type Err = String;
25
26 #[allow(warnings)]
27 fn from_str(version_str: &str) -> Result<Self, Self::Err> {
28 match version_str {
29 HTTP_VERSION_0_9 => Ok(Self::HTTP0_9),
30 HTTP_VERSION_1_0 => Ok(Self::HTTP1_0),
31 HTTP_VERSION_1_1 => Ok(Self::HTTP1_1),
32 HTTP_VERSION_2 => Ok(Self::HTTP2),
33 HTTP_VERSION_3 => Ok(Self::HTTP3),
34 _ => Ok(Self::Unknown(version_str.to_string())),
35 }
36 }
37}
38
39impl HttpVersion {
40 pub fn is_http0_9(&self) -> bool {
42 matches!(self, Self::HTTP0_9)
43 }
44
45 pub fn is_http1_0(&self) -> bool {
47 matches!(self, Self::HTTP1_0)
48 }
49
50 pub fn is_http1_1(&self) -> bool {
52 matches!(self, Self::HTTP1_1)
53 }
54
55 pub fn is_http2(&self) -> bool {
57 matches!(self, Self::HTTP2)
58 }
59
60 pub fn is_http3(&self) -> bool {
62 matches!(self, Self::HTTP3)
63 }
64
65 pub fn is_unknown(&self) -> bool {
67 matches!(self, Self::Unknown(_))
68 }
69
70 pub fn is_http1_1_or_higher(&self) -> bool {
72 match self {
73 Self::HTTP1_1 | Self::HTTP2 | Self::HTTP3 => true,
74 _ => false,
75 }
76 }
77
78 pub fn is_http(&self) -> bool {
80 !self.is_unknown()
81 }
82}