From edc1c0dd01b24ee32250dff51268ad60fff9293d Mon Sep 17 00:00:00 2001 From: mayah Date: Tue, 30 May 2017 03:16:09 +0900 Subject: [PATCH] fix(header): add `Cookie::iter()` --- src/header/common/cookie.rs | 35 +++++++++++++++++++++++++++++++++++ src/header/common/mod.rs | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/header/common/cookie.rs b/src/header/common/cookie.rs index ee7c67b9cc..9e86bd2809 100644 --- a/src/header/common/cookie.rs +++ b/src/header/common/cookie.rs @@ -85,6 +85,29 @@ impl Cookie { pub fn get(&self, key: &str) -> Option<&str> { self.0.get(key).map(AsRef::as_ref) } + + /// Iterate cookies. + /// + /// Iterate cookie (key, value) in insertion order. + /// + /// ``` + /// use hyper::header::Cookie; + /// let mut cookie = Cookie::new(); + /// cookie.append("foo", "bar"); + /// cookie.append(String::from("dyn"), String::from("amic")); + /// + /// let mut keys = Vec::new(); + /// let mut values = Vec::new(); + /// for (k, v) in cookie.iter() { + /// keys.push(k); + /// values.push(v); + /// } + /// assert_eq!(keys, vec!["foo", "dyn"]); + /// assert_eq!(values, vec!["bar", "amic"]); + /// ``` + pub fn iter(&self) -> CookieIter { + CookieIter(self.0.iter()) + } } impl Header for Cookie { @@ -154,6 +177,18 @@ impl fmt::Display for Cookie { } } +/// Iterator for cookie. +#[derive(Debug)] +pub struct CookieIter<'a>(::std::slice::Iter<'a, (Cow<'static, str>, Cow<'static, str>)>); + +impl<'a> Iterator for CookieIter<'a> { + type Item = (&'a str, &'a str); + + fn next(&mut self) -> Option { + self.0.next().map(|kv| (kv.0.as_ref(), kv.1.as_ref())) + } +} + #[cfg(test)] mod tests { use header::Header; diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index 82230e0109..21e6f6abd3 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -30,7 +30,7 @@ pub use self::content_language::ContentLanguage; pub use self::content_location::ContentLocation; pub use self::content_range::{ContentRange, ContentRangeSpec}; pub use self::content_type::ContentType; -pub use self::cookie::Cookie; +pub use self::cookie::{Cookie, CookieIter}; pub use self::date::Date; pub use self::etag::ETag; pub use self::expect::Expect;