http_type/http_version/
impl.rs

1use crate::*;
2
3impl Default for HttpVersion {
4    #[inline]
5    fn default() -> Self {
6        Self::Unknown(String::new())
7    }
8}
9
10impl fmt::Display for HttpVersion {
11    #[inline]
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        let version_str = match self {
14            Self::HTTP0_9 => HTTP_VERSION_0_9,
15            Self::HTTP1_0 => HTTP_VERSION_1_0,
16            Self::HTTP1_1 => HTTP_VERSION_1_1,
17            Self::HTTP2 => HTTP_VERSION_2,
18            Self::HTTP3 => HTTP_VERSION_3,
19            Self::Unknown(version) => version,
20        };
21        write!(f, "{}", version_str)
22    }
23}
24
25impl FromStr for HttpVersion {
26    type Err = String;
27
28    #[inline]
29    fn from_str(version_str: &str) -> Result<Self, Self::Err> {
30        match version_str {
31            version_0_9 if version_0_9 == HTTP_VERSION_0_9 => Ok(Self::HTTP0_9),
32            version_1_0 if version_1_0 == HTTP_VERSION_1_0 => Ok(Self::HTTP1_0),
33            version_1_1 if version_1_1 == HTTP_VERSION_1_1 => Ok(Self::HTTP1_1),
34            version_2 if version_2 == HTTP_VERSION_2 => Ok(Self::HTTP2),
35            version_3 if version_3 == HTTP_VERSION_3 => Ok(Self::HTTP3),
36            _ => Ok(Self::Unknown(version_str.to_string())),
37        }
38    }
39}
40
41impl HttpVersion {
42    /// Checks if the current version is HTTP/0.9.
43    #[inline]
44    pub fn is_http0_9(&self) -> bool {
45        matches!(self, Self::HTTP0_9)
46    }
47
48    /// Checks if the current version is HTTP/1.0.
49    #[inline]
50    pub fn is_http1_0(&self) -> bool {
51        matches!(self, Self::HTTP1_0)
52    }
53
54    /// Checks if the current version is HTTP/1.1.
55    #[inline]
56    pub fn is_http1_1(&self) -> bool {
57        matches!(self, Self::HTTP1_1)
58    }
59
60    /// Checks if the current version is HTTP/2.
61    #[inline]
62    pub fn is_http2(&self) -> bool {
63        matches!(self, Self::HTTP2)
64    }
65
66    /// Checks if the current version is HTTP/3.
67    #[inline]
68    pub fn is_http3(&self) -> bool {
69        matches!(self, Self::HTTP3)
70    }
71
72    /// Checks if the current version is unknown.
73    #[inline]
74    pub fn is_unknown(&self) -> bool {
75        matches!(self, Self::Unknown(_))
76    }
77
78    /// Checks if the current version is HTTP/1.1 or higher.
79    #[inline]
80    pub fn is_http1_1_or_higher(&self) -> bool {
81        match self {
82            Self::HTTP1_1 | Self::HTTP2 | Self::HTTP3 => true,
83            _ => false,
84        }
85    }
86
87    /// Checks if the current version is http.
88    #[inline]
89    pub fn is_http(&self) -> bool {
90        !self.is_unknown()
91    }
92}