cookie/
delta.rs

1use std::ops::{Deref, DerefMut};
2use std::hash::{Hash, Hasher};
3use std::borrow::Borrow;
4
5use crate::Cookie;
6
7/// A `DeltaCookie` is a helper structure used in a cookie jar. It wraps a
8/// `Cookie` so that it can be hashed and compared purely by name. It further
9/// records whether the wrapped cookie is a "removal" cookie, that is, a cookie
10/// that when sent to the client removes the named cookie on the client's
11/// machine.
12#[derive(Clone, Debug)]
13pub(crate) struct DeltaCookie {
14    pub cookie: Cookie<'static>,
15    pub removed: bool,
16}
17
18impl DeltaCookie {
19    /// Create a new `DeltaCookie` that is being added to a jar.
20    #[inline]
21    pub fn added(cookie: Cookie<'static>) -> DeltaCookie {
22        DeltaCookie { cookie, removed: false, }
23    }
24
25    /// Create a new `DeltaCookie` that is being removed from a jar. The
26    /// `cookie` should be a "removal" cookie.
27    #[inline]
28    pub fn removed(cookie: Cookie<'static>) -> DeltaCookie {
29        DeltaCookie { cookie, removed: true, }
30    }
31}
32
33impl Deref for DeltaCookie {
34    type Target = Cookie<'static>;
35
36    fn deref(&self) -> &Cookie<'static> {
37        &self.cookie
38    }
39}
40
41impl DerefMut for DeltaCookie {
42    fn deref_mut(&mut self) -> &mut Cookie<'static> {
43        &mut self.cookie
44    }
45}
46
47impl PartialEq for DeltaCookie {
48    fn eq(&self, other: &DeltaCookie) -> bool {
49        self.name() == other.name()
50    }
51}
52
53impl Eq for DeltaCookie {}
54
55impl Hash for DeltaCookie {
56    fn hash<H: Hasher>(&self, state: &mut H) {
57        self.name().hash(state);
58    }
59}
60
61impl Borrow<str> for DeltaCookie {
62    fn borrow(&self) -> &str {
63        self.name()
64    }
65}