jmap_client/email/
set.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 super::{
13    Email, EmailAddress, EmailAddressGroup, EmailBodyPart, EmailBodyValue, EmailHeader, Header,
14    HeaderValue,
15};
16use crate::{
17    core::{
18        request::ResultReference,
19        set::{from_timestamp, SetObject},
20    },
21    Get, Set,
22};
23use ahash::AHashMap;
24
25impl Email<Set> {
26    pub fn mailbox_ids<T, U>(&mut self, mailbox_ids: T) -> &mut Self
27    where
28        T: IntoIterator<Item = U>,
29        U: Into<String>,
30    {
31        self.mailbox_ids = Some(mailbox_ids.into_iter().map(|s| (s.into(), true)).collect());
32        self.mailbox_ids_ref = None;
33        self
34    }
35
36    pub fn mailbox_ids_ref(&mut self, reference: ResultReference) -> &mut Self {
37        self.mailbox_ids_ref = reference.into();
38        self.mailbox_ids = None;
39        self
40    }
41
42    pub fn mailbox_id(&mut self, mailbox_id: &str, set: bool) -> &mut Self {
43        self.mailbox_ids = None;
44        self.patch
45            .get_or_insert_with(AHashMap::new)
46            .insert(format!("mailboxIds/{}", mailbox_id), set);
47        self
48    }
49
50    pub fn keywords<T, U>(&mut self, keywords: T) -> &mut Self
51    where
52        T: IntoIterator<Item = U>,
53        U: Into<String>,
54    {
55        self.keywords = Some(keywords.into_iter().map(|s| (s.into(), true)).collect());
56        self
57    }
58
59    pub fn keyword(&mut self, keyword: &str, set: bool) -> &mut Self {
60        self.keywords = None;
61        self.patch
62            .get_or_insert_with(AHashMap::new)
63            .insert(format!("keywords/{}", keyword), set);
64        self
65    }
66
67    pub fn message_id<T, U>(&mut self, message_id: T) -> &mut Self
68    where
69        T: IntoIterator<Item = U>,
70        U: Into<String>,
71    {
72        self.message_id = Some(message_id.into_iter().map(|v| v.into()).collect());
73        self
74    }
75
76    pub fn in_reply_to<T, U>(&mut self, in_reply_to: T) -> &mut Self
77    where
78        T: IntoIterator<Item = U>,
79        U: Into<String>,
80    {
81        self.in_reply_to = Some(in_reply_to.into_iter().map(|v| v.into()).collect());
82        self
83    }
84
85    pub fn references<T, U>(&mut self, references: T) -> &mut Self
86    where
87        T: IntoIterator<Item = U>,
88        U: Into<String>,
89    {
90        self.references = Some(references.into_iter().map(|v| v.into()).collect());
91        self
92    }
93
94    pub fn sender<T, U>(&mut self, sender: T) -> &mut Self
95    where
96        T: IntoIterator<Item = U>,
97        U: Into<EmailAddress>,
98    {
99        self.sender = Some(sender.into_iter().map(|s| s.into()).collect());
100        self
101    }
102
103    pub fn from<T, U>(&mut self, from: T) -> &mut Self
104    where
105        T: IntoIterator<Item = U>,
106        U: Into<EmailAddress>,
107    {
108        self.from = Some(from.into_iter().map(|s| s.into()).collect());
109        self
110    }
111
112    pub fn to<T, U>(&mut self, to: T) -> &mut Self
113    where
114        T: IntoIterator<Item = U>,
115        U: Into<EmailAddress>,
116    {
117        self.to = Some(to.into_iter().map(|s| s.into()).collect());
118        self
119    }
120
121    pub fn cc<T, U>(&mut self, cc: T) -> &mut Self
122    where
123        T: IntoIterator<Item = U>,
124        U: Into<EmailAddress>,
125    {
126        self.cc = Some(cc.into_iter().map(|s| s.into()).collect());
127        self
128    }
129
130    pub fn bcc<T, U>(&mut self, bcc: T) -> &mut Self
131    where
132        T: IntoIterator<Item = U>,
133        U: Into<EmailAddress>,
134    {
135        self.bcc = Some(bcc.into_iter().map(|s| s.into()).collect());
136        self
137    }
138
139    pub fn reply_to<T, U>(&mut self, reply_to: T) -> &mut Self
140    where
141        T: IntoIterator<Item = U>,
142        U: Into<EmailAddress>,
143    {
144        self.reply_to = Some(reply_to.into_iter().map(|s| s.into()).collect());
145        self
146    }
147
148    pub fn subject(&mut self, subject: impl Into<String>) -> &mut Self {
149        self.subject = Some(subject.into());
150        self
151    }
152
153    pub fn sent_at(&mut self, sent_at: i64) -> &mut Self {
154        self.sent_at = Some(from_timestamp(sent_at));
155        self
156    }
157
158    pub fn body_structure(&mut self, body_structure: EmailBodyPart) -> &mut Self {
159        self.body_structure = Some(body_structure.into());
160        self
161    }
162
163    pub fn body_value(&mut self, id: String, body_value: impl Into<EmailBodyValue>) -> &mut Self {
164        self.body_values
165            .get_or_insert_with(AHashMap::new)
166            .insert(id, body_value.into());
167        self
168    }
169
170    pub fn text_body(&mut self, text_body: impl Into<EmailBodyPart<Get>>) -> &mut Self {
171        self.text_body
172            .get_or_insert_with(Vec::new)
173            .push(text_body.into());
174        self
175    }
176
177    pub fn html_body(&mut self, html_body: impl Into<EmailBodyPart<Get>>) -> &mut Self {
178        self.html_body
179            .get_or_insert_with(Vec::new)
180            .push(html_body.into());
181        self
182    }
183
184    pub fn attachment(&mut self, attachment: impl Into<EmailBodyPart<Get>>) -> &mut Self {
185        self.attachments
186            .get_or_insert_with(Vec::new)
187            .push(attachment.into());
188        self
189    }
190
191    pub fn header(&mut self, header: Header, value: impl Into<HeaderValue>) -> &mut Self {
192        self.headers.insert(header, Some(value.into()));
193        self
194    }
195
196    pub fn received_at(&mut self, received_at: i64) -> &mut Self {
197        self.received_at = Some(from_timestamp(received_at));
198        self
199    }
200}
201
202impl SetObject for Email<Set> {
203    type SetArguments = ();
204
205    fn new(_create_id: Option<usize>) -> Email<Set> {
206        Email {
207            _create_id,
208            _state: Default::default(),
209            id: Default::default(),
210            blob_id: Default::default(),
211            thread_id: Default::default(),
212            mailbox_ids: Default::default(),
213            mailbox_ids_ref: Default::default(),
214            keywords: Default::default(),
215            size: Default::default(),
216            received_at: Default::default(),
217            message_id: Default::default(),
218            in_reply_to: Default::default(),
219            references: Default::default(),
220            sender: Default::default(),
221            from: Default::default(),
222            to: Default::default(),
223            cc: Default::default(),
224            bcc: Default::default(),
225            reply_to: Default::default(),
226            subject: Default::default(),
227            sent_at: Default::default(),
228            body_structure: Default::default(),
229            body_values: Default::default(),
230            text_body: Default::default(),
231            html_body: Default::default(),
232            attachments: Default::default(),
233            has_attachment: Default::default(),
234            preview: Default::default(),
235            headers: Default::default(),
236            patch: Default::default(),
237        }
238    }
239
240    fn create_id(&self) -> Option<String> {
241        self._create_id.map(|id| format!("c{}", id))
242    }
243}
244
245impl SetObject for Email<Get> {
246    type SetArguments = ();
247
248    fn new(_create_id: Option<usize>) -> Email<Get> {
249        unimplemented!()
250    }
251
252    fn create_id(&self) -> Option<String> {
253        None
254    }
255}
256
257impl EmailBodyPart {
258    pub fn new() -> EmailBodyPart<Set> {
259        EmailBodyPart {
260            part_id: None,
261            blob_id: None,
262            size: None,
263            headers: None,
264            name: None,
265            type_: None,
266            charset: None,
267            disposition: None,
268            cid: None,
269            language: None,
270            location: None,
271            sub_parts: None,
272            header: None,
273            _state: Default::default(),
274        }
275    }
276}
277
278impl From<EmailBodyPart<Set>> for EmailBodyPart<Get> {
279    fn from(part: EmailBodyPart<Set>) -> Self {
280        EmailBodyPart {
281            part_id: part.part_id,
282            blob_id: part.blob_id,
283            size: part.size,
284            headers: part.headers,
285            name: part.name,
286            type_: part.type_,
287            charset: part.charset,
288            disposition: part.disposition,
289            cid: part.cid,
290            language: part.language,
291            location: part.location,
292            sub_parts: part.sub_parts,
293            header: part.header,
294            _state: Default::default(),
295        }
296    }
297}
298
299impl EmailBodyPart<Set> {
300    pub fn part_id(mut self, part_id: impl Into<String>) -> Self {
301        self.part_id = Some(part_id.into());
302        self
303    }
304
305    pub fn blob_id(mut self, blob_id: impl Into<String>) -> Self {
306        self.blob_id = Some(blob_id.into());
307        self
308    }
309
310    pub fn name(mut self, name: impl Into<String>) -> Self {
311        self.name = Some(name.into());
312        self
313    }
314
315    pub fn content_type(mut self, content_type: impl Into<String>) -> Self {
316        self.type_ = Some(content_type.into());
317        self
318    }
319
320    pub fn content_id(mut self, content_id: impl Into<String>) -> Self {
321        self.cid = Some(content_id.into());
322        self
323    }
324
325    pub fn content_language<T, U>(mut self, content_language: T) -> Self
326    where
327        T: IntoIterator<Item = U>,
328        U: Into<String>,
329    {
330        self.language = Some(content_language.into_iter().map(|v| v.into()).collect());
331        self
332    }
333
334    pub fn content_location(mut self, content_location: impl Into<String>) -> Self {
335        self.location = Some(content_location.into());
336        self
337    }
338
339    pub fn sub_part(mut self, sub_part: EmailBodyPart) -> Self {
340        self.sub_parts.get_or_insert_with(Vec::new).push(sub_part);
341        self
342    }
343}
344
345impl From<String> for EmailBodyValue {
346    fn from(value: String) -> Self {
347        EmailBodyValue {
348            value,
349            is_encoding_problem: None,
350            is_truncated: None,
351            _state: Default::default(),
352        }
353    }
354}
355
356impl From<&str> for EmailBodyValue {
357    fn from(value: &str) -> Self {
358        EmailBodyValue {
359            value: value.to_string(),
360            is_encoding_problem: None,
361            is_truncated: None,
362            _state: Default::default(),
363        }
364    }
365}
366
367impl EmailAddress {
368    pub fn new(email: String) -> EmailAddress<Set> {
369        EmailAddress {
370            _state: Default::default(),
371            name: None,
372            email,
373        }
374    }
375}
376
377impl EmailAddress<Set> {
378    pub fn name(mut self, name: String) -> Self {
379        self.name = Some(name);
380        self
381    }
382}
383
384impl From<String> for EmailAddress {
385    fn from(email: String) -> Self {
386        EmailAddress {
387            _state: Default::default(),
388            name: None,
389            email,
390        }
391    }
392}
393
394impl From<(String, String)> for EmailAddress {
395    fn from(parts: (String, String)) -> Self {
396        EmailAddress {
397            _state: Default::default(),
398            name: parts.0.into(),
399            email: parts.1,
400        }
401    }
402}
403
404impl From<&str> for EmailAddress {
405    fn from(email: &str) -> Self {
406        EmailAddress {
407            _state: Default::default(),
408            name: None,
409            email: email.to_string(),
410        }
411    }
412}
413
414impl From<(&str, &str)> for EmailAddress {
415    fn from(parts: (&str, &str)) -> Self {
416        EmailAddress {
417            _state: Default::default(),
418            name: parts.0.to_string().into(),
419            email: parts.1.to_string(),
420        }
421    }
422}
423
424impl EmailAddressGroup {
425    pub fn new() -> EmailAddressGroup<Set> {
426        EmailAddressGroup {
427            _state: Default::default(),
428            name: None,
429            addresses: Vec::new(),
430        }
431    }
432}
433
434impl EmailAddressGroup<Set> {
435    pub fn name(mut self, name: impl Into<String>) -> Self {
436        self.name = Some(name.into());
437        self
438    }
439
440    pub fn address(mut self, address: impl Into<EmailAddress>) -> Self {
441        self.addresses.push(address.into());
442        self
443    }
444}
445
446impl EmailHeader {
447    pub fn new(name: String, value: String) -> EmailHeader<Set> {
448        EmailHeader {
449            _state: Default::default(),
450            name,
451            value,
452        }
453    }
454}