pub struct Cookie<'c> { /* private fields */ }
Expand description
Representation of an HTTP cookie.
§Constructing a Cookie
To construct a cookie with only a name/value, use Cookie::new()
:
use cookie::Cookie;
let cookie = Cookie::new("name", "value");
assert_eq!(cookie.to_string(), "name=value");
§Building a Cookie
To construct more elaborate cookies, use Cookie::build()
and
CookieBuilder
methods. Cookie::build()
accepts any type that
implements T: Into<Cookie>
. See Cookie::build()
for details.
use cookie::Cookie;
let cookie = Cookie::build(("name", "value"))
.domain("www.rust-lang.org")
.path("/")
.secure(true)
.http_only(true);
jar.add(cookie);
jar.remove(Cookie::build("name").path("/"));
Implementations§
Source§impl<'c> Cookie<'c>
impl<'c> Cookie<'c>
Sourcepub fn new<N, V>(name: N, value: V) -> Cookie<'c>
pub fn new<N, V>(name: N, value: V) -> Cookie<'c>
Creates a new Cookie
with the given name and value.
§Example
use cookie::Cookie;
let cookie = Cookie::new("name", "value");
assert_eq!(cookie.name_value(), ("name", "value"));
// This is equivalent to `from` with a `(name, value)` tuple:
let cookie = Cookie::from(("name", "value"));
assert_eq!(cookie.name_value(), ("name", "value"));
Sourcepub fn named<N>(name: N) -> Cookie<'c>
👎Deprecated since 0.18.0: use Cookie::build(name)
or Cookie::from(name)
pub fn named<N>(name: N) -> Cookie<'c>
Cookie::build(name)
or Cookie::from(name)
Creates a new Cookie
with the given name and an empty value.
§Example
use cookie::Cookie;
let cookie = Cookie::named("name");
assert_eq!(cookie.name(), "name");
assert!(cookie.value().is_empty());
// This is equivalent to `from` with `"name`:
let cookie = Cookie::from("name");
assert_eq!(cookie.name(), "name");
assert!(cookie.value().is_empty());
Sourcepub fn build<C>(base: C) -> CookieBuilder<'c>
pub fn build<C>(base: C) -> CookieBuilder<'c>
Creates a new CookieBuilder
starting from a base
cookie.
Any type that implements T: Into<Cookie>
can be used as a base
:
Into<Cookie> Type | Example | Equivalent To |
---|---|---|
(K, V) , K, V: Into<Cow<str>> | ("name", "value") | Cookie::new(name, value) |
&str , String , Cow<str> | "name" | Cookie::new(name, "") |
CookieBuilder | Cookie::build("foo") | CookieBuilder::build() |
§Example
use cookie::Cookie;
// Use `(K, V)` as the base, setting a name and value.
let b1 = Cookie::build(("name", "value")).path("/");
assert_eq!(b1.inner().name_value(), ("name", "value"));
assert_eq!(b1.inner().path(), Some("/"));
// Use `&str` as the base, setting a name and empty value.
let b2 = Cookie::build(("name"));
assert_eq!(b2.inner().name_value(), ("name", ""));
// Use `CookieBuilder` as the base, inheriting all properties.
let b3 = Cookie::build(b1);
assert_eq!(b3.inner().name_value(), ("name", "value"));
assert_eq!(b3.inner().path(), Some("/"));
Sourcepub fn parse<S>(s: S) -> Result<Cookie<'c>, ParseError>
pub fn parse<S>(s: S) -> Result<Cookie<'c>, ParseError>
Parses a Cookie
from the given HTTP cookie header value string. Does
not perform any percent-decoding.
§Example
use cookie::Cookie;
let c = Cookie::parse("foo=bar%20baz; HttpOnly").unwrap();
assert_eq!(c.name_value(), ("foo", "bar%20baz"));
assert_eq!(c.http_only(), Some(true));
assert_eq!(c.secure(), None);
Sourcepub fn parse_encoded<S>(s: S) -> Result<Cookie<'c>, ParseError>
pub fn parse_encoded<S>(s: S) -> Result<Cookie<'c>, ParseError>
Parses a Cookie
from the given HTTP cookie header value string where
the name and value fields are percent-encoded. Percent-decodes the
name/value fields.
§Example
use cookie::Cookie;
let c = Cookie::parse_encoded("foo=bar%20baz; HttpOnly").unwrap();
assert_eq!(c.name_value(), ("foo", "bar baz"));
assert_eq!(c.http_only(), Some(true));
assert_eq!(c.secure(), None);
Sourcepub fn split_parse<S>(string: S) -> SplitCookies<'c> ⓘ
pub fn split_parse<S>(string: S) -> SplitCookies<'c> ⓘ
Parses the HTTP Cookie
header, a series of cookie names and value
separated by ;
, returning an iterator over the parse results. Each
item returned by the iterator is a Result<Cookie, ParseError>
of
parsing one name/value pair. Empty cookie values (i.e, in a=1;;b=2
)
and any excess surrounding whitespace are ignored.
Unlike Cookie::split_parse_encoded()
, this method does not
percent-decode keys and values.
§Example
use cookie::Cookie;
let string = "name=value; other=key%20value";
for cookie in Cookie::split_parse(string) {
let cookie = cookie.unwrap();
match cookie.name() {
"name" => assert_eq!(cookie.value(), "value"),
"other" => assert_eq!(cookie.value(), "key%20value"),
_ => unreachable!()
}
}
Sourcepub fn split_parse_encoded<S>(string: S) -> SplitCookies<'c> ⓘ
pub fn split_parse_encoded<S>(string: S) -> SplitCookies<'c> ⓘ
Parses the HTTP Cookie
header, a series of cookie names and value
separated by ;
, returning an iterator over the parse results. Each
item returned by the iterator is a Result<Cookie, ParseError>
of
parsing one name/value pair. Empty cookie values (i.e, in a=1;;b=2
)
and any excess surrounding whitespace are ignored.
Unlike Cookie::split_parse()
, this method does percent-decode keys
and values.
§Example
use cookie::Cookie;
let string = "name=value; other=key%20value";
for cookie in Cookie::split_parse_encoded(string) {
let cookie = cookie.unwrap();
match cookie.name() {
"name" => assert_eq!(cookie.value(), "value"),
"other" => assert_eq!(cookie.value(), "key value"),
_ => unreachable!()
}
}
Sourcepub fn into_owned(self) -> Cookie<'static>
pub fn into_owned(self) -> Cookie<'static>
Converts self
into a Cookie
with a static lifetime with as few
allocations as possible.
§Example
use cookie::Cookie;
let c = Cookie::new("a", "b");
let owned_cookie = c.into_owned();
assert_eq!(owned_cookie.name_value(), ("a", "b"));
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the name of self
.
§Example
use cookie::Cookie;
let c = Cookie::new("name", "value");
assert_eq!(c.name(), "name");
Sourcepub fn value(&self) -> &str
pub fn value(&self) -> &str
Returns the value of self
.
Does not strip surrounding quotes. See Cookie::value_trimmed()
for a
version that does.
§Example
use cookie::Cookie;
let c = Cookie::new("name", "value");
assert_eq!(c.value(), "value");
let c = Cookie::new("name", "\"value\"");
assert_eq!(c.value(), "\"value\"");
Sourcepub fn value_trimmed(&self) -> &str
pub fn value_trimmed(&self) -> &str
Returns the value of self
with surrounding double-quotes trimmed.
This is not the value of the cookie (that is Cookie::value()
).
Instead, this is the value with a surrounding pair of double-quotes, if
any, trimmed away. Quotes are only trimmed when they form a pair and
never otherwise. The trimmed value is never used for other operations,
such as equality checking, on self
.
§Example
use cookie::Cookie;
let c0 = Cookie::new("name", "value");
assert_eq!(c0.value_trimmed(), "value");
let c = Cookie::new("name", "\"value\"");
assert_eq!(c.value_trimmed(), "value");
assert!(c != c0);
let c = Cookie::new("name", "\"value");
assert_eq!(c.value(), "\"value");
assert_eq!(c.value_trimmed(), "\"value");
assert!(c != c0);
let c = Cookie::new("name", "\"value\"\"");
assert_eq!(c.value(), "\"value\"\"");
assert_eq!(c.value_trimmed(), "value\"");
assert!(c != c0);
Sourcepub fn name_value(&self) -> (&str, &str)
pub fn name_value(&self) -> (&str, &str)
Returns the name and value of self
as a tuple of (name, value)
.
§Example
use cookie::Cookie;
let c = Cookie::new("name", "value");
assert_eq!(c.name_value(), ("name", "value"));
Sourcepub fn name_value_trimmed(&self) -> (&str, &str)
pub fn name_value_trimmed(&self) -> (&str, &str)
Returns the name and trimmed value of self
as a tuple of (name, trimmed_value)
.
§Example
use cookie::Cookie;
let c = Cookie::new("name", "\"value\"");
assert_eq!(c.name_value_trimmed(), ("name", "value"));
Sourcepub fn http_only(&self) -> Option<bool>
pub fn http_only(&self) -> Option<bool>
Returns whether this cookie was marked HttpOnly
or not. Returns
Some(true)
when the cookie was explicitly set (manually or parsed) as
HttpOnly
, Some(false)
when http_only
was manually set to false
,
and None
otherwise.
§Example
use cookie::Cookie;
let c = Cookie::parse("name=value; httponly").unwrap();
assert_eq!(c.http_only(), Some(true));
let mut c = Cookie::new("name", "value");
assert_eq!(c.http_only(), None);
let mut c = Cookie::new("name", "value");
assert_eq!(c.http_only(), None);
// An explicitly set "false" value.
c.set_http_only(false);
assert_eq!(c.http_only(), Some(false));
// An explicitly set "true" value.
c.set_http_only(true);
assert_eq!(c.http_only(), Some(true));
Sourcepub fn secure(&self) -> Option<bool>
pub fn secure(&self) -> Option<bool>
Returns whether this cookie was marked Secure
or not. Returns
Some(true)
when the cookie was explicitly set (manually or parsed) as
Secure
, Some(false)
when secure
was manually set to false
, and
None
otherwise.
§Example
use cookie::Cookie;
let c = Cookie::parse("name=value; Secure").unwrap();
assert_eq!(c.secure(), Some(true));
let mut c = Cookie::parse("name=value").unwrap();
assert_eq!(c.secure(), None);
let mut c = Cookie::new("name", "value");
assert_eq!(c.secure(), None);
// An explicitly set "false" value.
c.set_secure(false);
assert_eq!(c.secure(), Some(false));
// An explicitly set "true" value.
c.set_secure(true);
assert_eq!(c.secure(), Some(true));
Sourcepub fn same_site(&self) -> Option<SameSite>
pub fn same_site(&self) -> Option<SameSite>
Returns the SameSite
attribute of this cookie if one was specified.
§Example
use cookie::{Cookie, SameSite};
let c = Cookie::parse("name=value; SameSite=Lax").unwrap();
assert_eq!(c.same_site(), Some(SameSite::Lax));
Sourcepub fn partitioned(&self) -> Option<bool>
pub fn partitioned(&self) -> Option<bool>
Returns whether this cookie was marked Partitioned
or not. Returns
Some(true)
when the cookie was explicitly set (manually or parsed) as
Partitioned
, Some(false)
when partitioned
was manually set to false
,
and None
otherwise.
Note: This cookie attribute is an HTTP draft! Its meaning and definition are not standardized and therefore subject to change.
§Example
use cookie::Cookie;
let c = Cookie::parse("name=value; Partitioned").unwrap();
assert_eq!(c.partitioned(), Some(true));
let mut c = Cookie::parse("name=value").unwrap();
assert_eq!(c.partitioned(), None);
let mut c = Cookie::new("name", "value");
assert_eq!(c.partitioned(), None);
// An explicitly set "false" value.
c.set_partitioned(false);
assert_eq!(c.partitioned(), Some(false));
// An explicitly set "true" value.
c.set_partitioned(true);
assert_eq!(c.partitioned(), Some(true));
Sourcepub fn max_age(&self) -> Option<Duration>
pub fn max_age(&self) -> Option<Duration>
Returns the specified max-age of the cookie if one was specified.
§Example
use cookie::Cookie;
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.max_age(), None);
let c = Cookie::parse("name=value; Max-Age=3600").unwrap();
assert_eq!(c.max_age().map(|age| age.whole_hours()), Some(1));
Sourcepub fn path(&self) -> Option<&str>
pub fn path(&self) -> Option<&str>
Returns the Path
of the cookie if one was specified.
§Example
use cookie::Cookie;
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.path(), None);
let c = Cookie::parse("name=value; Path=/").unwrap();
assert_eq!(c.path(), Some("/"));
let c = Cookie::parse("name=value; path=/sub").unwrap();
assert_eq!(c.path(), Some("/sub"));
Sourcepub fn domain(&self) -> Option<&str>
pub fn domain(&self) -> Option<&str>
Returns the Domain
of the cookie if one was specified.
This does not consider whether the Domain
is valid; validation is left
to higher-level libraries, as needed. However, if the Domain
starts
with a leading .
, the leading .
is stripped.
§Example
use cookie::Cookie;
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.domain(), None);
let c = Cookie::parse("name=value; Domain=crates.io").unwrap();
assert_eq!(c.domain(), Some("crates.io"));
let c = Cookie::parse("name=value; Domain=.crates.io").unwrap();
assert_eq!(c.domain(), Some("crates.io"));
// Note that `..crates.io` is not a valid domain.
let c = Cookie::parse("name=value; Domain=..crates.io").unwrap();
assert_eq!(c.domain(), Some(".crates.io"));
Sourcepub fn expires(&self) -> Option<Expiration>
pub fn expires(&self) -> Option<Expiration>
Returns the Expiration
of the cookie if one was specified.
§Example
use cookie::{Cookie, Expiration};
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.expires(), None);
// Here, `cookie.expires_datetime()` returns `None`.
let c = Cookie::build(("name", "value")).expires(None).build();
assert_eq!(c.expires(), Some(Expiration::Session));
let expire_time = "Wed, 21 Oct 2017 07:28:00 GMT";
let cookie_str = format!("name=value; Expires={}", expire_time);
let c = Cookie::parse(cookie_str).unwrap();
assert_eq!(c.expires().and_then(|e| e.datetime()).map(|t| t.year()), Some(2017));
Sourcepub fn expires_datetime(&self) -> Option<OffsetDateTime>
pub fn expires_datetime(&self) -> Option<OffsetDateTime>
Returns the expiration date-time of the cookie if one was specified.
§Example
use cookie::Cookie;
let c = Cookie::parse("name=value").unwrap();
assert_eq!(c.expires_datetime(), None);
// Here, `cookie.expires()` returns `Some`.
let c = Cookie::build(("name", "value")).expires(None).build();
assert_eq!(c.expires_datetime(), None);
let expire_time = "Wed, 21 Oct 2017 07:28:00 GMT";
let cookie_str = format!("name=value; Expires={}", expire_time);
let c = Cookie::parse(cookie_str).unwrap();
assert_eq!(c.expires_datetime().map(|t| t.year()), Some(2017));
Sourcepub fn set_name<N>(&mut self, name: N)
pub fn set_name<N>(&mut self, name: N)
Sets the name of self
to name
.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.name(), "name");
c.set_name("foo");
assert_eq!(c.name(), "foo");
Sourcepub fn set_value<V>(&mut self, value: V)
pub fn set_value<V>(&mut self, value: V)
Sets the value of self
to value
.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.value(), "value");
c.set_value("bar");
assert_eq!(c.value(), "bar");
Sourcepub fn set_http_only<T>(&mut self, value: T)
pub fn set_http_only<T>(&mut self, value: T)
Sets the value of http_only
in self
to value
. If value
is
None
, the field is unset.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.http_only(), None);
c.set_http_only(true);
assert_eq!(c.http_only(), Some(true));
c.set_http_only(false);
assert_eq!(c.http_only(), Some(false));
c.set_http_only(None);
assert_eq!(c.http_only(), None);
Sourcepub fn set_secure<T>(&mut self, value: T)
pub fn set_secure<T>(&mut self, value: T)
Sets the value of secure
in self
to value
. If value
is None
,
the field is unset.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.secure(), None);
c.set_secure(true);
assert_eq!(c.secure(), Some(true));
c.set_secure(false);
assert_eq!(c.secure(), Some(false));
c.set_secure(None);
assert_eq!(c.secure(), None);
Sourcepub fn set_same_site<T>(&mut self, value: T)
pub fn set_same_site<T>(&mut self, value: T)
Sets the value of same_site
in self
to value
. If value
is
None
, the field is unset. If value
is SameSite::None
, the “Secure”
flag will be set when the cookie is written out unless secure
is
explicitly set to false
via Cookie::set_secure()
or the equivalent
builder method.
§Example
use cookie::{Cookie, SameSite};
let mut c = Cookie::new("name", "value");
assert_eq!(c.same_site(), None);
c.set_same_site(SameSite::None);
assert_eq!(c.same_site(), Some(SameSite::None));
assert_eq!(c.to_string(), "name=value; SameSite=None; Secure");
c.set_secure(false);
assert_eq!(c.to_string(), "name=value; SameSite=None");
let mut c = Cookie::new("name", "value");
assert_eq!(c.same_site(), None);
c.set_same_site(SameSite::Strict);
assert_eq!(c.same_site(), Some(SameSite::Strict));
assert_eq!(c.to_string(), "name=value; SameSite=Strict");
c.set_same_site(None);
assert_eq!(c.same_site(), None);
assert_eq!(c.to_string(), "name=value");
Sourcepub fn set_partitioned<T>(&mut self, value: T)
pub fn set_partitioned<T>(&mut self, value: T)
Sets the value of partitioned
in self
to value
. If value
is
None
, the field is unset.
Note: Partitioned cookies require the Secure
attribute to be
set. As such, Partitioned
cookies are always rendered with the
Secure
attribute, irrespective of the Secure
attribute’s setting.
Note: This cookie attribute is an HTTP draft! Its meaning and definition are not standardized and therefore subject to change.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.partitioned(), None);
c.set_partitioned(true);
assert_eq!(c.partitioned(), Some(true));
assert!(c.to_string().contains("Secure"));
c.set_partitioned(false);
assert_eq!(c.partitioned(), Some(false));
assert!(!c.to_string().contains("Secure"));
c.set_partitioned(None);
assert_eq!(c.partitioned(), None);
assert!(!c.to_string().contains("Secure"));
Sourcepub fn set_max_age<D>(&mut self, value: D)
pub fn set_max_age<D>(&mut self, value: D)
Sets the value of max_age
in self
to value
. If value
is None
,
the field is unset.
§Example
use cookie::Cookie;
use cookie::time::Duration;
let mut c = Cookie::new("name", "value");
assert_eq!(c.max_age(), None);
c.set_max_age(Duration::hours(10));
assert_eq!(c.max_age(), Some(Duration::hours(10)));
c.set_max_age(None);
assert!(c.max_age().is_none());
Sourcepub fn set_path<P>(&mut self, path: P)
pub fn set_path<P>(&mut self, path: P)
Sets the path
of self
to path
.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.path(), None);
c.set_path("/");
assert_eq!(c.path(), Some("/"));
Sourcepub fn unset_path(&mut self)
pub fn unset_path(&mut self)
Unsets the path
of self
.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.path(), None);
c.set_path("/");
assert_eq!(c.path(), Some("/"));
c.unset_path();
assert_eq!(c.path(), None);
Sourcepub fn set_domain<D>(&mut self, domain: D)
pub fn set_domain<D>(&mut self, domain: D)
Sets the domain
of self
to domain
.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.domain(), None);
c.set_domain("rust-lang.org");
assert_eq!(c.domain(), Some("rust-lang.org"));
Sourcepub fn unset_domain(&mut self)
pub fn unset_domain(&mut self)
Unsets the domain
of self
.
§Example
use cookie::Cookie;
let mut c = Cookie::new("name", "value");
assert_eq!(c.domain(), None);
c.set_domain("rust-lang.org");
assert_eq!(c.domain(), Some("rust-lang.org"));
c.unset_domain();
assert_eq!(c.domain(), None);
Sourcepub fn set_expires<T>(&mut self, time: T)where
T: Into<Expiration>,
pub fn set_expires<T>(&mut self, time: T)where
T: Into<Expiration>,
Sets the expires field of self
to time
. If time
is None
, an
expiration of Session
is set.
§Example
use cookie::{Cookie, Expiration};
use cookie::time::{Duration, OffsetDateTime};
let mut c = Cookie::new("name", "value");
assert_eq!(c.expires(), None);
let mut now = OffsetDateTime::now_utc();
now += Duration::weeks(52);
c.set_expires(now);
assert!(c.expires().is_some());
c.set_expires(None);
assert_eq!(c.expires(), Some(Expiration::Session));
Sourcepub fn unset_expires(&mut self)
pub fn unset_expires(&mut self)
Unsets the expires
of self
.
§Example
use cookie::{Cookie, Expiration};
let mut c = Cookie::new("name", "value");
assert_eq!(c.expires(), None);
c.set_expires(None);
assert_eq!(c.expires(), Some(Expiration::Session));
c.unset_expires();
assert_eq!(c.expires(), None);
Sourcepub fn make_permanent(&mut self)
pub fn make_permanent(&mut self)
Makes self
a “permanent” cookie by extending its expiration and max
age 20 years into the future.
§Example
use cookie::Cookie;
use cookie::time::Duration;
let mut c = Cookie::new("foo", "bar");
assert!(c.expires().is_none());
assert!(c.max_age().is_none());
c.make_permanent();
assert!(c.expires().is_some());
assert_eq!(c.max_age(), Some(Duration::days(365 * 20)));
Sourcepub fn make_removal(&mut self)
pub fn make_removal(&mut self)
Make self
a “removal” cookie by clearing its value, setting a max-age
of 0
, and setting an expiration date far in the past.
§Example
use cookie::Cookie;
use cookie::time::Duration;
let mut c = Cookie::new("foo", "bar");
c.make_permanent();
assert_eq!(c.max_age(), Some(Duration::days(365 * 20)));
assert_eq!(c.value(), "bar");
c.make_removal();
assert_eq!(c.value(), "");
assert_eq!(c.max_age(), Some(Duration::ZERO));
Sourcepub fn name_raw(&self) -> Option<&'c str>
pub fn name_raw(&self) -> Option<&'c str>
Returns the name of self
as a string slice of the raw string self
was originally parsed from. If self
was not originally parsed from a
raw string, returns None
.
This method differs from Cookie::name()
in that it returns a string
with the same lifetime as the originally parsed string. This lifetime
may outlive self
. If a longer lifetime is not required, or you’re
unsure if you need a longer lifetime, use Cookie::name()
.
§Example
use cookie::Cookie;
let cookie_string = format!("{}={}", "foo", "bar");
// `c` will be dropped at the end of the scope, but `name` will live on
let name = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
c.name_raw()
};
assert_eq!(name, Some("foo"));
Sourcepub fn value_raw(&self) -> Option<&'c str>
pub fn value_raw(&self) -> Option<&'c str>
Returns the value of self
as a string slice of the raw string self
was originally parsed from. If self
was not originally parsed from a
raw string, returns None
.
This method differs from Cookie::value()
in that it returns a
string with the same lifetime as the originally parsed string. This
lifetime may outlive self
. If a longer lifetime is not required, or
you’re unsure if you need a longer lifetime, use Cookie::value()
.
§Example
use cookie::Cookie;
let cookie_string = format!("{}={}", "foo", "bar");
// `c` will be dropped at the end of the scope, but `value` will live on
let value = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
c.value_raw()
};
assert_eq!(value, Some("bar"));
Sourcepub fn path_raw(&self) -> Option<&'c str>
pub fn path_raw(&self) -> Option<&'c str>
Returns the Path
of self
as a string slice of the raw string self
was originally parsed from. If self
was not originally parsed from a
raw string, or if self
doesn’t contain a Path
, or if the Path
has
changed since parsing, returns None
.
This method differs from Cookie::path()
in that it returns a
string with the same lifetime as the originally parsed string. This
lifetime may outlive self
. If a longer lifetime is not required, or
you’re unsure if you need a longer lifetime, use Cookie::path()
.
§Example
use cookie::Cookie;
let cookie_string = format!("{}={}; Path=/", "foo", "bar");
// `c` will be dropped at the end of the scope, but `path` will live on
let path = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
c.path_raw()
};
assert_eq!(path, Some("/"));
Sourcepub fn domain_raw(&self) -> Option<&'c str>
pub fn domain_raw(&self) -> Option<&'c str>
Returns the Domain
of self
as a string slice of the raw string
self
was originally parsed from. If self
was not originally parsed
from a raw string, or if self
doesn’t contain a Domain
, or if the
Domain
has changed since parsing, returns None
.
Like Cookie::domain()
, this does not consider whether Domain
is
valid; validation is left to higher-level libraries, as needed. However,
if Domain
starts with a leading .
, the leading .
is stripped.
This method differs from Cookie::domain()
in that it returns a
string with the same lifetime as the originally parsed string. This
lifetime may outlive self
struct. If a longer lifetime is not
required, or you’re unsure if you need a longer lifetime, use
Cookie::domain()
.
§Example
use cookie::Cookie;
let cookie_string = format!("{}={}; Domain=.crates.io", "foo", "bar");
//`c` will be dropped at the end of the scope, but `domain` will live on
let domain = {
let c = Cookie::parse(cookie_string.as_str()).unwrap();
c.domain_raw()
};
assert_eq!(domain, Some("crates.io"));
Sourcepub fn encoded<'a>(&'a self) -> Display<'a, 'c>
pub fn encoded<'a>(&'a self) -> Display<'a, 'c>
Wraps self
in an encoded Display
: a cost-free wrapper around
Cookie
whose fmt::Display
implementation percent-encodes the name
and value of the wrapped Cookie
.
The returned structure can be chained with Display::stripped()
to
display only the name and value.
§Example
use cookie::Cookie;
let mut c = Cookie::build(("my name", "this; value?")).secure(true).build();
assert_eq!(&c.encoded().to_string(), "my%20name=this%3B%20value%3F; Secure");
assert_eq!(&c.encoded().stripped().to_string(), "my%20name=this%3B%20value%3F");
Sourcepub fn stripped<'a>(&'a self) -> Display<'a, 'c>
pub fn stripped<'a>(&'a self) -> Display<'a, 'c>
Wraps self
in a stripped Display
]: a cost-free wrapper around
Cookie
whose fmt::Display
implementation prints only the name
and value
of the wrapped Cookie
.
The returned structure can be chained with Display::encoded()
to
encode the name and value.
§Example
use cookie::Cookie;
let mut c = Cookie::build(("key?", "value")).secure(true).path("/").build();
assert_eq!(&c.stripped().to_string(), "key?=value");
// Note: `encoded()` is only available when `percent-encode` is enabled.
assert_eq!(&c.stripped().encoded().to_string(), "key%3F=value");
Trait Implementations§
Source§impl<'a> AsMut<Cookie<'a>> for CookieBuilder<'a>
impl<'a> AsMut<Cookie<'a>> for CookieBuilder<'a>
Source§impl<'a> AsRef<Cookie<'a>> for CookieBuilder<'a>
impl<'a> AsRef<Cookie<'a>> for CookieBuilder<'a>
Source§impl<'a> Borrow<Cookie<'a>> for CookieBuilder<'a>
impl<'a> Borrow<Cookie<'a>> for CookieBuilder<'a>
Source§impl<'a> BorrowMut<Cookie<'a>> for CookieBuilder<'a>
impl<'a> BorrowMut<Cookie<'a>> for CookieBuilder<'a>
Source§fn borrow_mut(&mut self) -> &mut Cookie<'a>
fn borrow_mut(&mut self) -> &mut Cookie<'a>
Source§impl<'c> Display for Cookie<'c>
impl<'c> Display for Cookie<'c>
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Formats the cookie self
as a Set-Cookie
header value.
Does not percent-encode any values. To percent-encode, use
Cookie::encoded()
.
§Example
use cookie::Cookie;
let mut cookie = Cookie::build(("foo", "bar")).path("/");
assert_eq!(cookie.to_string(), "foo=bar; Path=/");
Source§impl<'c> From<Cookie<'c>> for CookieBuilder<'c>
impl<'c> From<Cookie<'c>> for CookieBuilder<'c>
Source§fn from(cookie: Cookie<'c>) -> CookieBuilder<'c>
fn from(cookie: Cookie<'c>) -> CookieBuilder<'c>
Source§impl<'a> From<CookieBuilder<'a>> for Cookie<'a>
impl<'a> From<CookieBuilder<'a>> for Cookie<'a>
Source§fn from(builder: CookieBuilder<'a>) -> Cookie<'a>
fn from(builder: CookieBuilder<'a>) -> Cookie<'a>
Source§impl<'a, 'b> PartialEq<Cookie<'b>> for CookieBuilder<'a>
impl<'a, 'b> PartialEq<Cookie<'b>> for CookieBuilder<'a>
Source§impl<'a, 'b> PartialEq<CookieBuilder<'b>> for Cookie<'a>
impl<'a, 'b> PartialEq<CookieBuilder<'b>> for Cookie<'a>
Auto Trait Implementations§
impl<'c> Freeze for Cookie<'c>
impl<'c> RefUnwindSafe for Cookie<'c>
impl<'c> Send for Cookie<'c>
impl<'c> Sync for Cookie<'c>
impl<'c> Unpin for Cookie<'c>
impl<'c> UnwindSafe for Cookie<'c>
Blanket Implementations§
Source§impl<T> AsAny for Twhere
T: Any,
impl<T> AsAny for Twhere
T: Any,
fn as_any_ref(&self) -> &(dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.bright_black());
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.bright_green());
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.bright_yellow());
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.bright_magenta());
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.bright_white());
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.on_bright_black());
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.on_bright_green());
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.on_bright_yellow());
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
§Example
println!("{}", value.on_bright_blue());
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.on_bright_magenta());
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
§Example
println!("{}", value.on_bright_cyan());
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.on_bright_white());
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
§Example
println!("{}", value.underline());
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
§Example
println!("{}", value.rapid_blink());
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);