1use std::fmt::Display;
14
15use crate::{WebError, WebResult};
16use algorithm::buf::{Bt, BtMut};
17
18#[derive(Debug, Eq, PartialEq, Clone)]
19pub enum Version {
20 None,
21 Http10,
22 Http11,
23 Http2,
24 Http3,
25}
26
27impl Copy for Version {}
28
29impl Version {
30 pub const HTTP10: Version = Version::Http10;
31 pub const SHTTP10: &'static str = "HTTP/1.0";
32 pub const HTTP11: Version = Version::Http11;
33 pub const SHTTP11: &'static str = "HTTP/1.1";
34 pub const HTTP2: Version = Version::Http2;
35 pub const SHTTP2: &'static str = "HTTP/2";
36 pub const HTTP3: Version = Version::Http3;
37 pub const SHTTP3: &'static str = "HTTP/3";
38
39 pub fn as_str(&self) -> &'static str {
40 match self {
41 Version::Http10 => "HTTP/1.0",
42 Version::Http11 => "HTTP/1.1",
43 Version::Http2 => "HTTP/2",
44 Version::Http3 => "HTTP/3",
45 Version::None => "None",
46 }
47 }
48
49 pub fn encode<B: Bt + BtMut>(&mut self, buffer: &mut B) -> WebResult<usize> {
50 match self {
51 Version::None => Err(WebError::Serialize("version")),
52 _ => Ok(buffer.put_slice(&self.as_str().as_bytes())),
53 }
54 }
55
56 pub fn is_http1(&self) -> bool {
57 match self {
58 Version::Http10 => true,
59 Version::Http11 => true,
60 _ => false,
61 }
62 }
63
64 pub fn is_http2(&self) -> bool {
65 match self {
66 Version::Http2 => true,
67 _ => false,
68 }
69 }
70}
71
72impl From<&str> for Version {
73 fn from(value: &str) -> Self {
74 match value {
75 "HTTP/1.0" => Version::HTTP10,
76 "HTTP/1.1" => Version::HTTP11,
77 "HTTP/2" => Version::HTTP2,
78 "HTTP/3" => Version::HTTP3,
79 _ => Version::None
80 }
81 }
82}
83
84impl Display for Version {
85 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86 f.write_str(&self.as_str())
87 }
88}