1use std::borrow::Cow;
2
3use crate::Cookie;
4
5#[derive(Debug, Clone)]
6pub enum CookieChange<'a> {
7 Create(Cookie<'a>),
8 Remove(Cow<'a, str>),
9}
10
11impl<'a> CookieChange<'a> {
12 pub fn create(cookie: Cookie<'a>) -> Self {
13 Self::Create(cookie)
14 }
15
16 pub fn remove(name: Cow<'a, str>) -> Self {
17 Self::Remove(name)
18 }
19
20 pub fn cookie(&self) -> Option<&Cookie<'a>> {
21 match self {
22 Self::Create(cookie) => Some(cookie),
23 Self::Remove(_) => None,
24 }
25 }
26
27 pub fn name(&self) -> &str {
28 match self {
29 Self::Create(cookie) => cookie.name(),
30 Self::Remove(name) => name.as_ref(),
31 }
32 }
33
34 pub fn is_create(&self) -> bool {
35 match self {
36 Self::Create(_) => true,
37 Self::Remove(_) => false,
38 }
39 }
40
41 pub fn is_remove(&self) -> bool {
42 !self.is_create()
43 }
44
45 pub fn into_cookie(self) -> Option<Cookie<'a>> {
46 match self {
47 Self::Create(cookie) => Some(cookie),
48 Self::Remove(_) => None,
49 }
50 }
51
52 pub fn as_header_value(&self) -> String {
53 match self {
54 Self::Create(cookie) => cookie.to_string(),
55 Self::Remove(name) => format!("{name}=removed; Max-Age=0"),
56 }
57 }
58}
59
60impl PartialEq for CookieChange<'_> {
61 fn eq(&self, other: &Self) -> bool {
62 self.name() == other.name()
63 }
64}
65
66impl Eq for CookieChange<'_> {}
67
68impl PartialOrd for CookieChange<'_> {
69 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
70 Some(self.cmp(other))
71 }
72}
73
74impl Ord for CookieChange<'_> {
75 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
76 self.name().cmp(other.name())
77 }
78}