Skip to content

Commit

Permalink
feat(header): implement fmt::Display for several headers
Browse files Browse the repository at this point in the history
Specifically, `CacheControl`, `Expect`, `Origin`, `Pragma`, `Prefer`,
`PreferenceApplied`, `ReferrerPolicy`, `StrictTransportSecurity`.
  • Loading branch information
seanmonstar committed Jan 31, 2017
1 parent 0dcd5f5 commit d507577
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/header/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ impl Header for CacheControl {

impl HeaderFormat for CacheControl {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for CacheControl {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_comma_delimited(f, &self[..])
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/header/common/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ impl Header for Expect {

impl HeaderFormat for Expect {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for Expect {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("100-continue")
}
}
6 changes: 6 additions & 0 deletions src/header/common/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ impl FromStr for Origin {

impl HeaderFormat for Origin {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for Origin {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}://{}", self.scheme, self.host)
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/header/common/pragma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl Header for Pragma {

impl HeaderFormat for Pragma {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for Pragma {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
Pragma::NoCache => "no-cache",
Pragma::Ext(ref string) => &string[..],
Expand Down
6 changes: 6 additions & 0 deletions src/header/common/prefer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ impl Header for Prefer {

impl HeaderFormat for Prefer {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for Prefer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_comma_delimited(f, &self[..])
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/header/common/preference_applied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ impl Header for PreferenceApplied {

impl HeaderFormat for PreferenceApplied {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for PreferenceApplied {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//TODO: format this without allocating a Vec and cloning contents
let preferences: Vec<_> = self.0.iter().map(|pref| match pref {
// The spec ignores parameters in `Preferences-Applied`
&Preference::Extension(ref name, ref value, _) => Preference::Extension(
Expand Down
6 changes: 6 additions & 0 deletions src/header/common/referrer_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ impl Header for ReferrerPolicy {

impl HeaderFormat for ReferrerPolicy {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for ReferrerPolicy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::ReferrerPolicy::*;
f.write_str(match *self {
NoReferrer => "no-referrer",
Expand Down
6 changes: 6 additions & 0 deletions src/header/common/strict_transport_security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ impl Header for StrictTransportSecurity {

impl HeaderFormat for StrictTransportSecurity {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for StrictTransportSecurity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.include_subdomains {
write!(f, "max-age={}; includeSubdomains", self.max_age)
} else {
Expand Down

0 comments on commit d507577

Please sign in to comment.