cookie_rs::cookie

Struct Cookie

Source
pub struct Cookie<'a> { /* private fields */ }
Expand description

Represents an HTTP cookie, including attributes such as domain, path, and expiration.

Implementations§

Source§

impl<'a> Cookie<'a>

Source

pub fn parse<V: Into<Cow<'a, str>>>(value: V) -> Result<Self, ParseError>

Parses a cookie from a string in a lenient mode.

In lenient mode, unknown attributes are ignored.

§Arguments
  • value: The string representation of the cookie.
§Returns

A Result containing the parsed Cookie or a ParseError.

§Example
use cookie_rs::prelude::*;

let cookie = Cookie::parse("session=abc123; Secure").unwrap();
assert_eq!(cookie.name(), "session");
assert_eq!(cookie.value(), "abc123");
assert_eq!(cookie.secure(), Some(true));
Source

pub fn parse_strict<V: Into<Cow<'a, str>>>(value: V) -> Result<Self, ParseError>

Parses a cookie from a string in a strict mode.

In strict mode, unknown attributes cause an error.

§Arguments
  • value: The string representation of the cookie.
§Returns

A Result containing the parsed Cookie or a ParseError.

§Example
use cookie_rs::prelude::*;

let result = Cookie::parse_strict("session=abc123; UnknownAttr");
assert!(result.is_err());
Source§

impl<'a> Cookie<'a>

Source

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

Creates a new Cookie with the specified name and value.

§Arguments
  • name: The name of the cookie.
  • value: The value of the cookie.
§Example
use cookie_rs::prelude::*;

let cookie = Cookie::new("session", "abc123");
assert_eq!(cookie.name(), "session");
assert_eq!(cookie.value(), "abc123");
Source

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

Creates a CookieBuilder for constructing a Cookie with additional attributes.

§Arguments
  • name: The name of the cookie.
  • value: The value of the cookie.
§Example
use cookie_rs::prelude::*;

let cookie = Cookie::builder("session", "abc123")
    .domain("example.com")
    .secure(true)
    .build();
Source

pub fn set_domain<V: Into<Cow<'a, str>>>(&mut self, domain: V)

Sets the domain for the cookie.

§Arguments
  • domain: The domain attribute of the cookie.
§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_domain("example.com");
assert_eq!(cookie.domain(), Some("example.com"));
Source

pub fn set_expires<V: Into<Cow<'a, str>>>(&mut self, expires: V)

Sets the expiration date for the cookie.

§Arguments
  • expires: The expiration date of the cookie.
§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_expires("Wed, 21 Oct 2025 07:28:00 GMT");
assert_eq!(cookie.expires(), Some("Wed, 21 Oct 2025 07:28:00 GMT"));
Source

pub fn set_http_only(&mut self, http_only: bool)

Sets the HttpOnly attribute for the cookie.

§Arguments
  • http_only: Whether the cookie is HttpOnly.
§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_http_only(true);
assert_eq!(cookie.http_only(), Some(true));
Source

pub fn set_max_age<V: Into<Duration>>(&mut self, max_age: V)

Sets the maximum age for the cookie.

§Arguments
  • max_age: The maximum age of the cookie as a Duration.
§Example
use std::time::Duration;
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_max_age(Duration::from_secs(3600));
assert_eq!(cookie.max_age(), Some(Duration::from_secs(3600)));
Source

pub fn set_partitioned(&mut self, partitioned: bool)

Sets the partitioned attribute for the cookie.

§Arguments
  • partitioned: Whether the cookie is partitioned.
§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_partitioned(true);
assert_eq!(cookie.partitioned(), Some(true));
Source

pub fn set_path<V: Into<Cow<'a, str>>>(&mut self, path: V)

Sets the path attribute for the cookie.

§Arguments
  • path: The path attribute of the cookie.
§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_path("/");
assert_eq!(cookie.path(), Some("/"));
Source

pub fn set_same_site(&mut self, same_site: SameSite)

Sets the SameSite attribute for the cookie.

§Arguments
  • same_site: The SameSite attribute for the cookie.
§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_same_site(SameSite::Lax);
assert_eq!(cookie.same_site(), Some(SameSite::Lax));
Source

pub fn set_secure(&mut self, secure: bool)

Sets the Secure attribute for the cookie.

§Arguments
  • secure: Whether the cookie is secure.
§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_secure(true);
assert_eq!(cookie.secure(), Some(true));
Source

pub fn name(&self) -> &str

Returns the name of the cookie.

§Example
use cookie_rs::prelude::*;

let cookie = Cookie::new("session", "abc123");
assert_eq!(cookie.name(), "session");
Source

pub fn value(&self) -> &str

Returns the value of the cookie.

§Example
use cookie_rs::prelude::*;

let cookie = Cookie::new("session", "abc123");
assert_eq!(cookie.value(), "abc123");
Source

pub fn domain(&self) -> Option<&str>

Returns the domain of the cookie, if set.

§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_domain("example.com");
assert_eq!(cookie.domain(), Some("example.com"));
Source

pub fn expires(&self) -> Option<&str>

Returns the expiration date of the cookie, if set.

§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_expires("Wed, 21 Oct 2025 07:28:00 GMT");
assert_eq!(cookie.expires(), Some("Wed, 21 Oct 2025 07:28:00 GMT"));
Source

pub fn http_only(&self) -> Option<bool>

Returns whether the cookie has the HttpOnly attribute set.

§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_http_only(true);
assert_eq!(cookie.http_only(), Some(true));
Source

pub fn max_age(&self) -> Option<Duration>

Returns the maximum age of the cookie, if set.

§Example
use std::time::Duration;
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_max_age(Duration::from_secs(3600));
assert_eq!(cookie.max_age(), Some(Duration::from_secs(3600)));
Source

pub fn partitioned(&self) -> Option<bool>

Returns whether the cookie is partitioned.

§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_partitioned(true);
assert_eq!(cookie.partitioned(), Some(true));
Source

pub fn path(&self) -> Option<&str>

Returns the path of the cookie, if set.

§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_path("/");
assert_eq!(cookie.path(), Some("/"));
Source

pub fn same_site(&self) -> Option<SameSite>

Returns the SameSite attribute of the cookie, if set.

§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_same_site(SameSite::Lax);
assert_eq!(cookie.same_site(), Some(SameSite::Lax));
Source

pub fn secure(&self) -> Option<bool>

Returns whether the cookie has the Secure attribute set.

§Example
use cookie_rs::prelude::*;

let mut cookie = Cookie::new("session", "abc123");
cookie.set_secure(true);
assert_eq!(cookie.secure(), Some(true));

Trait Implementations§

Source§

impl<'a> Borrow<str> for Cookie<'a>

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl<'a> Clone for Cookie<'a>

Source§

fn clone(&self) -> Cookie<'a>

Returns a copy 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<'a> Debug for Cookie<'a>

Source§

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

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

impl<'a> Default for Cookie<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> Display for Cookie<'a>

Source§

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

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

impl<'a> From<&'a str> for Cookie<'a>

Source§

fn from(value: &'a str) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<(&'a str, &'a str)> for Cookie<'a>

Source§

fn from(value: (&'a str, &'a str)) -> Self

Converts to this type from the input type.
Source§

impl<'a> FromStr for Cookie<'a>

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<'a> Ord for Cookie<'a>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq for Cookie<'a>

Source§

fn eq(&self, other: &Self) -> 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<'a> PartialOrd for Cookie<'a>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Eq for Cookie<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Cookie<'a>

§

impl<'a> RefUnwindSafe for Cookie<'a>

§

impl<'a> Send for Cookie<'a>

§

impl<'a> Sync for Cookie<'a>

§

impl<'a> Unpin for Cookie<'a>

§

impl<'a> UnwindSafe for Cookie<'a>

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, dst: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.