cookie_monster/cookie/expires/
mod.rs

1use std::fmt::{Debug, Write};
2
3use super::Cookie;
4
5#[cfg(feature = "time")]
6pub mod dep_time;
7
8#[cfg(feature = "chrono")]
9pub mod dep_chrono;
10
11#[cfg(feature = "jiff")]
12pub mod dep_jiff;
13
14const REMOVE: &str = "Thu, 01 Jan 1970 00:00:00 GMT";
15
16/// The Expires attribute.
17#[derive(Clone, Default)]
18pub enum Expires {
19    // So a user can still remove a cookie without needing any of the datetime features.
20    Remove,
21    // No expiry time.
22    #[default]
23    Session,
24    Exp(ExpVal),
25}
26
27#[derive(Clone, Default)]
28pub struct ExpVal {
29    #[cfg(feature = "time")]
30    time: Option<time::OffsetDateTime>,
31    #[cfg(feature = "chrono")]
32    chrono: Option<chrono::DateTime<chrono::Utc>>,
33    #[cfg(feature = "jiff")]
34    jiff: Option<jiff::Zoned>,
35}
36
37impl Expires {
38    /// If one of the `time`, `chrono` or `jiff` features are enabled, the Expires tag is set to the
39    /// current time minus one year. If none of the those features are enabled, the Expires
40    /// attribute is set to 1 Jan 1970 00:00.
41    pub fn remove() -> Self {
42        #![allow(unreachable_code)]
43
44        #[cfg(feature = "jiff")]
45        return Self::remove_jiff();
46
47        #[cfg(feature = "chrono")]
48        return Self::remove_chrono();
49
50        #[cfg(feature = "time")]
51        return Self::remove_time();
52
53        Self::Remove
54    }
55}
56
57impl Cookie {
58    /// If the Expires attribute is not set, the expiration of the cookie is tied to the session
59    /// with the user-agent.
60    pub fn expires_is_set(&self) -> bool {
61        !matches!(self.expires, Expires::Session)
62    }
63
64    pub(crate) fn serialize_expire(&self, buf: &mut String) -> crate::Result<()> {
65        // Only one can be set at all times, except while parsing but then the first match is used.
66        match &self.expires {
67            #[cfg(feature = "time")]
68            Expires::Exp(ExpVal { time: Some(t), .. }) => dep_time::ser_expires(t, buf),
69            #[cfg(feature = "chrono")]
70            Expires::Exp(ExpVal {
71                chrono: Some(c), ..
72            }) => dep_chrono::ser_expires(c, buf),
73            #[cfg(feature = "jiff")]
74            Expires::Exp(ExpVal { jiff: Some(j), .. }) => dep_jiff::ser_expires(j, buf),
75            Expires::Remove => {
76                let _ = write!(buf, "; Expires={REMOVE}");
77                Ok(())
78            }
79
80            _ => Ok(()),
81        }
82    }
83}
84
85impl PartialEq for Expires {
86    fn eq(&self, other: &Self) -> bool {
87        match (self, other) {
88            (Expires::Remove, Expires::Remove) => true,
89            (Expires::Session, Expires::Session) => true,
90            (Expires::Exp(_s), Expires::Exp(_o)) => {
91                // When serializing, all the time, chrono and jiff fields are filled. If one
92                // matches it's enough to be equal.
93                #[cfg(feature = "time")]
94                if _s.time == _o.time {
95                    return true;
96                }
97
98                #[cfg(feature = "chrono")]
99                if _s.chrono == _o.chrono {
100                    return true;
101                }
102                #[cfg(feature = "jiff")]
103                if _s.jiff == _o.jiff {
104                    return true;
105                }
106
107                false
108            }
109            _ => false,
110        }
111    }
112}
113
114impl Debug for Expires {
115    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116        match self {
117            Self::Remove => write!(f, "{REMOVE}"),
118            Self::Session => write!(f, "Session"),
119            Self::Exp(_exp) => {
120                let mut debug = f.debug_struct("Exp");
121
122                #[cfg(feature = "time")]
123                let debug = debug.field("expires_time", &_exp.time);
124
125                #[cfg(feature = "chrono")]
126                let debug = debug.field("expires_chrono", &_exp.chrono);
127
128                #[cfg(feature = "jiff")]
129                let debug = debug.field("expires_jiff", &_exp.jiff);
130
131                debug.finish()
132            }
133        }
134    }
135}