1 //! This module contains types that represent cookie properties that are not yet
2 //! standardized. That is, _draft_ features.
3 
4 use std::fmt;
5 
6 /// The `SameSite` cookie attribute.
7 ///
8 /// A cookie with a `SameSite` attribute is imposed restrictions on when it is
9 /// sent to the origin server in a cross-site request. If the `SameSite`
10 /// attribute is "Strict", then the cookie is never sent in cross-site requests.
11 /// If the `SameSite` attribute is "Lax", the cookie is only sent in cross-site
12 /// requests with "safe" HTTP methods, i.e, `GET`, `HEAD`, `OPTIONS`, `TRACE`.
13 /// If the `SameSite` attribute is not present (made explicit via the
14 /// `SameSite::None` variant), then the cookie will be sent as normal.
15 ///
16 /// **Note:** This cookie attribute is an HTTP draft! Its meaning and definition
17 /// are subject to change.
18 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19 pub enum SameSite {
20     /// The "Strict" `SameSite` attribute.
21     Strict,
22     /// The "Lax" `SameSite` attribute.
23     Lax,
24     /// No `SameSite` attribute.
25     None
26 }
27 
28 impl SameSite {
29     /// Returns `true` if `self` is `SameSite::Strict` and `false` otherwise.
30     ///
31     /// # Example
32     ///
33     /// ```rust
34     /// use cookie::SameSite;
35     ///
36     /// let strict = SameSite::Strict;
37     /// assert!(strict.is_strict());
38     /// assert!(!strict.is_lax());
39     /// assert!(!strict.is_none());
40     /// ```
41     #[inline]
is_strict(&self) -> bool42     pub fn is_strict(&self) -> bool {
43         match *self {
44             SameSite::Strict => true,
45             SameSite::Lax | SameSite::None => false,
46         }
47     }
48 
49     /// Returns `true` if `self` is `SameSite::Lax` and `false` otherwise.
50     ///
51     /// # Example
52     ///
53     /// ```rust
54     /// use cookie::SameSite;
55     ///
56     /// let lax = SameSite::Lax;
57     /// assert!(lax.is_lax());
58     /// assert!(!lax.is_strict());
59     /// assert!(!lax.is_none());
60     /// ```
61     #[inline]
is_lax(&self) -> bool62     pub fn is_lax(&self) -> bool {
63         match *self {
64             SameSite::Lax => true,
65             SameSite::Strict | SameSite::None => false,
66         }
67     }
68 
69     /// Returns `true` if `self` is `SameSite::None` and `false` otherwise.
70     ///
71     /// # Example
72     ///
73     /// ```rust
74     /// use cookie::SameSite;
75     ///
76     /// let none = SameSite::None;
77     /// assert!(none.is_none());
78     /// assert!(!none.is_lax());
79     /// assert!(!none.is_strict());
80     /// ```
81     #[inline]
is_none(&self) -> bool82     pub fn is_none(&self) -> bool {
83         match *self {
84             SameSite::None => true,
85             SameSite::Lax | SameSite::Strict => false
86         }
87     }
88 }
89 
90 impl fmt::Display for SameSite {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result91     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92         match *self {
93             SameSite::Strict => write!(f, "Strict"),
94             SameSite::Lax => write!(f, "Lax"),
95             SameSite::None => Ok(()),
96         }
97     }
98 }
99