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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use std::fmt::{self, Display};
use std::str::FromStr;
use crate::Error;
use crate::{Cookie, Mime};
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct HeaderValue {
inner: String,
}
impl HeaderValue {
pub fn from_ascii(bytes: &[u8]) -> Result<Self, Error> {
crate::ensure!(bytes.is_ascii(), "Bytes should be valid ASCII");
let string = unsafe { String::from_utf8_unchecked(bytes.to_vec()) };
Ok(Self { inner: string })
}
pub unsafe fn from_ascii_unchecked(bytes: Vec<u8>) -> Self {
let string = String::from_utf8_unchecked(bytes);
Self { inner: string }
}
pub fn as_str(&self) -> &str {
&self.inner
}
}
impl From<Mime> for HeaderValue {
fn from(mime: Mime) -> Self {
HeaderValue {
inner: format!("{}", mime),
}
}
}
impl From<Cookie<'_>> for HeaderValue {
fn from(cookie: Cookie<'_>) -> Self {
HeaderValue {
inner: cookie.to_string(),
}
}
}
impl From<&Mime> for HeaderValue {
fn from(mime: &Mime) -> Self {
HeaderValue {
inner: format!("{}", mime),
}
}
}
impl FromStr for HeaderValue {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
crate::ensure!(s.is_ascii(), "String slice should be valid ASCII");
Ok(Self {
inner: String::from(s),
})
}
}
impl<'a> std::convert::TryFrom<&'a str> for HeaderValue {
type Error = Error;
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
Self::from_str(value)
}
}
impl Display for HeaderValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
impl PartialEq<str> for HeaderValue {
fn eq(&self, other: &str) -> bool {
self.inner == other
}
}
impl<'a> PartialEq<&'a str> for HeaderValue {
fn eq(&self, other: &&'a str) -> bool {
&self.inner == other
}
}
impl PartialEq<String> for HeaderValue {
fn eq(&self, other: &String) -> bool {
&self.inner == other
}
}
impl<'a> PartialEq<&String> for HeaderValue {
fn eq(&self, other: &&String) -> bool {
&&self.inner == other
}
}