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 with_domain<V: Into<Cow<'a, str>>>(self, domain: V) -> Self
pub fn with_domain<V: Into<Cow<'a, str>>>(self, domain: V) -> Self
Sourcepub fn with_expires<V: Into<Cow<'a, str>>>(self, expires: V) -> Self
pub fn with_expires<V: Into<Cow<'a, str>>>(self, expires: V) -> Self
Sourcepub fn with_http_only(self, http_only: bool) -> Self
pub fn with_http_only(self, http_only: bool) -> Self
Sourcepub fn with_max_age<V: Into<Duration>>(self, max_age: V) -> Self
pub fn with_max_age<V: Into<Duration>>(self, max_age: V) -> Self
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 cookie = Cookie::new("session", "abc123").with_max_age(Duration::from_secs(3600));
assert_eq!(cookie.max_age(), Some(Duration::from_secs(3600)));Sourcepub fn with_partitioned(self, partitioned: bool) -> Self
pub fn with_partitioned(self, partitioned: bool) -> Self
Sourcepub fn with_secure(self, secure: bool) -> Self
pub fn with_secure(self, secure: bool) -> Self
Sourcepub fn with_same_site(self, same_site: SameSite) -> Self
pub fn with_same_site(self, same_site: SameSite) -> Self
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("/"));Sourcepub fn same_site(&self) -> Option<SameSite>
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));Sourcepub fn secure(&self) -> Option<bool>
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));Sourcepub fn into_owned(self) -> Cookie<'static>
pub fn into_owned(self) -> Cookie<'static>
Converts the cookie into an owned version with a 'static lifetime.
§Example
use cookie_rs::prelude::*;
let input = String::from("session=abc123; Path=/");
let cookie: Cookie<'static> = Cookie::parse(input).unwrap().into_owned();
assert_eq!(cookie.name(), "session");
assert_eq!(cookie.path(), Some("/"));