pub struct CookieBuilder(/* private fields */);Expand description
A builder struct for building a Cookie.
Implementations§
Source§impl CookieBuilder
impl CookieBuilder
Sourcepub fn new<N, V>(name: N, value: V) -> CookieBuilder
pub fn new<N, V>(name: N, value: V) -> CookieBuilder
Build a new cookie. This returns a CookieBuilder that can be used to set other attribute
values.
§Example
use cookie_monster::CookieBuilder;
let cookie = CookieBuilder::new("foo", "bar")
.secure()
.http_only()
.build();
assert!(cookie.secure());
assert!(cookie.http_only());Sourcepub fn expires(self, expiration: impl Into<Expires>) -> Self
pub fn expires(self, expiration: impl Into<Expires>) -> Self
Sets the Expires attribute of the cookie.
The argument can be a few different types, based on what features are enabled.
§No features needed
use cookie_monster::{Cookie, Expires};
let cookie = Cookie::build("foo", "bar")
.expires(Expires::remove())
.build();
assert!(cookie.expires_is_set());§Jiff
use jiff::Zoned;
let cookie = Cookie::build("foo", "bar")
.expires(Zoned::now())
.build();
§Chrono
use chrono::Utc;
let cookie = Cookie::build("foo", "bar")
.expires(Utc::now())
.build();
§Time
use time::OffsetDateTime;
let cookie = Cookie::build("foo", "bar")
.expires(OffsetDateTime::now_utc())
.build();
Sourcepub fn max_age_secs(self, max_age_secs: u64) -> Self
pub fn max_age_secs(self, max_age_secs: u64) -> Self
Sets the Max-Age attribute of the cookie.
§Example
use cookie_monster::Cookie;
let cookie = Cookie::build("foo", "bar")
.max_age_secs(100)
.build();
assert_eq!(cookie.max_age_secs(), Some(100));Sourcepub fn max_age(self, max_age: Duration) -> Self
pub fn max_age(self, max_age: Duration) -> Self
Sets the Max-Age attribute of the cookie.
§Example
use cookie_monster::Cookie;
use std::time::Duration;
let cookie = Cookie::build("foo", "bar")
.max_age(Duration::from_secs(100))
.build();
assert_eq!(cookie.max_age(), Some(Duration::from_secs(100)));Sourcepub fn domain<D: Into<Cow<'static, str>>>(self, domain: D) -> Self
pub fn domain<D: Into<Cow<'static, str>>>(self, domain: D) -> Self
Sets the Domain attribute of the cookie.
§Note
If the domain attribute is set to an empty string or the string contains an invalid cookie character, the attribute is ignored.
§Example
use cookie_monster::Cookie;
let cookie = Cookie::build("foo", "bar")
.domain("rust-lang.com")
.build();
assert_eq!(cookie.domain(), Some("rust-lang.com"));Sourcepub fn path<D: Into<Cow<'static, str>>>(self, path: D) -> Self
pub fn path<D: Into<Cow<'static, str>>>(self, path: D) -> Self
Sets the Path attribute of the cookie.
§Note
Not all path value’s are allowed by the standard:
- The path can’t be set to and empty string.
- The path must start with a leading
/. - The path can’t contain invalid cookie characters.
If any of these conditions are not met, serializing this cookie returns an error.
§Example
use cookie_monster::Cookie;
let cookie = Cookie::build("foo", "bar")
.path("/api/login")
.build();
assert_eq!(cookie.path(), Some("/api/login"));Sourcepub fn secure(self) -> Self
pub fn secure(self) -> Self
Sets the Secure attribute of the cookie.
§Example
use cookie_monster::Cookie;
let cookie = Cookie::build("foo", "bar")
.secure()
.build();
assert!(cookie.secure());Sourcepub fn set_secure(self, secure: bool) -> Self
pub fn set_secure(self, secure: bool) -> Self
Sets the Secure attribute.
Sourcepub fn http_only(self) -> Self
pub fn http_only(self) -> Self
Sets the HttpOnly attribute of the cookie.
§Example
use cookie_monster::Cookie;
let cookie = Cookie::build("foo", "bar")
.http_only()
.build();
assert!(cookie.http_only());Sourcepub fn set_http_only(self, http_only: bool) -> Self
pub fn set_http_only(self, http_only: bool) -> Self
Sets the HttpOnly attribute of the cookie.
Sourcepub fn partitioned(self) -> Self
pub fn partitioned(self) -> Self
Sets the Partitioned attribute of the cookie. When the partitioned attribute is enabled, the secure flag is also enabled while serializing.
https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies
§Example
use cookie_monster::Cookie;
let cookie = Cookie::build("foo", "bar")
.partitioned()
.build();
assert!(cookie.partitioned());Sourcepub fn set_partitioned(self, partitioned: bool) -> Self
pub fn set_partitioned(self, partitioned: bool) -> Self
Set the Partitioned flag, enabling the Partitioned attribute also enables the Secure Attribute.
Sourcepub fn same_site<S: Into<Option<SameSite>>>(self, same_site: S) -> Self
pub fn same_site<S: Into<Option<SameSite>>>(self, same_site: S) -> Self
Sets the SameSite attribute value of the cookie.
§Example
use cookie_monster::{Cookie, SameSite};
let cookie = Cookie::build("foo", "bar")
.same_site(SameSite::Strict)
.build();
assert_eq!(cookie.same_site(), Some(SameSite::Strict));Trait Implementations§
Source§impl Borrow<Cookie> for CookieBuilder
impl Borrow<Cookie> for CookieBuilder
Source§impl Clone for CookieBuilder
impl Clone for CookieBuilder
Source§fn clone(&self) -> CookieBuilder
fn clone(&self) -> CookieBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more