1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use std::{
borrow::{Borrow, Cow},
fmt,
time::Duration,
};
use crate::Cookie;
use super::{expires::Expires, same_site::SameSite};
/// A builder struct for building a [`Cookie`].
#[derive(PartialEq, Clone)]
pub struct CookieBuilder(Cookie);
impl CookieBuilder {
/// Build a new cookie. This returns a `CookieBuilder` that can be used to set other attribute
/// values.
///
/// # Example
/// ```rust
/// use cookie_monster::CookieBuilder;
///
/// let cookie = CookieBuilder::new("foo", "bar")
/// .secure()
/// .http_only()
/// .build();
///
/// assert!(cookie.is_secure());
/// assert!(cookie.is_http_only());
/// ```
#[inline]
pub fn new<N, V>(name: N, value: V) -> CookieBuilder
where
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
{
CookieBuilder(Cookie::new(name, value))
}
/// Sets the name of the cookie.
#[inline]
pub fn name<N: Into<Cow<'static, str>>>(mut self, name: N) -> Self {
self.0.set_name(name);
self
}
/// Returns the name of the cookie.
pub fn get_name(&self) -> &str {
self.0.name()
}
/// Sets the name of the cookie.
#[inline]
pub fn set_name<N: Into<Cow<'static, str>>>(&mut self, name: N) {
self.0.set_name(name);
}
/// Stores the cookie name prefix flavour. Used by [`Cookie::host`] / [`Cookie::secure`].
#[inline]
pub(crate) fn with_prefix(mut self, prefix: super::prefix::CookiePrefix) -> Self {
self.0.prefix = Some(prefix);
self
}
/// Sets the value of the cookie.
#[inline]
pub fn value<V: Into<Cow<'static, str>>>(mut self, value: V) -> Self {
self.0.set_value(value);
self
}
/// Returns the value of the cookie.
pub fn get_value(&self) -> &str {
self.0.value()
}
/// Sets the value of the cookie.
pub fn set_value<V>(&mut self, value: V)
where
V: Into<Cow<'static, str>>,
{
self.0.value = value.into().into();
}
/// Sets the Expires attribute of the cookie.
///
/// The argument can be a few different types, based on what features are enabled.
///
/// # No features needed
///
/// ```rust
/// use cookie_monster::{Cookie, Expires};
///
/// let cookie = Cookie::build("foo", "bar")
/// .expires(Expires::remove())
/// .build();
///
/// assert!(cookie.expires_is_set());
/// ```
///
/// # Jiff
/// ```rust
/// # #[cfg(feature="jiff")]
/// # {
/// # use cookie_monster::Cookie;
/// use jiff::Zoned;
///
/// let cookie = Cookie::build("foo", "bar")
/// .expires(Zoned::now())
/// .build();
///
/// # assert!(cookie.expires_is_set());
/// # }
/// ```
///
/// # Chrono
/// ```rust
/// # #[cfg(feature="chrono")]
/// # {
/// # use cookie_monster::Cookie;
/// use chrono::Utc;
///
/// let cookie = Cookie::build("foo", "bar")
/// .expires(Utc::now())
/// .build();
///
/// # assert!(cookie.expires_is_set());
/// # }
/// ```
///
/// # Time
/// ```rust
/// # #[cfg(feature="time")]
/// # {
/// # use cookie_monster::Cookie;
/// use time::OffsetDateTime;
///
/// let cookie = Cookie::build("foo", "bar")
/// .expires(OffsetDateTime::now_utc())
/// .build();
///
/// # assert!(cookie.expires_is_set());
/// # }
/// ```
#[inline]
pub fn expires(mut self, expiration: impl Into<Expires>) -> Self {
self.0.set_expires(expiration.into());
self
}
/// Sets the Expires attribute of the cookie.
pub fn set_expires(&mut self, expiration: impl Into<Expires>) {
self.0.set_expires(expiration.into());
}
/// Sets the Max-Age attribute of the cookie.
///
/// # Example
/// ```rust
/// use cookie_monster::Cookie;
///
/// let cookie = Cookie::build("foo", "bar")
/// .max_age_secs(100)
/// .build();
///
/// assert_eq!(cookie.max_age_secs(), Some(100));
/// ```
#[inline]
pub fn max_age_secs(mut self, max_age_secs: u64) -> Self {
self.0.set_max_age_secs(max_age_secs);
self
}
/// Sets the Max-Age attribute in seconds.
pub fn set_max_age_secs(&mut self, max_age_secs: u64) {
self.0.set_max_age_secs(max_age_secs);
}
/// Sets the Max-Age attribute of the cookie.
///
/// # Example
/// ```rust
/// use cookie_monster::Cookie;
/// use std::time::Duration;
///
/// let cookie = Cookie::build("foo", "bar")
/// .max_age(Duration::from_secs(100))
/// .build();
///
/// assert_eq!(cookie.max_age(), Some(Duration::from_secs(100)));
/// ```
#[inline]
pub fn max_age(mut self, max_age: Duration) -> Self {
self.0.set_max_age(max_age);
self
}
/// Returns the Max-Age attribute of the cookie.
pub fn get_max_age(&self) -> Option<Duration> {
self.0.max_age()
}
/// Sets the Max-Age attribute of the cookie.
#[inline]
pub fn set_max_age(&mut self, max_age: Duration) {
self.0.set_max_age(max_age);
}
/// Sets the Domain attribute of the cookie.
///
/// # Note
/// If the domain attribute is set to an empty string or the string contains an invalid cookie
/// character, the attribute is ignored.
///
/// # Example
/// ```rust
/// use cookie_monster::Cookie;
///
/// let cookie = Cookie::build("foo", "bar")
/// .domain("rust-lang.com")
/// .build();
///
/// assert_eq!(cookie.domain(), Some("rust-lang.com"));
/// ```
#[inline]
pub fn domain<D: Into<Cow<'static, str>>>(mut self, domain: D) -> Self {
self.0.set_domain(domain);
self
}
/// Sets the Domain attribute of the cookie.
#[inline]
pub fn set_domain<D: Into<Cow<'static, str>>>(&mut self, domain: D) {
self.0.set_domain(domain);
}
/// Sets the Path attribute of the cookie.
///
/// # Note
/// Not all path value's are allowed by the standard:
/// * The path can't be set to and empty string.
/// * The path must start with a leading `/`.
/// * The path can't contain invalid cookie characters.
///
/// If any of these conditions are not met, serializing this cookie returns an error.
///
/// # Example
/// ```rust
/// use cookie_monster::Cookie;
///
/// let cookie = Cookie::build("foo", "bar")
/// .path("/api/login")
/// .build();
///
/// assert_eq!(cookie.path(), Some("/api/login"));
/// ```
#[inline]
pub fn path<D: Into<Cow<'static, str>>>(mut self, path: D) -> Self {
self.0.set_path(path);
self
}
/// Returns the Path attribute of the cookie.
pub fn get_path(&self) -> Option<&str> {
self.0.path()
}
/// Sets the Path attribute of the cookie.
#[inline]
pub fn set_path<D: Into<Cow<'static, str>>>(&mut self, path: D) {
self.0.set_path(path);
}
/// Sets the Secure attribute of the cookie.
///
/// # Example
/// ```rust
/// use cookie_monster::Cookie;
///
/// let cookie = Cookie::build("foo", "bar")
/// .secure()
/// .build();
///
/// assert!(cookie.is_secure());
/// ```
#[inline]
pub fn secure(mut self) -> Self {
self.0.set_secure(true);
self
}
/// Sets the Secure attribute.
#[inline]
pub fn set_secure(mut self, secure: bool) -> Self {
self.0.set_secure(secure);
self
}
/// Sets the HttpOnly attribute of the cookie.
///
/// # Example
/// ```rust
/// use cookie_monster::Cookie;
///
/// let cookie = Cookie::build("foo", "bar")
/// .http_only()
/// .build();
///
/// assert!(cookie.is_http_only());
/// ```
#[inline]
pub fn http_only(mut self) -> Self {
self.0.set_http_only(true);
self
}
/// Sets the HttpOnly attribute of the cookie.
#[inline]
pub fn set_http_only(mut self, http_only: bool) -> Self {
self.0.set_http_only(http_only);
self
}
/// Sets the Partitioned attribute of the cookie. When the partitioned attribute is enabled, the
/// secure flag is also enabled while serializing.
///
/// <https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies>
///
/// # Example
/// ```rust
/// use cookie_monster::Cookie;
///
/// let cookie = Cookie::build("foo", "bar")
/// .partitioned()
/// .build();
///
/// assert!(cookie.is_partitioned());
/// ```
#[inline]
pub fn partitioned(self) -> Self {
self.set_partitioned(true)
}
/// Set the Partitioned flag, enabling the Partitioned attribute also enables the Secure Attribute.
#[inline]
pub fn set_partitioned(mut self, partitioned: bool) -> Self {
self.0.set_partitioned(partitioned);
self
}
/// Sets the SameSite attribute value of the cookie.
///
/// # Example
/// ```rust
/// use cookie_monster::{Cookie, SameSite};
///
/// let cookie = Cookie::build("foo", "bar")
/// .same_site(SameSite::Strict)
/// .build();
///
/// assert_eq!(cookie.same_site(), Some(SameSite::Strict));
/// ```
#[inline]
pub fn same_site<S: Into<Option<SameSite>>>(mut self, same_site: S) -> Self {
self.0.set_same_site(same_site);
self
}
/// Builds and returns the cookie
#[inline]
pub fn build(self) -> Cookie {
self.0
}
}
impl fmt::Debug for CookieBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl fmt::Display for CookieBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl Borrow<Cookie> for CookieBuilder {
fn borrow(&self) -> &Cookie {
&self.0
}
}
impl From<CookieBuilder> for Cookie {
fn from(value: CookieBuilder) -> Self {
value.build()
}
}