diff --git a/rupring/src/response.rs b/rupring/src/response.rs index 8c9e4f7..b6dbbac 100644 --- a/rupring/src/response.rs +++ b/rupring/src/response.rs @@ -70,20 +70,27 @@ use crate::{header, meme, HeaderName, Request}; use http_body_util::Full; use hyper::body::Bytes; +/// HTTP cookie #[derive(Debug, Clone, Default)] pub struct Cookie { pub name: String, pub value: String, - pub(crate) expires: Option, - pub(crate) max_age: Option, - pub(crate) domain: Option, - pub(crate) path: Option, - pub(crate) secure: Option, - pub(crate) http_only: Option, - pub(crate) same_site: Option, + pub expires: Option, + pub max_age: Option, + pub domain: Option, + pub path: Option, + pub secure: Option, + pub http_only: Option, + pub same_site: Option, } impl Cookie { + /// Create a new cookie. + /// ``` + /// let cookie = rupring::response::Cookie::new("foo", "bar"); + /// assert_eq!(cookie.name, "foo"); + /// assert_eq!(cookie.value, "bar"); + /// ``` pub fn new(name: impl ToString, value: impl ToString) -> Self { Self { name: name.to_string(), @@ -92,36 +99,71 @@ impl Cookie { } } + /// Set the expiration date of the cookie. + /// ``` + /// let cookie = rupring::response::Cookie::new("foo", "bar").expires("Wed, 21 Oct 2015 07:28:00 GMT"); + /// assert_eq!(cookie.expires.unwrap(), "Wed, 21 Oct 2015 07:28:00 GMT"); + /// ``` pub fn expires(mut self, expires: impl ToString) -> Self { self.expires = Some(expires.to_string()); return self; } + /// Set the maximum age of the cookie. + /// ``` + /// let cookie = rupring::response::Cookie::new("foo", "bar").max_age("3600"); + /// assert_eq!(cookie.max_age.unwrap(), "3600"); + /// ``` pub fn max_age(mut self, max_age: impl ToString) -> Self { self.max_age = Some(max_age.to_string()); return self; } + /// Set the domain of the cookie. + /// ``` + /// let cookie = rupring::response::Cookie::new("foo", "bar").domain("example.com"); + /// assert_eq!(cookie.domain.unwrap(), "example.com"); + /// ``` pub fn domain(mut self, domain: impl ToString) -> Self { self.domain = Some(domain.to_string()); return self; } + /// Set the path of the cookie. + /// ``` + /// let cookie = rupring::response::Cookie::new("foo", "bar").path("/path"); + /// assert_eq!(cookie.path.unwrap(), "/path"); + /// ``` pub fn path(mut self, path: impl ToString) -> Self { self.path = Some(path.to_string()); return self; } + /// Set the secure flag of the cookie. + /// ``` + /// let cookie = rupring::response::Cookie::new("foo", "bar").secure(true); + /// assert_eq!(cookie.secure.unwrap(), true); + /// ``` pub fn secure(mut self, secure: bool) -> Self { self.secure = Some(secure); return self; } + /// Set the http only flag of the cookie. + /// ``` + /// let cookie = rupring::response::Cookie::new("foo", "bar").http_only(true); + /// assert_eq!(cookie.http_only.unwrap(), true); + /// ``` pub fn http_only(mut self, http_only: bool) -> Self { self.http_only = Some(http_only); return self; } + /// Set the same site attribute of the cookie. + /// ``` + /// let cookie = rupring::response::Cookie::new("foo", "bar").same_site("Strict"); + /// assert_eq!(cookie.same_site.unwrap(), "Strict"); + /// ``` pub fn same_site(mut self, same_site: impl ToString) -> Self { self.same_site = Some(same_site.to_string()); return self;