eggserve_core/primitives/
version.rs1use std::fmt;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum HttpVersionError {
11 Unsupported,
13}
14
15impl fmt::Display for HttpVersionError {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 match self {
18 Self::Unsupported => write!(f, "unsupported HTTP version"),
19 }
20 }
21}
22
23impl std::error::Error for HttpVersionError {}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
40pub enum HttpVersion {
41 Http10,
43 Http11,
45}
46
47impl HttpVersion {
48 pub fn parse(version_str: &str) -> Result<Self, HttpVersionError> {
56 match version_str {
57 "HTTP/1.0" => Ok(Self::Http10),
58 "HTTP/1.1" => Ok(Self::Http11),
59 _ => Err(HttpVersionError::Unsupported),
60 }
61 }
62
63 pub fn as_str(&self) -> &'static str {
65 match self {
66 Self::Http10 => "HTTP/1.0",
67 Self::Http11 => "HTTP/1.1",
68 }
69 }
70
71 pub fn major(&self) -> u8 {
73 match self {
74 Self::Http10 => 1,
75 Self::Http11 => 1,
76 }
77 }
78
79 pub fn minor(&self) -> u8 {
81 match self {
82 Self::Http10 => 0,
83 Self::Http11 => 1,
84 }
85 }
86}
87
88impl fmt::Display for HttpVersion {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 f.write_str(self.as_str())
91 }
92}
93
94impl AsRef<str> for HttpVersion {
95 fn as_ref(&self) -> &str {
96 self.as_str()
97 }
98}
99
100impl From<&hyper::http::Version> for HttpVersion {
101 fn from(v: &hyper::http::Version) -> Self {
102 match *v {
103 hyper::http::Version::HTTP_10 => Self::Http10,
104 hyper::http::Version::HTTP_11 => Self::Http11,
105 _ => Self::Http11, }
107 }
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn parse_http_1_0() {
116 assert_eq!(HttpVersion::parse("HTTP/1.0").unwrap(), HttpVersion::Http10);
117 }
118
119 #[test]
120 fn parse_http_1_1() {
121 assert_eq!(HttpVersion::parse("HTTP/1.1").unwrap(), HttpVersion::Http11);
122 }
123
124 #[test]
125 fn parse_unsupported() {
126 assert_eq!(
127 HttpVersion::parse("HTTP/2.0").unwrap_err(),
128 HttpVersionError::Unsupported
129 );
130 assert_eq!(
131 HttpVersion::parse("HTTP/0.9").unwrap_err(),
132 HttpVersionError::Unsupported
133 );
134 assert_eq!(
135 HttpVersion::parse("").unwrap_err(),
136 HttpVersionError::Unsupported
137 );
138 }
139
140 #[test]
141 fn as_str() {
142 assert_eq!(HttpVersion::Http10.as_str(), "HTTP/1.0");
143 assert_eq!(HttpVersion::Http11.as_str(), "HTTP/1.1");
144 }
145
146 #[test]
147 fn major_minor() {
148 assert_eq!(HttpVersion::Http10.major(), 1);
149 assert_eq!(HttpVersion::Http10.minor(), 0);
150 assert_eq!(HttpVersion::Http11.major(), 1);
151 assert_eq!(HttpVersion::Http11.minor(), 1);
152 }
153
154 #[test]
155 fn display() {
156 assert_eq!(format!("{}", HttpVersion::Http10), "HTTP/1.0");
157 assert_eq!(format!("{}", HttpVersion::Http11), "HTTP/1.1");
158 }
159
160 #[test]
161 fn as_ref_str() {
162 let s: &str = HttpVersion::Http11.as_ref();
163 assert_eq!(s, "HTTP/1.1");
164 }
165
166 #[test]
167 fn error_display() {
168 assert!(!HttpVersionError::Unsupported.to_string().is_empty());
169 }
170
171 #[test]
172 fn error_is_error() {
173 let err: &dyn std::error::Error = &HttpVersionError::Unsupported;
174 assert!(!err.to_string().is_empty());
175 }
176}