cookie_monster/cookie/expires/
mod.rs1use 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#[derive(Clone, Default)]
18pub enum Expires {
19 Remove,
21 #[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 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 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 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 #[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}