Skip to content

Commit 09771fc

Browse files
committed
checkpoint 1
1 parent 4ed7c93 commit 09771fc

File tree

17 files changed

+142
-427
lines changed

17 files changed

+142
-427
lines changed

src/conditional/vary.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ impl Vary {
7474
Ok(Some(Self { entries, wildcard }))
7575
}
7676

77-
/// Sets the `If-Match` header.
78-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
79-
headers.as_mut().insert(VARY, self.header_value());
80-
}
81-
82-
/// Get the `HeaderName`.
83-
pub fn name(&self) -> HeaderName {
84-
VARY
85-
}
86-
8777
/// Returns `true` if a wildcard directive was set.
8878
pub fn wildcard(&self) -> bool {
8979
self.wildcard
@@ -94,31 +84,6 @@ impl Vary {
9484
self.wildcard = wildcard
9585
}
9686

97-
/// Get the `HeaderValue`.
98-
pub fn value(&self) -> HeaderValue {
99-
let mut output = String::new();
100-
for (n, name) in self.entries.iter().enumerate() {
101-
let directive: HeaderValue = name
102-
.as_str()
103-
.parse()
104-
.expect("Could not convert a HeaderName into a HeaderValue");
105-
match n {
106-
0 => write!(output, "{}", directive).unwrap(),
107-
_ => write!(output, ", {}", directive).unwrap(),
108-
};
109-
}
110-
111-
if self.wildcard {
112-
match output.len() {
113-
0 => write!(output, "*").unwrap(),
114-
_ => write!(output, ", *").unwrap(),
115-
};
116-
}
117-
118-
// SAFETY: the internal string is validated to be ASCII.
119-
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
120-
}
121-
12287
/// Push a directive into the list of entries.
12388
pub fn push(&mut self, directive: impl Into<HeaderName>) -> crate::Result<()> {
12489
self.entries.push(directive.into());
@@ -144,8 +109,29 @@ impl Header for Vary {
144109
fn header_name(&self) -> HeaderName {
145110
VARY
146111
}
112+
147113
fn header_value(&self) -> HeaderValue {
148-
self.header_value()
114+
let mut output = String::new();
115+
for (n, name) in self.entries.iter().enumerate() {
116+
let directive: HeaderValue = name
117+
.as_str()
118+
.parse()
119+
.expect("Could not convert a HeaderName into a HeaderValue");
120+
match n {
121+
0 => write!(output, "{}", directive).unwrap(),
122+
_ => write!(output, ", {}", directive).unwrap(),
123+
};
124+
}
125+
126+
if self.wildcard {
127+
match output.len() {
128+
0 => write!(output, "*").unwrap(),
129+
_ => write!(output, ", *").unwrap(),
130+
};
131+
}
132+
133+
// SAFETY: the internal string is validated to be ASCII.
134+
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
149135
}
150136
}
151137

src/content/accept.rs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,26 @@ impl Accept {
146146
Err(err)
147147
}
148148

149-
/// Sets the `Accept-Encoding` header.
150-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
151-
headers.as_mut().insert(ACCEPT, self.header_value());
149+
/// An iterator visiting all entries.
150+
pub fn iter(&self) -> Iter<'_> {
151+
Iter {
152+
inner: self.entries.iter(),
153+
}
152154
}
153155

154-
/// Get the `HeaderName`.
155-
pub fn name(&self) -> HeaderName {
156-
ACCEPT
156+
/// An iterator visiting all entries.
157+
pub fn iter_mut(&mut self) -> IterMut<'_> {
158+
IterMut {
159+
inner: self.entries.iter_mut(),
160+
}
157161
}
162+
}
158163

159-
/// Get the `HeaderValue`.
160-
pub fn value(&self) -> HeaderValue {
164+
impl Header for Accept {
165+
fn header_name(&self) -> HeaderName {
166+
ACCEPT
167+
}
168+
fn header_value(&self) -> HeaderValue {
161169
let mut output = String::new();
162170
for (n, directive) in self.entries.iter().enumerate() {
163171
let directive: HeaderValue = directive.clone().into();
@@ -177,29 +185,6 @@ impl Accept {
177185
// SAFETY: the internal string is validated to be ASCII.
178186
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
179187
}
180-
181-
/// An iterator visiting all entries.
182-
pub fn iter(&self) -> Iter<'_> {
183-
Iter {
184-
inner: self.entries.iter(),
185-
}
186-
}
187-
188-
/// An iterator visiting all entries.
189-
pub fn iter_mut(&mut self) -> IterMut<'_> {
190-
IterMut {
191-
inner: self.entries.iter_mut(),
192-
}
193-
}
194-
}
195-
196-
impl Header for Accept {
197-
fn header_name(&self) -> HeaderName {
198-
ACCEPT
199-
}
200-
fn header_value(&self) -> HeaderValue {
201-
self.header_value()
202-
}
203188
}
204189

205190
impl IntoIterator for Accept {

src/content/accept_encoding.rs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,27 @@ impl AcceptEncoding {
138138
Err(err)
139139
}
140140

141-
/// Sets the `Accept-Encoding` header.
142-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
143-
headers
144-
.as_mut()
145-
.insert(ACCEPT_ENCODING, self.header_value());
141+
/// An iterator visiting all entries.
142+
pub fn iter(&self) -> Iter<'_> {
143+
Iter {
144+
inner: self.entries.iter(),
145+
}
146146
}
147147

148-
/// Get the `HeaderName`.
149-
pub fn name(&self) -> HeaderName {
148+
/// An iterator visiting all entries.
149+
pub fn iter_mut(&mut self) -> IterMut<'_> {
150+
IterMut {
151+
inner: self.entries.iter_mut(),
152+
}
153+
}
154+
}
155+
156+
impl Header for AcceptEncoding {
157+
fn header_name(&self) -> HeaderName {
150158
ACCEPT_ENCODING
151159
}
152160

153-
/// Get the `HeaderValue`.
154-
pub fn value(&self) -> HeaderValue {
161+
fn header_value(&self) -> HeaderValue {
155162
let mut output = String::new();
156163
for (n, directive) in self.entries.iter().enumerate() {
157164
let directive: HeaderValue = directive.clone().into();
@@ -171,29 +178,6 @@ impl AcceptEncoding {
171178
// SAFETY: the internal string is validated to be ASCII.
172179
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
173180
}
174-
175-
/// An iterator visiting all entries.
176-
pub fn iter(&self) -> Iter<'_> {
177-
Iter {
178-
inner: self.entries.iter(),
179-
}
180-
}
181-
182-
/// An iterator visiting all entries.
183-
pub fn iter_mut(&mut self) -> IterMut<'_> {
184-
IterMut {
185-
inner: self.entries.iter_mut(),
186-
}
187-
}
188-
}
189-
190-
impl Header for AcceptEncoding {
191-
fn header_name(&self) -> HeaderName {
192-
ACCEPT_ENCODING
193-
}
194-
fn header_value(&self) -> HeaderValue {
195-
self.header_value()
196-
}
197181
}
198182

199183
impl IntoIterator for AcceptEncoding {

src/content/content_encoding.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,6 @@ impl ContentEncoding {
6262
Ok(Some(Self { inner }))
6363
}
6464

65-
/// Sets the `Content-Encoding` header.
66-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
67-
headers
68-
.as_mut()
69-
.insert(CONTENT_ENCODING, self.header_value());
70-
}
71-
72-
/// Get the `HeaderName`.
73-
pub fn name(&self) -> HeaderName {
74-
CONTENT_ENCODING
75-
}
76-
77-
/// Get the `HeaderValue`.
78-
pub fn value(&self) -> HeaderValue {
79-
self.inner.into()
80-
}
81-
8265
/// Access the encoding kind.
8366
pub fn encoding(&self) -> Encoding {
8467
self.inner
@@ -90,7 +73,7 @@ impl Header for ContentEncoding {
9073
CONTENT_ENCODING
9174
}
9275
fn header_value(&self) -> HeaderValue {
93-
self.header_value()
76+
self.inner.into()
9477
}
9578
}
9679

src/content/content_length.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,6 @@ impl ContentLength {
5151
Ok(Some(Self { length }))
5252
}
5353

54-
/// Sets the header.
55-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
56-
headers.as_mut().insert(self.name(), self.header_value());
57-
}
58-
59-
/// Get the `HeaderName`.
60-
pub fn name(&self) -> HeaderName {
61-
CONTENT_LENGTH
62-
}
63-
64-
/// Get the `HeaderValue`.
65-
pub fn value(&self) -> HeaderValue {
66-
let output = format!("{}", self.length);
67-
68-
// SAFETY: the internal string is validated to be ASCII.
69-
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
70-
}
71-
7254
/// Get the content length.
7355
pub fn len(&self) -> u64 {
7456
self.length
@@ -85,7 +67,10 @@ impl Header for ContentLength {
8567
CONTENT_LENGTH
8668
}
8769
fn header_value(&self) -> HeaderValue {
88-
self.header_value()
70+
let output = format!("{}", self.length);
71+
72+
// SAFETY: the internal string is validated to be ASCII.
73+
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
8974
}
9075
}
9176

src/content/content_location.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,6 @@ impl ContentLocation {
6464
Ok(Some(Self { url }))
6565
}
6666

67-
/// Sets the header.
68-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
69-
headers.as_mut().insert(self.name(), self.header_value());
70-
}
71-
72-
/// Get the `HeaderName`.
73-
pub fn name(&self) -> HeaderName {
74-
CONTENT_LOCATION
75-
}
76-
77-
/// Get the `HeaderValue`.
78-
pub fn value(&self) -> HeaderValue {
79-
let output = self.url.to_string();
80-
81-
// SAFETY: the internal string is validated to be ASCII.
82-
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
83-
}
84-
8567
/// Get the url.
8668
pub fn location(&self) -> &Url {
8769
&self.url
@@ -104,7 +86,10 @@ impl Header for ContentLocation {
10486
CONTENT_LOCATION
10587
}
10688
fn header_value(&self) -> HeaderValue {
107-
self.header_value()
89+
let output = self.url.to_string();
90+
91+
// SAFETY: the internal string is validated to be ASCII.
92+
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
10893
}
10994
}
11095

src/content/content_type.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,16 @@ impl ContentType {
7373
})?;
7474
Ok(Some(Self { media_type }))
7575
}
76-
77-
/// Sets the header.
78-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
79-
headers.as_mut().insert(self.name(), self.header_value());
80-
}
81-
82-
/// Get the `HeaderName`.
83-
pub fn name(&self) -> HeaderName {
84-
CONTENT_TYPE
85-
}
86-
87-
/// Get the `HeaderValue`.
88-
pub fn value(&self) -> HeaderValue {
89-
let output = format!("{}", self.media_type);
90-
// SAFETY: the internal string is validated to be ASCII.
91-
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
92-
}
9376
}
9477

9578
impl Header for ContentType {
9679
fn header_name(&self) -> HeaderName {
9780
CONTENT_TYPE
9881
}
9982
fn header_value(&self) -> HeaderValue {
100-
self.header_value()
83+
let output = format!("{}", self.media_type);
84+
// SAFETY: the internal string is validated to be ASCII.
85+
unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
10186
}
10287
}
10388

src/other/date.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,14 @@ impl Date {
7171
let at = date.into();
7272
Ok(Some(Self { at }))
7373
}
74+
}
7475

75-
/// Sets the header.
76-
pub fn apply(&self, mut headers: impl AsMut<Headers>) {
77-
headers.as_mut().insert(self.name(), self.header_value());
78-
}
79-
80-
/// Get the `HeaderName`.
81-
pub fn name(&self) -> HeaderName {
76+
impl Header for Date {
77+
fn header_name(&self) -> HeaderName {
8278
DATE
8379
}
8480

85-
/// Get the `HeaderValue`.
86-
pub fn value(&self) -> HeaderValue {
81+
fn header_value(&self) -> HeaderValue {
8782
let date: HttpDate = self.at.into();
8883
let output = format!("{}", date);
8984

@@ -92,15 +87,6 @@ impl Date {
9287
}
9388
}
9489

95-
impl Header for Date {
96-
fn header_name(&self) -> HeaderName {
97-
DATE
98-
}
99-
fn header_value(&self) -> HeaderValue {
100-
self.header_value()
101-
}
102-
}
103-
10490
impl From<Date> for SystemTime {
10591
fn from(date: Date) -> Self {
10692
date.at

0 commit comments

Comments
 (0)