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
use crate::{ResponseCookie, ResponseCookieId};
use std::borrow::Cow;
use std::fmt::Debug;

#[derive(Debug, Clone)]
/// A [`ResponseCookie`] that, when sent to the client,
/// removes a cookie with the same [`ResponseCookieId`] from the client's machine, if it exists.
///
/// See [`ResponseCookies`]'s documentation for more details on cookie deletion.
///
/// [`ResponseCookies`]: crate::ResponseCookies
pub struct RemovalCookie<'c> {
    /// The cookie's name.
    pub(crate) name: Cow<'c, str>,
    /// The cookie's domain, if any.
    pub(crate) domain: Option<Cow<'c, str>>,
    /// The cookie's path domain, if any.
    pub(crate) path: Option<Cow<'c, str>>,
}

impl<'c> RemovalCookie<'c> {
    /// Creates a new [`RemovalCookie`] with the given name.
    ///
    /// # Example
    ///
    /// ```rust
    /// use biscotti::RemovalCookie;
    ///
    /// let removal = RemovalCookie::new("name")
    ///     .set_path("/");
    /// assert_eq!(removal.name(), "name");
    /// assert_eq!(removal.path(), Some("/"));
    /// assert_eq!(removal.domain(), None);
    /// ```
    pub fn new<N>(name: N) -> Self
    where
        N: Into<Cow<'c, str>>,
    {
        Self {
            name: name.into(),
            domain: None,
            path: None,
        }
    }

    /// Returns the name of `self`.
    ///
    /// # Example
    ///
    /// ```
    /// use biscotti::RemovalCookie;
    ///
    /// let c = RemovalCookie::new("name");
    /// assert_eq!(c.name(), "name");
    /// ```
    #[inline]
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }

    /// Returns the `Path` of the [`RemovalCookie`] if one was specified.
    ///
    /// # Example
    ///
    /// ```
    /// use biscotti::RemovalCookie;
    ///
    /// let c = RemovalCookie::new("name");
    /// assert_eq!(c.path(), None);
    ///
    /// let c = RemovalCookie::new("name").set_path("/");
    /// assert_eq!(c.path(), Some("/"));
    ///
    /// let c = RemovalCookie::new("name").set_path("/sub");
    /// assert_eq!(c.path(), Some("/sub"));
    /// ```
    #[inline]
    pub fn path(&self) -> Option<&str> {
        match self.path {
            Some(ref c) => Some(c.as_ref()),
            None => None,
        }
    }

    /// Returns the `Domain` of the [`RemovalCookie`] 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 biscotti::RemovalCookie;
    ///
    /// let c = RemovalCookie::new("name");
    /// assert_eq!(c.domain(), None);
    ///
    /// let c = RemovalCookie::new("name").set_domain("crates.io");
    /// assert_eq!(c.domain(), Some("crates.io"));
    ///
    /// let c = RemovalCookie::new("name").set_domain(".crates.io");
    /// assert_eq!(c.domain(), Some("crates.io"));
    ///
    /// // Note that `..crates.io` is not a valid domain.
    /// let c = RemovalCookie::new("name").set_domain("..crates.io");
    /// assert_eq!(c.domain(), Some(".crates.io"));
    /// ```
    #[inline]
    pub fn domain(&self) -> Option<&str> {
        match self.domain {
            Some(ref c) => {
                let domain = c.as_ref();
                domain.strip_prefix('.').or(Some(domain))
            }
            None => None,
        }
    }

    /// Converts `self` into a `RemovalCookie` with a `'static` lifetime with as few
    /// allocations as possible.
    pub fn into_owned(self) -> RemovalCookie<'static> {
        let to_owned = |s: Cow<'c, str>| match s {
            Cow::Borrowed(s) => Cow::Owned(s.to_owned()),
            Cow::Owned(s) => Cow::Owned(s),
        };
        RemovalCookie {
            name: to_owned(self.name),
            domain: self.domain.map(to_owned),
            path: self.path.map(to_owned),
        }
    }
}

/// Methods to set fields in a [`RemovalCookie`].
impl<'c> RemovalCookie<'c> {
    /// Sets the name of this removal cookie, replacing the current name.
    /// It returns the modified removal cookie.
    ///
    /// # Example
    ///
    /// ```
    /// use biscotti::RemovalCookie;
    ///
    /// let mut c = RemovalCookie::new("name");
    /// assert_eq!(c.name(), "name");
    ///
    /// c = c.set_name("foo");
    /// assert_eq!(c.name(), "foo");
    /// ```
    pub fn set_name<N: Into<Cow<'c, str>>>(mut self, name: N) -> Self {
        self.name = name.into();
        self
    }

    /// Sets the path property of the removal cookie to `path`.
    /// It returns the modified removal cookie.
    ///
    /// # Example
    ///
    /// ```rust
    /// use biscotti::RemovalCookie;
    ///
    /// let mut c = RemovalCookie::new("name");
    /// assert_eq!(c.path(), None);
    ///
    /// c = c.set_path("/");
    /// assert_eq!(c.path(), Some("/"));
    /// ```
    pub fn set_path<P: Into<Cow<'c, str>>>(mut self, path: P) -> Self {
        self.path = Some(path.into());
        self
    }

    /// Unsets the `path` property of the removal cookie.
    /// It returns the modified removal cookie.
    ///
    /// # Example
    ///
    /// ```
    /// use biscotti::RemovalCookie;
    ///
    /// let mut c = RemovalCookie::new("name");
    /// assert_eq!(c.path(), None);
    ///
    /// c = c.set_path("/");
    /// assert_eq!(c.path(), Some("/"));
    ///
    /// c = c.unset_path();
    /// assert_eq!(c.path(), None);
    /// ```
    pub fn unset_path(mut self) -> Self {
        self.path = None;
        self
    }

    /// Sets the `domain` of `self` to `domain`.
    ///
    /// # Example
    ///
    /// ```
    /// use biscotti::RemovalCookie;
    ///
    /// let mut c = RemovalCookie::new("name");
    /// assert_eq!(c.domain(), None);
    ///
    /// c = c.set_domain("rust-lang.org");
    /// assert_eq!(c.domain(), Some("rust-lang.org"));
    /// ```
    pub fn set_domain<D: Into<Cow<'c, str>>>(mut self, domain: D) -> Self {
        self.domain = Some(domain.into());
        self
    }

    /// Unsets the `domain` of `self`.
    ///
    /// # Example
    ///
    /// ```
    /// use biscotti::RemovalCookie;
    ///
    /// let mut c = RemovalCookie::new("name");
    /// assert_eq!(c.domain(), None);
    ///
    /// c = c.set_domain("rust-lang.org");
    /// assert_eq!(c.domain(), Some("rust-lang.org"));
    ///
    /// c = c.unset_domain();
    /// assert_eq!(c.domain(), None);
    /// ```
    pub fn unset_domain(mut self) -> Self {
        self.domain = None;
        self
    }
}

impl<'c> From<RemovalCookie<'c>> for ResponseCookie<'c> {
    fn from(value: RemovalCookie<'c>) -> Self {
        let mut c = ResponseCookie::new(value.name, "");
        if let Some(domain) = value.domain {
            c = c.set_domain(domain);
        }
        if let Some(path) = value.path {
            c = c.set_path(path);
        }
        // A date in the past to ensure the client removes the cookie.
        c = c.set_expires(time::OffsetDateTime::from_unix_timestamp(0).unwrap());
        c
    }
}

impl<'c> From<ResponseCookieId<'c>> for RemovalCookie<'c> {
    fn from(value: ResponseCookieId<'c>) -> Self {
        RemovalCookie {
            name: value.name,
            domain: value.domain,
            path: value.path,
        }
    }
}