From d50757707b1c628f398fb0583aa3dd02111ae658 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 31 Jan 2017 13:25:33 -0800 Subject: [PATCH] feat(header): implement fmt::Display for several headers Specifically, `CacheControl`, `Expect`, `Origin`, `Pragma`, `Prefer`, `PreferenceApplied`, `ReferrerPolicy`, `StrictTransportSecurity`. --- src/header/common/cache_control.rs | 6 ++++++ src/header/common/expect.rs | 6 ++++++ src/header/common/origin.rs | 6 ++++++ src/header/common/pragma.rs | 6 ++++++ src/header/common/prefer.rs | 6 ++++++ src/header/common/preference_applied.rs | 7 +++++++ src/header/common/referrer_policy.rs | 6 ++++++ src/header/common/strict_transport_security.rs | 6 ++++++ 8 files changed, 49 insertions(+) diff --git a/src/header/common/cache_control.rs b/src/header/common/cache_control.rs index adbcaf18b0..c9a19e17b9 100644 --- a/src/header/common/cache_control.rs +++ b/src/header/common/cache_control.rs @@ -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[..]) } } diff --git a/src/header/common/expect.rs b/src/header/common/expect.rs index f380e5271a..2aa9ad5fb7 100644 --- a/src/header/common/expect.rs +++ b/src/header/common/expect.rs @@ -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") } } diff --git a/src/header/common/origin.rs b/src/header/common/origin.rs index 4f2d1ba17d..3fbbe05cfa 100644 --- a/src/header/common/origin.rs +++ b/src/header/common/origin.rs @@ -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) } } diff --git a/src/header/common/pragma.rs b/src/header/common/pragma.rs index 39dee7e8c5..5acf4b926e 100644 --- a/src/header/common/pragma.rs +++ b/src/header/common/pragma.rs @@ -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[..], diff --git a/src/header/common/prefer.rs b/src/header/common/prefer.rs index 71490da1be..a72effa09b 100644 --- a/src/header/common/prefer.rs +++ b/src/header/common/prefer.rs @@ -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[..]) } } diff --git a/src/header/common/preference_applied.rs b/src/header/common/preference_applied.rs index fd1bc36fd0..a6b789fa3d 100644 --- a/src/header/common/preference_applied.rs +++ b/src/header/common/preference_applied.rs @@ -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( diff --git a/src/header/common/referrer_policy.rs b/src/header/common/referrer_policy.rs index 08fd85c9b3..1091a219b0 100644 --- a/src/header/common/referrer_policy.rs +++ b/src/header/common/referrer_policy.rs @@ -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", diff --git a/src/header/common/strict_transport_security.rs b/src/header/common/strict_transport_security.rs index 5497830765..e80503a3ce 100644 --- a/src/header/common/strict_transport_security.rs +++ b/src/header/common/strict_transport_security.rs @@ -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 {