Skip to content

Commit 6f02d43

Browse files
committed
refactor(header): change Header::fmt_header to take a header::Formatter
The `header::Formatter` ensures that a formatted header is written to a line, and allows for headers that require multiple lines. The only header to specifically require this is `Set-Cookie`. BREAKING CHANGE: The `fmt_header` method has changed to take a different formatter. In most cases, if your header also implements `fmt::Display`, you can just call `f.fmt_line(self)`.
1 parent f1859df commit 6f02d43

24 files changed

+170
-155
lines changed

src/header/common/access_control_allow_credentials.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ impl Header for AccessControlAllowCredentials {
6363
Err(::Error::Header)
6464
}
6565

66-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
67-
f.write_str("true")
66+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
67+
f.fmt_line(self)
6868
}
6969
}
7070

7171
impl Display for AccessControlAllowCredentials {
7272
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
73-
self.fmt_header(f)
73+
f.write_str("true")
7474
}
7575
}
7676

src/header/common/access_control_allow_origin.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ impl Header for AccessControlAllowOrigin {
7272
}
7373
}
7474

75-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
76-
match *self {
77-
AccessControlAllowOrigin::Any => f.write_str("*"),
78-
AccessControlAllowOrigin::Null => f.write_str("null"),
79-
AccessControlAllowOrigin::Value(ref url) => Display::fmt(url, f),
80-
}
75+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
76+
f.fmt_line(self)
8177
}
8278
}
8379

8480
impl Display for AccessControlAllowOrigin {
8581
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
86-
self.fmt_header(f)
82+
match *self {
83+
AccessControlAllowOrigin::Any => f.write_str("*"),
84+
AccessControlAllowOrigin::Null => f.write_str("null"),
85+
AccessControlAllowOrigin::Value(ref url) => Display::fmt(url, f),
86+
}
8787
}
8888
}
8989

src/header/common/authorization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ impl<S: Scheme + Any> Header for Authorization<S> where <S as FromStr>::Err: 'st
100100
}
101101
}
102102

103-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
104-
fmt::Display::fmt(self, f)
103+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
104+
f.fmt_line(self)
105105
}
106106
}
107107

src/header/common/cache_control.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct CacheControl(pub Vec<CacheDirective>);
4949

5050
__hyper__deref!(CacheControl => Vec<CacheDirective>);
5151

52+
//TODO: this could just be the header! macro
5253
impl Header for CacheControl {
5354
fn header_name() -> &'static str {
5455
static NAME: &'static str = "Cache-Control";
@@ -64,8 +65,8 @@ impl Header for CacheControl {
6465
}
6566
}
6667

67-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
68-
fmt::Display::fmt(self, f)
68+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
69+
f.fmt_line(self)
6970
}
7071
}
7172

src/header/common/content_disposition.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ impl Header for ContentDisposition {
146146
}
147147

148148
#[inline]
149-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
150-
fmt::Display::fmt(&self, f)
149+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
150+
f.fmt_line(self)
151151
}
152152
}
153153

src/header/common/content_length.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl Header for ContentLength {
6060
}
6161

6262
#[inline]
63-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
64-
fmt::Display::fmt(&self.0, f)
63+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
64+
f.fmt_line(self)
6565
}
6666
}
6767

src/header/common/cookie.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ impl Header for Cookie {
5454
}
5555
}
5656

57-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
58-
fmt::Display::fmt(self, f)
57+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
58+
f.fmt_line(self)
5959
}
6060
}
6161

src/header/common/expect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ impl Header for Expect {
5454
}
5555
}
5656

57-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
58-
fmt::Display::fmt(self, f)
57+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
58+
f.fmt_line(self)
5959
}
6060
}
6161

src/header/common/host.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ impl Header for Host {
6868
from_one_raw_str(raw)
6969
}
7070

71-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
72-
match self.port {
73-
None | Some(80) | Some(443) => f.write_str(&self.hostname[..]),
74-
Some(port) => write!(f, "{}:{}", self.hostname, port)
75-
}
71+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
72+
f.fmt_line(self)
7673
}
7774
}
7875

7976
impl fmt::Display for Host {
8077
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81-
self.fmt_header(f)
78+
match self.port {
79+
None | Some(80) | Some(443) => f.write_str(&self.hostname[..]),
80+
Some(port) => write!(f, "{}:{}", self.hostname, port)
81+
}
8282
}
8383
}
8484

src/header/common/if_range.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ impl Header for IfRange {
7070
Err(::Error::Header)
7171
}
7272

73-
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
74-
match *self {
75-
IfRange::EntityTag(ref x) => Display::fmt(x, f),
76-
IfRange::Date(ref x) => Display::fmt(x, f),
77-
}
73+
fn fmt_header(&self, f: &mut ::header::Formatter) -> ::std::fmt::Result {
74+
f.fmt_line(self)
7875
}
7976
}
8077

8178
impl Display for IfRange {
8279
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83-
self.fmt_header(f)
80+
match *self {
81+
IfRange::EntityTag(ref x) => Display::fmt(x, f),
82+
IfRange::Date(ref x) => Display::fmt(x, f),
83+
}
8484
}
8585
}
8686

src/header/common/link.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,14 @@ impl Header for Link {
423423
.unwrap_or(Err(::Error::Header))
424424
}
425425

426-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
427-
fmt_delimited(f, self.values.as_slice(), ", ", ("", ""))
426+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
427+
f.fmt_line(self)
428428
}
429429
}
430430

431431
impl fmt::Display for Link {
432432
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
433-
self.fmt_header(f)
433+
fmt_delimited(f, self.values.as_slice(), ", ", ("", ""))
434434
}
435435
}
436436

src/header/common/mod.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,13 @@ macro_rules! header {
204204
fn parse_header(raw: &$crate::header::Raw) -> $crate::Result<Self> {
205205
$crate::header::parsing::from_comma_delimited(raw).map($id)
206206
}
207-
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
208-
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
207+
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
208+
f.fmt_line(self)
209209
}
210210
}
211211
impl ::std::fmt::Display for $id {
212212
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
213-
use $crate::header::Header;
214-
self.fmt_header(f)
213+
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
215214
}
216215
}
217216
};
@@ -229,14 +228,13 @@ macro_rules! header {
229228
fn parse_header(raw: &$crate::header::Raw) -> $crate::Result<Self> {
230229
$crate::header::parsing::from_comma_delimited(raw).map($id)
231230
}
232-
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
233-
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
231+
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
232+
f.fmt_line(self)
234233
}
235234
}
236235
impl ::std::fmt::Display for $id {
237236
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
238-
use $crate::header::Header;
239-
self.fmt_header(f)
237+
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
240238
}
241239
}
242240
};
@@ -254,8 +252,8 @@ macro_rules! header {
254252
fn parse_header(raw: &$crate::header::Raw) -> $crate::Result<Self> {
255253
$crate::header::parsing::from_one_raw_str(raw).map($id)
256254
}
257-
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
258-
::std::fmt::Display::fmt(&**self, f)
255+
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
256+
f.fmt_line(self)
259257
}
260258
}
261259
impl ::std::fmt::Display for $id {
@@ -289,8 +287,8 @@ macro_rules! header {
289287
fn parse_header(raw: &$crate::header::Raw) -> $crate::Result<Self> {
290288
$crate::header::parsing::from_one_raw_str::<<$value as ::std::borrow::ToOwned>::Owned>(raw).map($id::new)
291289
}
292-
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
293-
::std::fmt::Display::fmt(&**self, f)
290+
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
291+
f.fmt_line(self)
294292
}
295293
}
296294
impl ::std::fmt::Display for $id {
@@ -323,20 +321,19 @@ macro_rules! header {
323321
}
324322
$crate::header::parsing::from_comma_delimited(raw).map($id::Items)
325323
}
326-
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
324+
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
325+
f.fmt_line(self)
326+
}
327+
}
328+
impl ::std::fmt::Display for $id {
329+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
327330
match *self {
328331
$id::Any => f.write_str("*"),
329332
$id::Items(ref fields) => $crate::header::parsing::fmt_comma_delimited(
330333
f, &fields[..])
331334
}
332335
}
333336
}
334-
impl ::std::fmt::Display for $id {
335-
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
336-
use $crate::header::Header;
337-
self.fmt_header(f)
338-
}
339-
}
340337
};
341338

342339
// optional test module

src/header/common/origin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ impl Header for Origin {
104104
from_one_raw_str(raw)
105105
}
106106

107-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
108-
fmt::Display::fmt(self, f)
107+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
108+
f.fmt_line(self)
109109
}
110110
}
111111

src/header/common/pragma.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ impl Header for Pragma {
5454
})
5555
}
5656

57-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
58-
fmt::Display::fmt(self, f)
57+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
58+
f.fmt_line(self)
5959
}
6060
}
6161

src/header/common/prefer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ impl Header for Prefer {
6565
}
6666
}
6767

68-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
69-
fmt::Display::fmt(self, f)
68+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
69+
f.fmt_line(self)
7070
}
7171
}
7272

src/header/common/preference_applied.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ impl Header for PreferenceApplied {
6363
}
6464
}
6565

66-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
67-
fmt::Display::fmt(self, f)
66+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
67+
f.fmt_line(self)
6868
}
6969
}
7070

src/header/common/range.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ impl Header for Range {
190190
from_one_raw_str(raw)
191191
}
192192

193-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
194-
Display::fmt(self, f)
193+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
194+
f.fmt_line(self)
195195
}
196196

197197
}

src/header/common/referrer_policy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ impl Header for ReferrerPolicy {
7979
Err(::Error::Header)
8080
}
8181

82-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
83-
fmt::Display::fmt(self, f)
82+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
83+
f.fmt_line(self)
8484
}
8585
}
8686

src/header/common/retry_after.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ impl Header for RetryAfter {
121121
}
122122
}
123123

124-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
124+
fn fmt_header(&self, f: &mut ::header::Formatter) -> ::std::fmt::Result {
125+
f.fmt_line(self)
126+
}
127+
}
128+
129+
impl fmt::Display for RetryAfter {
130+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125131
match *self {
126132
RetryAfter::Delay(ref duration) => {
127133
write!(f, "{}", duration.num_seconds())

src/header/common/set_cookie.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,7 @@ impl Header for SetCookie {
9191
}
9292
}
9393

94-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
95-
if self.0.len() == 1 {
96-
write!(f, "{}", &self.0[0])
97-
} else {
98-
panic!("SetCookie with multiple cookies cannot be used with fmt_header, must use fmt_multi_header");
99-
}
100-
}
101-
102-
103-
fn fmt_multi_header(&self, f: &mut ::header::MultilineFormatter) -> fmt::Result {
94+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
10495
for cookie in &self.0 {
10596
try!(f.fmt_line(cookie));
10697
}

src/header/common/strict_transport_security.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ impl Header for StrictTransportSecurity {
129129
parsing::from_one_raw_str(raw)
130130
}
131131

132-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
133-
fmt::Display::fmt(self, f)
132+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
133+
f.fmt_line(self)
134134
}
135135
}
136136

src/header/common/warning.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ impl Header for Warning {
9696
from_one_raw_str(raw)
9797
}
9898

99-
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
99+
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
100+
f.fmt_line(self)
101+
}
102+
}
103+
104+
impl fmt::Display for Warning {
105+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100106
match self.date {
101107
Some(date) => write!(f, "{:03} {} \"{}\" \"{}\"", self.code, self.agent, self.text, date),
102108
None => write!(f, "{:03} {} \"{}\"", self.code, self.agent, self.text)

0 commit comments

Comments
 (0)