http_type/http_version/
impl.rs

1use 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 = 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    fn from_str(version_str: &str) -> Result<Self, Self::Err> {
27        match version_str {
28            version_0_9 if version_0_9 == HTTP_VERSION_0_9 => Ok(Self::HTTP0_9),
29            version_1_0 if version_1_0 == HTTP_VERSION_1_0 => Ok(Self::HTTP1_0),
30            version_1_1 if version_1_1 == HTTP_VERSION_1_1 => Ok(Self::HTTP1_1),
31            version_2 if version_2 == HTTP_VERSION_2 => Ok(Self::HTTP2),
32            version_3 if version_3 == HTTP_VERSION_3 => Ok(Self::HTTP3),
33            _ => Ok(Self::Unknown(version_str.to_string())),
34        }
35    }
36}
37
38impl HttpVersion {
39    /// Checks if the current version is HTTP/0.9.
40    pub fn is_http0_9(&self) -> bool {
41        matches!(self, Self::HTTP0_9)
42    }
43
44    /// Checks if the current version is HTTP/1.0.
45    pub fn is_http1_0(&self) -> bool {
46        matches!(self, Self::HTTP1_0)
47    }
48
49    /// Checks if the current version is HTTP/1.1.
50    pub fn is_http1_1(&self) -> bool {
51        matches!(self, Self::HTTP1_1)
52    }
53
54    /// Checks if the current version is HTTP/2.
55    pub fn is_http2(&self) -> bool {
56        matches!(self, Self::HTTP2)
57    }
58
59    /// Checks if the current version is HTTP/3.
60    pub fn is_http3(&self) -> bool {
61        matches!(self, Self::HTTP3)
62    }
63
64    /// Checks if the current version is unknown.
65    pub fn is_unknown(&self) -> bool {
66        matches!(self, Self::Unknown(_))
67    }
68
69    /// Checks if the current version is HTTP/1.1 or higher.
70    pub fn is_http1_1_or_higher(&self) -> bool {
71        match self {
72            Self::HTTP1_1 | Self::HTTP2 | Self::HTTP3 => true,
73            _ => false,
74        }
75    }
76
77    /// Checks if the current version is http.
78    pub fn is_http(&self) -> bool {
79        !self.is_unknown()
80    }
81}