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>
impl<'a> Cookie<'a>
Sourcepub fn parse<V: Into<Cow<'a, str>>>(value: V) -> Result<Self, ParseError>
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));Sourcepub fn parse_strict<V: Into<Cow<'a, str>>>(value: V) -> Result<Self, ParseError>
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>
impl<'a> Cookie<'a>
Sourcepub fn builder<N, V>(name: N, value: V) -> CookieBuilder<'a>
pub fn builder<N, V>(name: N, value: V) -> CookieBuilder<'a>
Sourcepub fn set_domain<V: Into<Cow<'a, str>>>(&mut self, domain: V)
pub fn set_domain<V: Into<Cow<'a, str>>>(&mut self, domain: V)
Sourcepub fn set_expires<V: Into<Cow<'a, str>>>(&mut self, expires: V)
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"));Sourcepub fn set_http_only(&mut self, http_only: bool)
pub fn set_http_only(&mut self, http_only: bool)
Sourcepub fn set_max_age<V: Into<Duration>>(&mut self, max_age: V)
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 aDuration.
§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)));Sourcepub fn set_partitioned(&mut self, partitioned: bool)
pub fn set_partitioned(&mut self, partitioned: bool)
Sourcepub fn set_same_site(&mut self, same_site: SameSite)
pub fn set_same_site(&mut self, same_site: SameSite)
Sourcepub fn set_secure(&mut self, secure: bool)
pub fn set_secure(&mut self, secure: bool)
Sourcepub fn name(&self) -> &str
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");Sourcepub fn value(&self) -> &str
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");Sourcepub fn domain(&self) -> Option<&str>
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"));Sourcepub fn expires(&self) -> Option<&str>
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"));Sourcepub fn http_only(&self) -> Option<bool>
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));Sourcepub fn max_age(&self) -> Option<Duration>
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)));Sourcepub fn partitioned(&self) -> Option<bool>
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));Sourcepub fn path(&self) -> Option<&str>
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("/"));