Struct cookie::CookieBuilder

source ·
pub struct CookieBuilder<'c> { /* private fields */ }
Expand description

Structure that follows the builder pattern for building Cookie structs.

To construct a cookie:

  1. Call Cookie::build to start building.
  2. Use any of the builder methods to set fields in the cookie.
  3. Call CookieBuilder::finish() to retrieve the built cookie.

Example

use cookie::Cookie;
use cookie::time::Duration;

let cookie: Cookie = Cookie::build("name", "value")
    .domain("www.rust-lang.org")
    .path("/")
    .secure(true)
    .http_only(true)
    .max_age(Duration::days(1))
    .finish();

Implementations§

Creates a new CookieBuilder instance from the given name and value.

This method is typically called indirectly via Cookie::build().

Example
use cookie::Cookie;

let c = Cookie::build("foo", "bar").finish();
assert_eq!(c.name_value(), ("foo", "bar"));

Sets the expires field in the cookie being built.

Example
use cookie::{Cookie, Expiration};
use cookie::time::OffsetDateTime;

let c = Cookie::build("foo", "bar")
    .expires(OffsetDateTime::now_utc())
    .finish();

assert!(c.expires().is_some());

let c = Cookie::build("foo", "bar")
    .expires(None)
    .finish();

assert_eq!(c.expires(), Some(Expiration::Session));

Sets the max_age field in the cookie being built.

Example
use cookie::Cookie;
use cookie::time::Duration;

let c = Cookie::build("foo", "bar")
    .max_age(Duration::minutes(30))
    .finish();

assert_eq!(c.max_age(), Some(Duration::seconds(30 * 60)));

Sets the domain field in the cookie being built.

Example
use cookie::Cookie;

let c = Cookie::build("foo", "bar")
    .domain("www.rust-lang.org")
    .finish();

assert_eq!(c.domain(), Some("www.rust-lang.org"));

Sets the path field in the cookie being built.

Example
use cookie::Cookie;

let c = Cookie::build("foo", "bar")
    .path("/")
    .finish();

assert_eq!(c.path(), Some("/"));

Sets the secure field in the cookie being built.

Example
use cookie::Cookie;

let c = Cookie::build("foo", "bar")
    .secure(true)
    .finish();

assert_eq!(c.secure(), Some(true));

Sets the http_only field in the cookie being built.

Example
use cookie::Cookie;

let c = Cookie::build("foo", "bar")
    .http_only(true)
    .finish();

assert_eq!(c.http_only(), Some(true));

Sets the same_site field in the cookie being built.

Example
use cookie::{Cookie, SameSite};

let c = Cookie::build("foo", "bar")
    .same_site(SameSite::Strict)
    .finish();

assert_eq!(c.same_site(), Some(SameSite::Strict));

Makes the cookie being built ‘permanent’ by extending its expiration and max age 20 years into the future.

Example
use cookie::Cookie;
use cookie::time::Duration;

let c = Cookie::build("foo", "bar")
    .permanent()
    .finish();

assert_eq!(c.max_age(), Some(Duration::days(365 * 20)));

Finishes building and returns the built Cookie.

Example
use cookie::Cookie;

let c = Cookie::build("foo", "bar")
    .domain("crates.io")
    .path("/")
    .finish();

assert_eq!(c.name_value(), ("foo", "bar"));
assert_eq!(c.domain(), Some("crates.io"));
assert_eq!(c.path(), Some("/"));

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.