teo_runtime/cookies/
cookie.rs1use std::fmt::{Debug, Display, Formatter};
2use std::sync::{Arc, Mutex};
3use cookie::{Cookie as Inner, Expiration, SameSite};
4use cookie::time::Duration;
5use teo_result::Result;
6use crate::cookies::Cookies;
7
8#[repr(transparent)]
9#[derive(Clone)]
10pub struct Cookie {
11 pub inner: Arc<Mutex<Inner<'static>>>
12}
13
14impl Cookie {
15
16 pub fn new<T, U>(name: T, value: U) -> Self where String: From<T>, String: From<U> {
17 Self {
18 inner: Arc::new(Mutex::new(Inner::new(String::from(name), String::from(value))))
19 }
20 }
21
22 pub fn parse(string: impl Into<String>) -> Result<Self> {
23 Ok(Self {
24 inner: Arc::new(Mutex::new(Inner::parse(string.into())?))
25 })
26 }
27
28 pub fn parse_encoded(string: impl Into<String>) -> Result<Self> {
29 Ok(Self {
30 inner: Arc::new(Mutex::new(Inner::parse_encoded(string.into())?))
31 })
32 }
33
34 pub fn name(&self) -> String {
35 self.inner.lock().unwrap().name().to_string()
36 }
37
38 pub fn set_name(&self, name: impl Into<String>) {
39 self.inner.lock().unwrap().set_name(name.into());
40 }
41
42 pub fn value(&self) -> String {
43 self.inner.lock().unwrap().value().to_string()
44 }
45
46 pub fn value_trimmed(&self) -> String {
47 self.inner.lock().unwrap().value_trimmed().to_string()
48 }
49
50 pub fn set_value(&self, value: impl Into<String>) {
51 self.inner.lock().unwrap().set_value(value.into());
52 }
53
54 pub fn http_only(&self) -> Option<bool> {
55 self.inner.lock().unwrap().http_only()
56 }
57
58 pub fn set_http_only(&self, http_only: Option<bool>) {
59 self.inner.lock().unwrap().set_http_only(http_only);
60 }
61
62 pub fn secure(&self) -> Option<bool> {
63 self.inner.lock().unwrap().secure()
64 }
65
66 pub fn set_secure(&self, secure: Option<bool>) {
67 self.inner.lock().unwrap().set_secure(secure);
68 }
69
70 pub fn same_site(&self) -> Option<SameSite> {
71 self.inner.lock().unwrap().same_site()
72 }
73
74 pub fn set_same_site(&self, same_site: Option<SameSite>) {
75 self.inner.lock().unwrap().set_same_site(same_site);
76 }
77
78 pub fn partitioned(&self) -> Option<bool> {
79 self.inner.lock().unwrap().partitioned()
80 }
81
82 pub fn set_partitioned(&self, partitioned: Option<bool>) {
83 self.inner.lock().unwrap().set_partitioned(partitioned);
84 }
85
86 pub fn max_age(&self) -> Option<Duration> {
87 self.inner.lock().unwrap().max_age()
88 }
89
90 pub fn set_max_age(&self, max_age: Option<Duration>) {
91 self.inner.lock().unwrap().set_max_age(max_age);
92 }
93
94 pub fn path(&self) -> Option<String> {
95 self.inner.lock().unwrap().path().map(|s| s.to_string())
96 }
97
98 pub fn set_path<T>(&self, path: Option<T>) where String: From<T> {
99 let mut guard = self.inner.lock().unwrap();
100 let inner = guard.as_mut();
101 if let Some(value) = path {
102 inner.set_path(String::from(value))
103 } else {
104 inner.unset_path()
105 }
106 }
107
108 pub fn domain(&self) -> Option<String> {
109 self.inner.lock().unwrap().domain().map(|s| s.to_string())
110 }
111
112 pub fn set_domain<T>(&self, domain: Option<T>) where String: From<T> {
113 let mut guard = self.inner.lock().unwrap();
114 let inner = guard.as_mut();
115 if let Some(value) = domain {
116 inner.set_domain(String::from(value))
117 } else {
118 inner.unset_domain()
119 }
120 }
121
122 pub fn expires(&self) -> Option<Expiration> {
123 self.inner.lock().unwrap().expires()
124 }
125
126 pub fn set_expires(&self, expires: Option<Expiration>) {
127 let mut guard = self.inner.lock().unwrap();
128 let inner = guard.as_mut();
129 if let Some(value) = expires {
130 inner.set_expires(value)
131 } else {
132 inner.unset_expires()
133 }
134 }
135
136 pub fn make_permanent(&self) {
137 self.inner.lock().unwrap().make_permanent();
138 }
139
140 pub fn make_removal(&self) {
141 self.inner.lock().unwrap().make_removal();
142 }
143
144 pub fn encoded(&self) -> String {
145 self.inner.lock().unwrap().encoded().to_string()
146 }
147}
148
149pub struct CookiesIter {
150 pub inner: Vec<Cookie>,
151 pub index: usize,
152}
153
154impl Iterator for CookiesIter {
155 type Item = Cookie;
156
157 fn next(&mut self) -> Option<Self::Item> {
158 if self.index < self.inner.len() {
159 let cookie = self.inner[self.index].clone();
160 self.index += 1;
161 Some(cookie)
162 } else {
163 None
164 }
165 }
166}
167
168impl IntoIterator for Cookies {
169 type Item = Cookie;
170 type IntoIter = CookiesIter;
171
172 fn into_iter(self) -> Self::IntoIter {
173 CookiesIter {
174 inner: self.inner.lock().unwrap().list.clone(),
175 index: 0,
176 }
177 }
178}
179
180impl IntoIterator for &Cookies {
181 type Item = Cookie;
182 type IntoIter = CookiesIter;
183
184 fn into_iter(self) -> Self::IntoIter {
185 CookiesIter {
186 inner: self.inner.lock().unwrap().list.clone(),
187 index: 0,
188 }
189 }
190}
191
192
193impl Debug for Cookie {
194 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
195 let guard = self.inner.lock().unwrap();
196 let inner = guard.as_ref();
197 Debug::fmt(&inner, f)
198 }
199}
200
201impl Display for Cookie {
202 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
203 let guard = self.inner.lock().unwrap();
204 let inner = guard.as_ref();
205 Display::fmt(&inner, f)
206 }
207}
208
209impl PartialEq for Cookie {
210 fn eq(&self, other: &Self) -> bool {
211 let guard_self = self.inner.lock().unwrap();
212 let inner = guard_self.as_ref();
213 let guard_other = other.inner.lock().unwrap();
214 let other = guard_other.as_ref();
215 inner == other
216 }
217}