feat(header): implement fmt::Display for several headers
Specifically, `CacheControl`, `Expect`, `Origin`, `Pragma`, `Prefer`, `PreferenceApplied`, `ReferrerPolicy`, `StrictTransportSecurity`.
This commit is contained in:
		| @@ -101,6 +101,12 @@ impl<S: Scheme + Any> Header for Authorization<S> where <S as FromStr>::Err: 'st | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|  |         fmt::Display::fmt(self, f) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | impl<S: Scheme> fmt::Display for Authorization<S> { | ||||||
|  |     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|         if let Some(scheme) = <S as Scheme>::scheme() { |         if let Some(scheme) = <S as Scheme>::scheme() { | ||||||
|             try!(write!(f, "{} ", scheme)) |             try!(write!(f, "{} ", scheme)) | ||||||
|         }; |         }; | ||||||
| @@ -152,7 +158,6 @@ impl Scheme for Basic { | |||||||
|         if let Some(ref pass) = self.password { |         if let Some(ref pass) = self.password { | ||||||
|             text.push_str(&pass[..]); |             text.push_str(&pass[..]); | ||||||
|         } |         } | ||||||
|          |  | ||||||
|         f.write_str(&encode(text.as_ref())) |         f.write_str(&encode(text.as_ref())) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -193,25 +198,25 @@ impl FromStr for Basic { | |||||||
| #[derive(Clone, PartialEq, Debug)] | #[derive(Clone, PartialEq, Debug)] | ||||||
| ///Token holder for Bearer Authentication, most often seen with oauth | ///Token holder for Bearer Authentication, most often seen with oauth | ||||||
| pub struct Bearer { | pub struct Bearer { | ||||||
| 	///Actual bearer token as a string |     ///Actual bearer token as a string | ||||||
| 	pub token: String |     pub token: String | ||||||
| } | } | ||||||
|  |  | ||||||
| impl Scheme for Bearer { | impl Scheme for Bearer { | ||||||
| 	fn scheme() -> Option<&'static str> { |     fn scheme() -> Option<&'static str> { | ||||||
| 		Some("Bearer") |         Some("Bearer") | ||||||
| 	} |     } | ||||||
|  |  | ||||||
| 	fn fmt_scheme(&self, f: &mut fmt::Formatter) -> fmt::Result { |     fn fmt_scheme(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
| 		write!(f, "{}", self.token) |         write!(f, "{}", self.token) | ||||||
| 	} |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| impl FromStr for Bearer { | impl FromStr for Bearer { | ||||||
| 	type Err = ::Error; |     type Err = ::Error; | ||||||
| 	fn from_str(s: &str) -> ::Result<Bearer> { |     fn from_str(s: &str) -> ::Result<Bearer> { | ||||||
| 		Ok(Bearer { token: s.to_owned()}) |         Ok(Bearer { token: s.to_owned()}) | ||||||
| 	} |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| @@ -265,7 +270,7 @@ mod tests { | |||||||
|         assert_eq!(auth.0.password, Some("".to_owned())); |         assert_eq!(auth.0.password, Some("".to_owned())); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| 	#[test] |     #[test] | ||||||
|     fn test_bearer_auth() { |     fn test_bearer_auth() { | ||||||
|         let mut headers = Headers::new(); |         let mut headers = Headers::new(); | ||||||
|         headers.set(Authorization( |         headers.set(Authorization( | ||||||
|   | |||||||
| @@ -65,6 +65,12 @@ impl Header for CacheControl { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     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[..]) |         fmt_comma_delimited(f, &self[..]) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -55,6 +55,12 @@ impl Header for Cookie { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|  |         fmt::Display::fmt(self, f) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | impl fmt::Display for Cookie { | ||||||
|  |     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|         let cookies = &self.0; |         let cookies = &self.0; | ||||||
|         for (i, cookie) in cookies.iter().enumerate() { |         for (i, cookie) in cookies.iter().enumerate() { | ||||||
|             if i != 0 { |             if i != 0 { | ||||||
| @@ -63,6 +69,7 @@ impl Header for Cookie { | |||||||
|             try!(Display::fmt(&cookie, f)); |             try!(Display::fmt(&cookie, f)); | ||||||
|         } |         } | ||||||
|         Ok(()) |         Ok(()) | ||||||
|  |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -55,6 +55,12 @@ impl Header for Expect { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     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") |         f.write_str("100-continue") | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -85,9 +85,8 @@ macro_rules! bench_header( | |||||||
|             fn bench_format(b: &mut Bencher) { |             fn bench_format(b: &mut Bencher) { | ||||||
|                 let raw = $value.into(); |                 let raw = $value.into(); | ||||||
|                 let val: $ty = Header::parse_header(&raw).unwrap(); |                 let val: $ty = Header::parse_header(&raw).unwrap(); | ||||||
|                 let fmt = ::header::HeaderFormatter(&val); |  | ||||||
|                 b.iter(|| { |                 b.iter(|| { | ||||||
|                     format!("{}", fmt); |                     format!("{}", val); | ||||||
|                 }); |                 }); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -59,7 +59,7 @@ impl Header for Origin { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|         write!(f, "{}://{}", self.scheme, self.host) |         fmt::Display::fmt(self, f) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -83,6 +83,12 @@ impl FromStr for Origin { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | impl fmt::Display for Origin { | ||||||
|  |     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|  |         write!(f, "{}://{}", self.scheme, self.host) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| impl PartialEq for Origin { | impl PartialEq for Origin { | ||||||
|     fn eq(&self, other: &Origin) -> bool { |     fn eq(&self, other: &Origin) -> bool { | ||||||
|         self.scheme == other.scheme && self.host == other.host |         self.scheme == other.scheme && self.host == other.host | ||||||
|   | |||||||
| @@ -55,6 +55,12 @@ impl Header for Pragma { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     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 { |         f.write_str(match *self { | ||||||
|             Pragma::NoCache => "no-cache", |             Pragma::NoCache => "no-cache", | ||||||
|             Pragma::Ext(ref string) => &string[..], |             Pragma::Ext(ref string) => &string[..], | ||||||
|   | |||||||
| @@ -66,6 +66,12 @@ impl Header for Prefer { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     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[..]) |         fmt_comma_delimited(f, &self[..]) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -64,6 +64,13 @@ impl Header for PreferenceApplied { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     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 { |         let preferences: Vec<_> = self.0.iter().map(|pref| match pref { | ||||||
|             // The spec ignores parameters in `Preferences-Applied` |             // The spec ignores parameters in `Preferences-Applied` | ||||||
|             &Preference::Extension(ref name, ref value, _) => Preference::Extension( |             &Preference::Extension(ref name, ref value, _) => Preference::Extension( | ||||||
|   | |||||||
| @@ -80,6 +80,12 @@ impl Header for ReferrerPolicy { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     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::*; |         use self::ReferrerPolicy::*; | ||||||
|         f.write_str(match *self { |         f.write_str(match *self { | ||||||
|             NoReferrer => "no-referrer", |             NoReferrer => "no-referrer", | ||||||
|   | |||||||
| @@ -130,6 +130,12 @@ impl Header for StrictTransportSecurity { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result { |     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 { |         if self.include_subdomains { | ||||||
|             write!(f, "max-age={}; includeSubdomains", self.max_age) |             write!(f, "max-age={}; includeSubdomains", self.max_age) | ||||||
|         } else { |         } else { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user