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
85
86
87
88
89
90
91
92
93
94
95
96
use std::cmp::Ordering;
use std::str::FromStr;
use http::header::{HeaderName, InvalidHeaderName};
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
#[non_exhaustive]
pub enum PseudoHeader {
RequestTarget,
}
impl PseudoHeader {
pub fn as_str(&self) -> &str {
match self {
PseudoHeader::RequestTarget => "(request-target)",
}
}
}
impl FromStr for PseudoHeader {
type Err = ();
fn from_str(s: &str) -> Result<PseudoHeader, Self::Err> {
match s {
"(request-target)" => Ok(PseudoHeader::RequestTarget),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Header {
Pseudo(PseudoHeader),
Normal(HeaderName),
}
impl Header {
pub fn as_str(&self) -> &str {
match self {
Header::Pseudo(h) => h.as_str(),
Header::Normal(h) => h.as_str(),
}
}
}
impl FromStr for Header {
type Err = InvalidHeaderName;
fn from_str(s: &str) -> Result<Header, Self::Err> {
PseudoHeader::from_str(s)
.map(Into::into)
.or_else(|_| HeaderName::from_str(s).map(Into::into))
}
}
impl Ord for Header {
fn cmp(&self, other: &Header) -> Ordering {
self.as_str().cmp(other.as_str())
}
}
impl PartialOrd for Header {
fn partial_cmp(&self, other: &Header) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl From<HeaderName> for Header {
fn from(other: HeaderName) -> Self {
Header::Normal(other)
}
}
impl From<PseudoHeader> for Header {
fn from(other: PseudoHeader) -> Self {
Header::Pseudo(other)
}
}