jmap_client/email_submission/
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::{Address, EmailSubmission, Envelope, SetArguments, UndoStatus};
13use crate::{core::set::SetObject, email::Email, Get, Set};
14use ahash::AHashMap;
15
16impl EmailSubmission<Set> {
17    pub fn identity_id(&mut self, identity_id: impl Into<String>) -> &mut Self {
18        self.identity_id = Some(identity_id.into());
19        self
20    }
21
22    pub fn email_id(&mut self, email_id: impl Into<String>) -> &mut Self {
23        self.email_id = Some(email_id.into());
24        self
25    }
26
27    pub fn envelope<S, T, U>(&mut self, mail_from: S, rcpt_to: T) -> &mut Self
28    where
29        S: Into<Address>,
30        T: IntoIterator<Item = U>,
31        U: Into<Address>,
32    {
33        self.envelope = Some(Envelope::new(mail_from, rcpt_to));
34        self
35    }
36
37    pub fn undo_status(&mut self, undo_status: UndoStatus) -> &mut Self {
38        self.undo_status = Some(undo_status);
39        self
40    }
41}
42
43impl SetObject for EmailSubmission<Set> {
44    type SetArguments = SetArguments;
45
46    fn new(_create_id: Option<usize>) -> Self {
47        EmailSubmission {
48            _create_id,
49            _state: Default::default(),
50            id: None,
51            identity_id: None,
52            email_id: None,
53            thread_id: None,
54            envelope: None,
55            send_at: None,
56            undo_status: None,
57            delivery_status: None,
58            dsn_blob_ids: None,
59            mdn_blob_ids: None,
60        }
61    }
62
63    fn create_id(&self) -> Option<String> {
64        self._create_id.map(|id| format!("c{}", id))
65    }
66}
67
68impl SetObject for EmailSubmission<Get> {
69    type SetArguments = SetArguments;
70
71    fn new(_create_id: Option<usize>) -> Self {
72        unimplemented!()
73    }
74
75    fn create_id(&self) -> Option<String> {
76        None
77    }
78}
79
80impl Envelope {
81    pub fn new<S, T, U>(mail_from: S, rcpt_to: T) -> Envelope
82    where
83        S: Into<Address>,
84        T: IntoIterator<Item = U>,
85        U: Into<Address>,
86    {
87        Envelope {
88            mail_from: mail_from.into(),
89            rcpt_to: rcpt_to.into_iter().map(|s| s.into()).collect(),
90        }
91    }
92}
93
94impl Address<Set> {
95    pub fn new(email: impl Into<String>) -> Address<Set> {
96        Address {
97            _state: Default::default(),
98            email: email.into(),
99            parameters: None,
100        }
101    }
102
103    pub fn parameter(
104        mut self,
105        parameter: impl Into<String>,
106        value: Option<impl Into<String>>,
107    ) -> Self {
108        self.parameters
109            .get_or_insert_with(AHashMap::new)
110            .insert(parameter.into(), value.map(|s| s.into()));
111        self
112    }
113}
114
115impl From<String> for Address {
116    fn from(email: String) -> Self {
117        Address {
118            _state: Default::default(),
119            email,
120            parameters: None,
121        }
122    }
123}
124
125impl From<&str> for Address {
126    fn from(email: &str) -> Self {
127        Address {
128            _state: Default::default(),
129            email: email.to_string(),
130            parameters: None,
131        }
132    }
133}
134
135impl From<Address<Set>> for Address<Get> {
136    fn from(addr: Address<Set>) -> Self {
137        Address {
138            _state: Default::default(),
139            email: addr.email,
140            parameters: addr.parameters,
141        }
142    }
143}
144
145impl From<Address<Get>> for Address<Set> {
146    fn from(addr: Address<Get>) -> Self {
147        Address {
148            _state: Default::default(),
149            email: addr.email,
150            parameters: addr.parameters,
151        }
152    }
153}
154
155impl SetArguments {
156    pub fn on_success_update_email(&mut self, id: impl Into<String>) -> &mut Email<Set> {
157        self.on_success_update_email_(format!("#{}", id.into()))
158    }
159
160    pub fn on_success_update_email_id(&mut self, id: impl Into<String>) -> &mut Email<Set> {
161        self.on_success_update_email_(id)
162    }
163
164    fn on_success_update_email_(&mut self, id: impl Into<String>) -> &mut Email<Set> {
165        let id = id.into();
166        self.on_success_update_email
167            .get_or_insert_with(AHashMap::new)
168            .insert(id.clone(), Email::new(None));
169        self.on_success_update_email
170            .as_mut()
171            .unwrap()
172            .get_mut(&id)
173            .unwrap()
174    }
175
176    pub fn on_success_destroy_email(&mut self, id: impl Into<String>) -> &mut Self {
177        self.on_success_destroy_email
178            .get_or_insert_with(Vec::new)
179            .push(format!("#{}", id.into()));
180        self
181    }
182
183    pub fn on_success_destroy_email_id(&mut self, id: impl Into<String>) -> &mut Self {
184        self.on_success_destroy_email
185            .get_or_insert_with(Vec::new)
186            .push(id.into());
187        self
188    }
189}