Skip to content

Commit

Permalink
fix(header): enable SetCookie.fmt_header when only 1 cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jan 30, 2017
1 parent 60b74eb commit 7611c30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/header/common/set_cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ impl Header for SetCookie {
}

impl HeaderFormat for SetCookie {
fn fmt_header(&self, _f: &mut fmt::Formatter) -> fmt::Result {
panic!("SetCookie cannot be used with fmt_header, must use fmt_multi_header");
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 {
Expand Down
14 changes: 9 additions & 5 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,28 +498,32 @@ impl<'a> FromIterator<HeaderView<'a>> for Headers {
impl<'a> fmt::Display for &'a (HeaderFormat + Send + Sync) {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(**self).fmt_header(f)
let mut multi = MultilineFormatter(Multi::Join(true, f));
self.fmt_multi_header(&mut multi)
}
}

/// A wrapper around any Header with a Display impl that calls fmt_header.
///
/// This can be used like so: `format!("{}", HeaderFormatter(&header))` to
/// get the representation of a Header which will be written to an
/// outgoing `TcpStream`.
/// get the 'value string' representation of this Header.
///
/// Note: This may not necessarily be the value written to stream, such
/// as with the SetCookie header.
pub struct HeaderFormatter<'a, H: HeaderFormat>(pub &'a H);

impl<'a, H: HeaderFormat> fmt::Display for HeaderFormatter<'a, H> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt_header(f)
let mut multi = MultilineFormatter(Multi::Join(true, f));
self.0.fmt_multi_header(&mut multi)
}
}

impl<'a, H: HeaderFormat> fmt::Debug for HeaderFormatter<'a, H> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt_header(f)
fmt::Display::fmt(self, f)
}
}

Expand Down

0 comments on commit 7611c30

Please sign in to comment.