CookieBuilder

Struct CookieBuilder 

Source
pub struct CookieBuilder(/* private fields */);
Expand description

A builder struct for building a Cookie.

Implementations§

Source§

impl CookieBuilder

Source

pub fn new<N, V>(name: N, value: V) -> CookieBuilder
where N: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>,

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());
Source

pub fn name<N: Into<Cow<'static, str>>>(self, name: N) -> Self

Sets the name of the cookie.

Source

pub fn get_name(&self) -> &str

Returns the name of the cookie.

Source

pub fn value<V: Into<Cow<'static, str>>>(self, value: V) -> Self

Sets the value of the cookie.

Source

pub fn get_value(&self) -> &str

Returns the value of the cookie.

Source

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();
Source

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));
Source

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)));
Source

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"));
Source

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"));
Source

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());
Source

pub fn set_secure(self, secure: bool) -> Self

Sets the Secure attribute.

Source

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());
Source

pub fn set_http_only(self, http_only: bool) -> Self

Sets the HttpOnly attribute of the cookie.

Source

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());
Source

pub fn set_partitioned(self, partitioned: bool) -> Self

Set the Partitioned flag, enabling the Partitioned attribute also enables the Secure Attribute.

Source

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));
Source

pub fn build(self) -> Cookie

Builds and returns the cookie

Trait Implementations§

Source§

impl Borrow<Cookie> for CookieBuilder

Source§

fn borrow(&self) -> &Cookie

Immutably borrows from an owned value. Read more
Source§

impl Clone for CookieBuilder

Source§

fn clone(&self) -> CookieBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CookieBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for CookieBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

fn from(value: CookieBuilder) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for CookieBuilder

Source§

fn eq(&self, other: &CookieBuilder) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for CookieBuilder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.