1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
macro_rules! enum_version {
(enum $name:ident {
$($variant:ident = $major:expr, $minor:expr),*
}) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum $name {
$($variant),*
}
impl $name {
pub fn major(&self) -> usize {
self.full().0
}
pub fn minor(&self) -> usize {
self.full().1
}
pub fn full(&self) -> (usize, usize) {
match self {
$(Self::$variant => ($major, $minor)),*
}
}
}
use std::cmp::Ordering::{ self, Equal };
impl std::cmp::PartialOrd for $name {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let this = self.full();
let other = other.full();
Some(match this.0.cmp(&other.0) {
Equal => this.1.cmp(&other.1),
cmp => cmp
})
}
}
impl std::convert::TryFrom<(usize, usize)> for $name {
type Error = ();
fn try_from(value: (usize, usize)) -> Result<Self, Self::Error> {
match value {
$(($major, $minor) => Ok(Self::$variant)),*,
_ => Err(())
}
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let full = self.full();
write!(f, "HTTP/{}.{}", full.0, full.1)
}
}
}
}
enum_version! {
enum Version {
One = 1, 0,
OnePointOne = 1, 1,
Two = 2, 0,
Three = 3, 0
}
}
impl Default for Version {
fn default() -> Self {
Self::OnePointOne
}
}