jmap_client/email/
get.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12use crate::{core::get::GetObject, Get, Set};
13
14use super::{
15    Email, EmailAddress, EmailAddressGroup, EmailBodyPart, EmailBodyValue, EmailHeader,
16    GetArguments, Header, HeaderValue,
17};
18
19impl Email<Get> {
20    pub fn id(&self) -> Option<&str> {
21        self.id.as_deref()
22    }
23
24    pub fn take_id(&mut self) -> String {
25        self.id.take().unwrap_or_default()
26    }
27
28    pub fn blob_id(&self) -> Option<&str> {
29        self.blob_id.as_deref()
30    }
31
32    pub fn take_blob_id(&mut self) -> String {
33        self.blob_id.take().unwrap_or_default()
34    }
35
36    pub fn thread_id(&self) -> Option<&str> {
37        self.thread_id.as_deref()
38    }
39
40    pub fn take_thread_id(&mut self) -> Option<String> {
41        self.thread_id.take()
42    }
43
44    pub fn mailbox_ids(&self) -> Vec<&str> {
45        self.mailbox_ids
46            .as_ref()
47            .map(|m| {
48                m.iter()
49                    .filter(|(_, v)| **v)
50                    .map(|(k, _)| k.as_str())
51                    .collect()
52            })
53            .unwrap_or_default()
54    }
55
56    pub fn keywords(&self) -> Vec<&str> {
57        self.keywords
58            .as_ref()
59            .map(|k| {
60                k.iter()
61                    .filter(|(_, v)| **v)
62                    .map(|(k, _)| k.as_str())
63                    .collect()
64            })
65            .unwrap_or_default()
66    }
67
68    pub fn size(&self) -> usize {
69        self.size.unwrap_or(0)
70    }
71
72    pub fn received_at(&self) -> Option<i64> {
73        self.received_at.as_ref().map(|r| r.timestamp())
74    }
75
76    pub fn message_id(&self) -> Option<&[String]> {
77        self.message_id.as_deref()
78    }
79
80    pub fn in_reply_to(&self) -> Option<&[String]> {
81        self.in_reply_to.as_deref()
82    }
83
84    pub fn references(&self) -> Option<&[String]> {
85        self.references.as_deref()
86    }
87
88    pub fn sender(&self) -> Option<&[EmailAddress]> {
89        self.sender.as_deref()
90    }
91
92    pub fn take_sender(&mut self) -> Option<Vec<EmailAddress>> {
93        self.sender.take()
94    }
95
96    pub fn from(&self) -> Option<&[EmailAddress]> {
97        self.from.as_deref()
98    }
99
100    pub fn take_from(&mut self) -> Option<Vec<EmailAddress>> {
101        self.from.take()
102    }
103
104    pub fn reply_to(&self) -> Option<&[EmailAddress]> {
105        self.reply_to.as_deref()
106    }
107
108    pub fn take_reply_to(&mut self) -> Option<Vec<EmailAddress>> {
109        self.reply_to.take()
110    }
111
112    pub fn to(&self) -> Option<&[EmailAddress]> {
113        self.to.as_deref()
114    }
115
116    pub fn take_to(&mut self) -> Option<Vec<EmailAddress>> {
117        self.to.take()
118    }
119
120    pub fn cc(&self) -> Option<&[EmailAddress]> {
121        self.cc.as_deref()
122    }
123
124    pub fn take_cc(&mut self) -> Option<Vec<EmailAddress>> {
125        self.cc.take()
126    }
127
128    pub fn bcc(&self) -> Option<&[EmailAddress]> {
129        self.bcc.as_deref()
130    }
131
132    pub fn take_bcc(&mut self) -> Option<Vec<EmailAddress>> {
133        self.bcc.take()
134    }
135
136    pub fn subject(&self) -> Option<&str> {
137        self.subject.as_deref()
138    }
139
140    pub fn take_subject(&mut self) -> Option<String> {
141        self.subject.take()
142    }
143
144    pub fn sent_at(&self) -> Option<i64> {
145        self.sent_at.as_ref().map(|v| v.timestamp())
146    }
147
148    pub fn body_structure(&self) -> Option<&EmailBodyPart> {
149        self.body_structure.as_deref()
150    }
151
152    pub fn body_value(&self, id: &str) -> Option<&EmailBodyValue> {
153        self.body_values.as_ref().and_then(|v| v.get(id))
154    }
155
156    pub fn text_body(&self) -> Option<&[EmailBodyPart]> {
157        self.text_body.as_deref()
158    }
159
160    pub fn html_body(&self) -> Option<&[EmailBodyPart]> {
161        self.html_body.as_deref()
162    }
163
164    pub fn attachments(&self) -> Option<&[EmailBodyPart]> {
165        self.attachments.as_deref()
166    }
167
168    pub fn has_attachment(&self) -> bool {
169        *self.has_attachment.as_ref().unwrap_or(&false)
170    }
171
172    pub fn header(&self, id: &Header) -> Option<&HeaderValue> {
173        self.headers.get(id).and_then(|v| v.as_ref())
174    }
175
176    pub fn has_header(&self, id: &Header) -> bool {
177        self.headers.contains_key(id)
178    }
179
180    pub fn preview(&self) -> Option<&str> {
181        self.preview.as_deref()
182    }
183
184    pub fn take_preview(&mut self) -> Option<String> {
185        self.preview.take()
186    }
187
188    #[cfg(feature = "debug")]
189    pub fn into_test(self) -> super::TestEmail {
190        self.into()
191    }
192}
193
194impl EmailBodyPart<Get> {
195    pub fn part_id(&self) -> Option<&str> {
196        self.part_id.as_deref()
197    }
198
199    pub fn blob_id(&self) -> Option<&str> {
200        self.blob_id.as_deref()
201    }
202
203    pub fn size(&self) -> usize {
204        *self.size.as_ref().unwrap_or(&0)
205    }
206
207    pub fn headers(&self) -> Option<&[EmailHeader]> {
208        self.headers.as_deref()
209    }
210
211    pub fn header(&self, id: &Header) -> Option<&HeaderValue> {
212        self.header.as_ref().and_then(|v| v.get(id))
213    }
214
215    pub fn name(&self) -> Option<&str> {
216        self.name.as_deref()
217    }
218
219    pub fn charset(&self) -> Option<&str> {
220        self.charset.as_deref()
221    }
222
223    pub fn content_type(&self) -> Option<&str> {
224        self.type_.as_deref()
225    }
226
227    pub fn content_disposition(&self) -> Option<&str> {
228        self.disposition.as_deref()
229    }
230
231    pub fn content_id(&self) -> Option<&str> {
232        self.cid.as_deref()
233    }
234
235    pub fn content_language(&self) -> Option<&[String]> {
236        self.language.as_deref()
237    }
238
239    pub fn content_location(&self) -> Option<&str> {
240        self.location.as_deref()
241    }
242
243    pub fn sub_parts(&self) -> Option<&[EmailBodyPart]> {
244        self.sub_parts.as_deref()
245    }
246}
247
248impl EmailBodyValue<Get> {
249    pub fn value(&self) -> &str {
250        self.value.as_str()
251    }
252
253    pub fn is_encoding_problem(&self) -> bool {
254        self.is_encoding_problem.unwrap_or(false)
255    }
256
257    pub fn is_truncated(&self) -> bool {
258        self.is_truncated.unwrap_or(false)
259    }
260}
261
262impl EmailAddress<Get> {
263    pub fn name(&self) -> Option<&str> {
264        self.name.as_deref()
265    }
266
267    pub fn email(&self) -> &str {
268        self.email.as_str()
269    }
270
271    pub fn unwrap(self) -> (String, Option<String>) {
272        (self.email, self.name)
273    }
274}
275
276impl EmailAddressGroup<Get> {
277    pub fn name(&self) -> Option<&str> {
278        self.name.as_deref()
279    }
280
281    pub fn addresses(&self) -> &[EmailAddress] {
282        self.addresses.as_ref()
283    }
284}
285
286impl EmailHeader<Get> {
287    pub fn name(&self) -> &str {
288        self.name.as_str()
289    }
290
291    pub fn value(&self) -> &str {
292        self.value.as_str()
293    }
294}
295
296impl GetObject for Email<Set> {
297    type GetArguments = GetArguments;
298}
299
300impl GetObject for Email<Get> {
301    type GetArguments = GetArguments;
302}