Skip to content

Commit

Permalink
refactor(header): change Header::fmt_header to take a `header::Form…
Browse files Browse the repository at this point in the history
…atter`

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)`.
  • Loading branch information
seanmonstar committed Apr 24, 2017
1 parent f1859df commit 6f02d43
Show file tree
Hide file tree
Showing 24 changed files with 170 additions and 155 deletions.
6 changes: 3 additions & 3 deletions src/header/common/access_control_allow_credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ impl Header for AccessControlAllowCredentials {
Err(::Error::Header)
}

fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("true")
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
f.fmt_line(self)
}
}

impl Display for AccessControlAllowCredentials {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.fmt_header(f)
f.write_str("true")
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/header/common/access_control_allow_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ impl Header for AccessControlAllowOrigin {
}
}

fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AccessControlAllowOrigin::Any => f.write_str("*"),
AccessControlAllowOrigin::Null => f.write_str("null"),
AccessControlAllowOrigin::Value(ref url) => Display::fmt(url, f),
}
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
f.fmt_line(self)
}
}

impl Display for AccessControlAllowOrigin {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
self.fmt_header(f)
match *self {
AccessControlAllowOrigin::Any => f.write_str("*"),
AccessControlAllowOrigin::Null => f.write_str("null"),
AccessControlAllowOrigin::Value(ref url) => Display::fmt(url, f),
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ impl<S: Scheme + Any> Header for Authorization<S> where <S as FromStr>::Err: 'st
}
}

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

Expand Down
5 changes: 3 additions & 2 deletions src/header/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct CacheControl(pub Vec<CacheDirective>);

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/content_disposition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ impl Header for ContentDisposition {
}

#[inline]
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self, f)
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
f.fmt_line(self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/content_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ impl Header for ContentLength {
}

#[inline]
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
f.fmt_line(self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl Header for Cookie {
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl Header for Expect {
}
}

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

Expand Down
12 changes: 6 additions & 6 deletions src/header/common/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ impl Header for Host {
from_one_raw_str(raw)
}

fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.port {
None | Some(80) | Some(443) => f.write_str(&self.hostname[..]),
Some(port) => write!(f, "{}:{}", self.hostname, port)
}
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
f.fmt_line(self)
}
}

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

Expand Down
12 changes: 6 additions & 6 deletions src/header/common/if_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ impl Header for IfRange {
Err(::Error::Header)
}

fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
IfRange::EntityTag(ref x) => Display::fmt(x, f),
IfRange::Date(ref x) => Display::fmt(x, f),
}
fn fmt_header(&self, f: &mut ::header::Formatter) -> ::std::fmt::Result {
f.fmt_line(self)
}
}

impl Display for IfRange {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.fmt_header(f)
match *self {
IfRange::EntityTag(ref x) => Display::fmt(x, f),
IfRange::Date(ref x) => Display::fmt(x, f),
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/header/common/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,14 @@ impl Header for Link {
.unwrap_or(Err(::Error::Header))
}

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

impl fmt::Display for Link {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.fmt_header(f)
fmt_delimited(f, self.values.as_slice(), ", ", ("", ""))
}
}

Expand Down
35 changes: 16 additions & 19 deletions src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,13 @@ macro_rules! header {
fn parse_header(raw: &$crate::header::Raw) -> $crate::Result<Self> {
$crate::header::parsing::from_comma_delimited(raw).map($id)
}
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
f.fmt_line(self)
}
}
impl ::std::fmt::Display for $id {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use $crate::header::Header;
self.fmt_header(f)
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
}
}
};
Expand All @@ -229,14 +228,13 @@ macro_rules! header {
fn parse_header(raw: &$crate::header::Raw) -> $crate::Result<Self> {
$crate::header::parsing::from_comma_delimited(raw).map($id)
}
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
f.fmt_line(self)
}
}
impl ::std::fmt::Display for $id {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use $crate::header::Header;
self.fmt_header(f)
$crate::header::parsing::fmt_comma_delimited(f, &self.0[..])
}
}
};
Expand All @@ -254,8 +252,8 @@ macro_rules! header {
fn parse_header(raw: &$crate::header::Raw) -> $crate::Result<Self> {
$crate::header::parsing::from_one_raw_str(raw).map($id)
}
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
::std::fmt::Display::fmt(&**self, f)
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
f.fmt_line(self)
}
}
impl ::std::fmt::Display for $id {
Expand Down Expand Up @@ -289,8 +287,8 @@ macro_rules! header {
fn parse_header(raw: &$crate::header::Raw) -> $crate::Result<Self> {
$crate::header::parsing::from_one_raw_str::<<$value as ::std::borrow::ToOwned>::Owned>(raw).map($id::new)
}
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
::std::fmt::Display::fmt(&**self, f)
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
f.fmt_line(self)
}
}
impl ::std::fmt::Display for $id {
Expand Down Expand Up @@ -323,20 +321,19 @@ macro_rules! header {
}
$crate::header::parsing::from_comma_delimited(raw).map($id::Items)
}
fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt_header(&self, f: &mut $crate::header::Formatter) -> ::std::fmt::Result {
f.fmt_line(self)
}
}
impl ::std::fmt::Display for $id {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
$id::Any => f.write_str("*"),
$id::Items(ref fields) => $crate::header::parsing::fmt_comma_delimited(
f, &fields[..])
}
}
}
impl ::std::fmt::Display for $id {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
use $crate::header::Header;
self.fmt_header(f)
}
}
};

// optional test module
Expand Down
4 changes: 2 additions & 2 deletions src/header/common/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ impl Header for Origin {
from_one_raw_str(raw)
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/pragma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl Header for Pragma {
})
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/prefer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ impl Header for Prefer {
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/preference_applied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl Header for PreferenceApplied {
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/header/common/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ impl Header for Range {
from_one_raw_str(raw)
}

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

}
Expand Down
4 changes: 2 additions & 2 deletions src/header/common/referrer_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl Header for ReferrerPolicy {
Err(::Error::Header)
}

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

Expand Down
8 changes: 7 additions & 1 deletion src/header/common/retry_after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ impl Header for RetryAfter {
}
}

fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt_header(&self, f: &mut ::header::Formatter) -> ::std::fmt::Result {
f.fmt_line(self)
}
}

impl fmt::Display for RetryAfter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RetryAfter::Delay(ref duration) => {
write!(f, "{}", duration.num_seconds())
Expand Down
11 changes: 1 addition & 10 deletions src/header/common/set_cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,7 @@ impl Header for SetCookie {
}
}

fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.0.len() == 1 {
write!(f, "{}", &self.0[0])
} else {
panic!("SetCookie with multiple cookies cannot be used with fmt_header, must use fmt_multi_header");
}
}


fn fmt_multi_header(&self, f: &mut ::header::MultilineFormatter) -> fmt::Result {
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
for cookie in &self.0 {
try!(f.fmt_line(cookie));
}
Expand Down
4 changes: 2 additions & 2 deletions src/header/common/strict_transport_security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl Header for StrictTransportSecurity {
parsing::from_one_raw_str(raw)
}

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

Expand Down
8 changes: 7 additions & 1 deletion src/header/common/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ impl Header for Warning {
from_one_raw_str(raw)
}

fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
f.fmt_line(self)
}
}

impl fmt::Display for Warning {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.date {
Some(date) => write!(f, "{:03} {} \"{}\" \"{}\"", self.code, self.agent, self.text, date),
None => write!(f, "{:03} {} \"{}\"", self.code, self.agent, self.text)
Expand Down
Loading

0 comments on commit 6f02d43

Please sign in to comment.