| 1 |
use std::borrow::Cow; |
| 2 |
|
| 3 |
use crate::{Cookie, SameSite, Expiration}; |
| 4 |
|
| 5 |
/// Structure that follows the builder pattern for building `Cookie` structs. |
| 6 |
/// |
| 7 |
/// To construct a cookie: |
| 8 |
/// |
| 9 |
/// 1. Call [`Cookie::build`] to start building. |
| 10 |
/// 2. Use any of the builder methods to set fields in the cookie. |
| 11 |
/// 3. Call [`CookieBuilder::finish()`] to retrieve the built cookie. |
| 12 |
/// |
| 13 |
/// # Example |
| 14 |
/// |
| 15 |
/// ```rust |
| 16 |
/// # extern crate cookie; |
| 17 |
/// use cookie::Cookie; |
| 18 |
/// use cookie::time::Duration; |
| 19 |
/// |
| 20 |
/// # fn main() { |
| 21 |
/// let cookie: Cookie = Cookie::build("name", "value") |
| 22 |
/// .domain("www.rust-lang.org") |
| 23 |
/// .path("/") |
| 24 |
/// .secure(true) |
| 25 |
/// .http_only(true) |
| 26 |
/// .max_age(Duration::days(1)) |
| 27 |
/// .finish(); |
| 28 |
/// # } |
| 29 |
/// ``` |
| 30 |
#[derive(Debug, Clone)] |
| 31 |
pub struct CookieBuilder<'c> { |
| 32 |
/// The cookie being built. |
| 33 |
cookie: Cookie<'c>, |
| 34 |
} |
| 35 |
|
| 36 |
impl<'c> CookieBuilder<'c> { |
| 37 |
/// Creates a new `CookieBuilder` instance from the given name and value. |
| 38 |
/// |
| 39 |
/// This method is typically called indirectly via [`Cookie::build()`]. |
| 40 |
/// |
| 41 |
/// # Example |
| 42 |
/// |
| 43 |
/// ```rust |
| 44 |
/// use cookie::Cookie; |
| 45 |
/// |
| 46 |
/// let c = Cookie::build("foo", "bar").finish(); |
| 47 |
/// assert_eq!(c.name_value(), ("foo", "bar")); |
| 48 |
/// ``` |
| 49 |
pub fn new<N, V>(name: N, value: V) -> Self |
| 50 |
where N: Into<Cow<'c, str>>, |
| 51 |
V: Into<Cow<'c, str>> |
| 52 |
{ |
| 53 |
CookieBuilder { cookie: Cookie::new(name, value) } |
| 54 |
} |
| 55 |
|
| 56 |
/// Sets the `expires` field in the cookie being built. |
| 57 |
/// |
| 58 |
/// # Example |
| 59 |
/// |
| 60 |
/// ```rust |
| 61 |
/// # extern crate cookie; |
| 62 |
/// use cookie::{Cookie, Expiration}; |
| 63 |
/// use cookie::time::OffsetDateTime; |
| 64 |
/// |
| 65 |
/// # fn main() { |
| 66 |
/// let c = Cookie::build("foo", "bar") |
| 67 |
/// .expires(OffsetDateTime::now_utc()) |
| 68 |
/// .finish(); |
| 69 |
/// |
| 70 |
/// assert!(c.expires().is_some()); |
| 71 |
/// |
| 72 |
/// let c = Cookie::build("foo", "bar") |
| 73 |
/// .expires(None) |
| 74 |
/// .finish(); |
| 75 |
/// |
| 76 |
/// assert_eq!(c.expires(), Some(Expiration::Session)); |
| 77 |
/// # } |
| 78 |
/// ``` |
| 79 |
#[inline] |
| 80 |
pub fn expires<E: Into<Expiration>>(mut self, when: E) -> Self { |
| 81 |
self.cookie.set_expires(when); |
| 82 |
self |
| 83 |
} |
| 84 |
|
| 85 |
/// Sets the `max_age` field in the cookie being built. |
| 86 |
/// |
| 87 |
/// # Example |
| 88 |
/// |
| 89 |
/// ```rust |
| 90 |
/// # extern crate cookie; |
| 91 |
/// use cookie::Cookie; |
| 92 |
/// use cookie::time::Duration; |
| 93 |
/// |
| 94 |
/// # fn main() { |
| 95 |
/// let c = Cookie::build("foo", "bar") |
| 96 |
/// .max_age(Duration::minutes(30)) |
| 97 |
/// .finish(); |
| 98 |
/// |
| 99 |
/// assert_eq!(c.max_age(), Some(Duration::seconds(30 * 60))); |
| 100 |
/// # } |
| 101 |
/// ``` |
| 102 |
#[inline] |
| 103 |
pub fn max_age(mut self, value: time::Duration) -> Self { |
| 104 |
self.cookie.set_max_age(value); |
| 105 |
self |
| 106 |
} |
| 107 |
|
| 108 |
/// Sets the `domain` field in the cookie being built. |
| 109 |
/// |
| 110 |
/// # Example |
| 111 |
/// |
| 112 |
/// ```rust |
| 113 |
/// use cookie::Cookie; |
| 114 |
/// |
| 115 |
/// let c = Cookie::build("foo", "bar") |
| 116 |
/// .domain("www.rust-lang.org") |
| 117 |
/// .finish(); |
| 118 |
/// |
| 119 |
/// assert_eq!(c.domain(), Some("www.rust-lang.org")); |
| 120 |
/// ``` |
| 121 |
pub fn domain<D: Into<Cow<'c, str>>>(mut self, value: D) -> Self { |
| 122 |
self.cookie.set_domain(value); |
| 123 |
self |
| 124 |
} |
| 125 |
|
| 126 |
/// Sets the `path` field in the cookie being built. |
| 127 |
/// |
| 128 |
/// # Example |
| 129 |
/// |
| 130 |
/// ```rust |
| 131 |
/// use cookie::Cookie; |
| 132 |
/// |
| 133 |
/// let c = Cookie::build("foo", "bar") |
| 134 |
/// .path("/") |
| 135 |
/// .finish(); |
| 136 |
/// |
| 137 |
/// assert_eq!(c.path(), Some("/")); |
| 138 |
/// ``` |
| 139 |
pub fn path<P: Into<Cow<'c, str>>>(mut self, path: P) -> Self { |
| 140 |
self.cookie.set_path(path); |
| 141 |
self |
| 142 |
} |
| 143 |
|
| 144 |
/// Sets the `secure` field in the cookie being built. |
| 145 |
/// |
| 146 |
/// # Example |
| 147 |
/// |
| 148 |
/// ```rust |
| 149 |
/// use cookie::Cookie; |
| 150 |
/// |
| 151 |
/// let c = Cookie::build("foo", "bar") |
| 152 |
/// .secure(true) |
| 153 |
/// .finish(); |
| 154 |
/// |
| 155 |
/// assert_eq!(c.secure(), Some(true)); |
| 156 |
/// ``` |
| 157 |
#[inline] |
| 158 |
pub fn secure(mut self, value: bool) -> Self { |
| 159 |
self.cookie.set_secure(value); |
| 160 |
self |
| 161 |
} |
| 162 |
|
| 163 |
/// Sets the `http_only` field in the cookie being built. |
| 164 |
/// |
| 165 |
/// # Example |
| 166 |
/// |
| 167 |
/// ```rust |
| 168 |
/// use cookie::Cookie; |
| 169 |
/// |
| 170 |
/// let c = Cookie::build("foo", "bar") |
| 171 |
/// .http_only(true) |
| 172 |
/// .finish(); |
| 173 |
/// |
| 174 |
/// assert_eq!(c.http_only(), Some(true)); |
| 175 |
/// ``` |
| 176 |
#[inline] |
| 177 |
pub fn http_only(mut self, value: bool) -> Self { |
| 178 |
self.cookie.set_http_only(value); |
| 179 |
self |
| 180 |
} |
| 181 |
|
| 182 |
/// Sets the `same_site` field in the cookie being built. |
| 183 |
/// |
| 184 |
/// # Example |
| 185 |
/// |
| 186 |
/// ```rust |
| 187 |
/// use cookie::{Cookie, SameSite}; |
| 188 |
/// |
| 189 |
/// let c = Cookie::build("foo", "bar") |
| 190 |
/// .same_site(SameSite::Strict) |
| 191 |
/// .finish(); |
| 192 |
/// |
| 193 |
/// assert_eq!(c.same_site(), Some(SameSite::Strict)); |
| 194 |
/// ``` |
| 195 |
#[inline] |
| 196 |
pub fn same_site(mut self, value: SameSite) -> Self { |
| 197 |
self.cookie.set_same_site(value); |
| 198 |
self |
| 199 |
} |
| 200 |
|
| 201 |
/// Makes the cookie being built 'permanent' by extending its expiration and |
| 202 |
/// max age 20 years into the future. |
| 203 |
/// |
| 204 |
/// # Example |
| 205 |
/// |
| 206 |
/// ```rust |
| 207 |
/// # extern crate cookie; |
| 208 |
/// use cookie::Cookie; |
| 209 |
/// use cookie::time::Duration; |
| 210 |
/// |
| 211 |
/// # fn main() { |
| 212 |
/// let c = Cookie::build("foo", "bar") |
| 213 |
/// .permanent() |
| 214 |
/// .finish(); |
| 215 |
/// |
| 216 |
/// assert_eq!(c.max_age(), Some(Duration::days(365 * 20))); |
| 217 |
/// # assert!(c.expires().is_some()); |
| 218 |
/// # } |
| 219 |
/// ``` |
| 220 |
#[inline] |
| 221 |
pub fn permanent(mut self) -> Self { |
| 222 |
self.cookie.make_permanent(); |
| 223 |
self |
| 224 |
} |
| 225 |
|
| 226 |
/// Finishes building and returns the built `Cookie`. |
| 227 |
/// |
| 228 |
/// # Example |
| 229 |
/// |
| 230 |
/// ```rust |
| 231 |
/// use cookie::Cookie; |
| 232 |
/// |
| 233 |
/// let c = Cookie::build("foo", "bar") |
| 234 |
/// .domain("crates.io") |
| 235 |
/// .path("/") |
| 236 |
/// .finish(); |
| 237 |
/// |
| 238 |
/// assert_eq!(c.name_value(), ("foo", "bar")); |
| 239 |
/// assert_eq!(c.domain(), Some("crates.io")); |
| 240 |
/// assert_eq!(c.path(), Some("/")); |
| 241 |
/// ``` |
| 242 |
#[inline] |
| 243 |
pub fn finish(self) -> Cookie<'c> { |
| 244 |
self.cookie |
| 245 |
} |
| 246 |
} |