Skip to main content

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    fn set_id(&mut self, id: Option<String>) {
245        self.id = id;
246    }
247}
248
249impl SetObject for Email<Get> {
250    type SetArguments = ();
251
252    fn new(_create_id: Option<usize>) -> Email<Get> {
253        unimplemented!()
254    }
255
256    fn create_id(&self) -> Option<String> {
257        None
258    }
259}
260
261impl EmailBodyPart {
262    pub fn new() -> EmailBodyPart<Set> {
263        EmailBodyPart {
264            part_id: None,
265            blob_id: None,
266            size: None,
267            headers: None,
268            name: None,
269            type_: None,
270            charset: None,
271            disposition: None,
272            cid: None,
273            language: None,
274            location: None,
275            sub_parts: None,
276            header: None,
277            _state: Default::default(),
278        }
279    }
280}
281
282impl From<EmailBodyPart<Set>> for EmailBodyPart<Get> {
283    fn from(part: EmailBodyPart<Set>) -> Self {
284        EmailBodyPart {
285            part_id: part.part_id,
286            blob_id: part.blob_id,
287            size: part.size,
288            headers: part.headers,
289            name: part.name,
290            type_: part.type_,
291            charset: part.charset,
292            disposition: part.disposition,
293            cid: part.cid,
294            language: part.language,
295            location: part.location,
296            sub_parts: part.sub_parts,
297            header: part.header,
298            _state: Default::default(),
299        }
300    }
301}
302
303impl EmailBodyPart<Set> {
304    pub fn part_id(mut self, part_id: impl Into<String>) -> Self {
305        self.part_id = Some(part_id.into());
306        self
307    }
308
309    pub fn blob_id(mut self, blob_id: impl Into<String>) -> Self {
310        self.blob_id = Some(blob_id.into());
311        self
312    }
313
314    pub fn name(mut self, name: impl Into<String>) -> Self {
315        self.name = Some(name.into());
316        self
317    }
318
319    pub fn content_type(mut self, content_type: impl Into<String>) -> Self {
320        self.type_ = Some(content_type.into());
321        self
322    }
323
324    pub fn content_id(mut self, content_id: impl Into<String>) -> Self {
325        self.cid = Some(content_id.into());
326        self
327    }
328
329    pub fn content_language<T, U>(mut self, content_language: T) -> Self
330    where
331        T: IntoIterator<Item = U>,
332        U: Into<String>,
333    {
334        self.language = Some(content_language.into_iter().map(|v| v.into()).collect());
335        self
336    }
337
338    pub fn content_location(mut self, content_location: impl Into<String>) -> Self {
339        self.location = Some(content_location.into());
340        self
341    }
342
343    pub fn sub_part(mut self, sub_part: EmailBodyPart) -> Self {
344        self.sub_parts.get_or_insert_with(Vec::new).push(sub_part);
345        self
346    }
347}
348
349impl From<String> for EmailBodyValue {
350    fn from(value: String) -> Self {
351        EmailBodyValue {
352            value,
353            is_encoding_problem: None,
354            is_truncated: None,
355            _state: Default::default(),
356        }
357    }
358}
359
360impl From<&str> for EmailBodyValue {
361    fn from(value: &str) -> Self {
362        EmailBodyValue {
363            value: value.to_string(),
364            is_encoding_problem: None,
365            is_truncated: None,
366            _state: Default::default(),
367        }
368    }
369}
370
371impl EmailAddress {
372    pub fn new(email: String) -> EmailAddress<Set> {
373        EmailAddress {
374            _state: Default::default(),
375            name: None,
376            email,
377        }
378    }
379}
380
381impl EmailAddress<Set> {
382    pub fn name(mut self, name: String) -> Self {
383        self.name = Some(name);
384        self
385    }
386}
387
388impl From<String> for EmailAddress {
389    fn from(email: String) -> Self {
390        EmailAddress {
391            _state: Default::default(),
392            name: None,
393            email,
394        }
395    }
396}
397
398impl From<(String, String)> for EmailAddress {
399    fn from(parts: (String, String)) -> Self {
400        EmailAddress {
401            _state: Default::default(),
402            name: parts.0.into(),
403            email: parts.1,
404        }
405    }
406}
407
408impl From<&str> for EmailAddress {
409    fn from(email: &str) -> Self {
410        EmailAddress {
411            _state: Default::default(),
412            name: None,
413            email: email.to_string(),
414        }
415    }
416}
417
418impl From<(&str, &str)> for EmailAddress {
419    fn from(parts: (&str, &str)) -> Self {
420        EmailAddress {
421            _state: Default::default(),
422            name: parts.0.to_string().into(),
423            email: parts.1.to_string(),
424        }
425    }
426}
427
428impl EmailAddressGroup {
429    pub fn new() -> EmailAddressGroup<Set> {
430        EmailAddressGroup {
431            _state: Default::default(),
432            name: None,
433            addresses: Vec::new(),
434        }
435    }
436}
437
438impl EmailAddressGroup<Set> {
439    pub fn name(mut self, name: impl Into<String>) -> Self {
440        self.name = Some(name.into());
441        self
442    }
443
444    pub fn address(mut self, address: impl Into<EmailAddress>) -> Self {
445        self.addresses.push(address.into());
446        self
447    }
448}
449
450impl EmailHeader {
451    pub fn new(name: String, value: String) -> EmailHeader<Set> {
452        EmailHeader {
453            _state: Default::default(),
454            name,
455            value,
456        }
457    }
458}