Skip to main content

rusted_mail/api/
requests.rs

1//! Typed IMAP fetch, search, message-set, and JMAP query options.
2
3use super::Serialize;
4
5/// Options for fetching IMAP message summaries.
6#[derive(Debug, Clone, Serialize)]
7#[serde(rename_all = "camelCase")]
8#[must_use]
9pub struct FetchOptions {
10    mailbox: String,
11    uid_set: String,
12    limit: usize,
13    include_preview: bool,
14}
15
16impl FetchOptions {
17    /// Fetches messages from `INBOX` for the supplied IMAP UID set.
18    pub fn new(uid_set: impl Into<String>) -> Self {
19        Self {
20            mailbox: "INBOX".to_string(),
21            uid_set: uid_set.into(),
22            limit: 100,
23            include_preview: true,
24        }
25    }
26
27    /// Selects the mailbox to fetch.
28    pub fn mailbox(mut self, mailbox: impl Into<String>) -> Self {
29        self.mailbox = mailbox.into();
30        self
31    }
32
33    /// Limits the number of returned summaries.
34    pub fn limit(mut self, limit: usize) -> Self {
35        self.limit = limit;
36        self
37    }
38
39    /// Controls generation of text previews.
40    pub fn include_preview(mut self, include: bool) -> Self {
41        self.include_preview = include;
42        self
43    }
44}
45
46/// Options for fetching a parsed message body.
47#[derive(Debug, Clone, Serialize)]
48#[serde(rename_all = "camelCase")]
49#[must_use]
50pub struct BodyOptions {
51    mailbox: String,
52    mark_seen: bool,
53    include_raw: bool,
54}
55
56impl Default for BodyOptions {
57    fn default() -> Self {
58        Self {
59            mailbox: "INBOX".to_string(),
60            mark_seen: false,
61            include_raw: false,
62        }
63    }
64}
65
66impl BodyOptions {
67    /// Selects the source mailbox.
68    pub fn mailbox(mut self, mailbox: impl Into<String>) -> Self {
69        self.mailbox = mailbox.into();
70        self
71    }
72
73    /// Marks the message as seen while fetching it.
74    pub fn mark_seen(mut self, mark_seen: bool) -> Self {
75        self.mark_seen = mark_seen;
76        self
77    }
78
79    /// Includes the original RFC 5322 source in the response.
80    pub fn include_raw(mut self, include_raw: bool) -> Self {
81        self.include_raw = include_raw;
82        self
83    }
84}
85
86/// Builder-style IMAP search criteria.
87#[derive(Debug, Clone, Serialize)]
88#[serde(rename_all = "camelCase")]
89#[must_use]
90pub struct SearchOptions {
91    mailbox: String,
92    query: String,
93    #[serde(skip_serializing_if = "Option::is_none")]
94    from: Option<String>,
95    #[serde(skip_serializing_if = "Option::is_none")]
96    to: Option<String>,
97    #[serde(skip_serializing_if = "Option::is_none")]
98    subject: Option<String>,
99    #[serde(skip_serializing_if = "Option::is_none")]
100    text: Option<String>,
101    #[serde(skip_serializing_if = "Option::is_none")]
102    seen: Option<bool>,
103}
104
105impl Default for SearchOptions {
106    fn default() -> Self {
107        Self {
108            mailbox: "INBOX".to_string(),
109            query: String::new(),
110            from: None,
111            to: None,
112            subject: None,
113            text: None,
114            seen: None,
115        }
116    }
117}
118
119impl SearchOptions {
120    /// Creates empty search criteria for `INBOX`.
121    pub fn new() -> Self {
122        Self::default()
123    }
124
125    /// Selects the mailbox to search.
126    pub fn mailbox(mut self, mailbox: impl Into<String>) -> Self {
127        self.mailbox = mailbox.into();
128        self
129    }
130
131    /// Sets a raw IMAP search expression.
132    pub fn query(mut self, query: impl Into<String>) -> Self {
133        self.query = query.into();
134        self
135    }
136
137    /// Matches the `From` header.
138    pub fn from(mut self, value: impl Into<String>) -> Self {
139        self.from = Some(value.into());
140        self
141    }
142
143    /// Matches the `To` header.
144    pub fn to(mut self, value: impl Into<String>) -> Self {
145        self.to = Some(value.into());
146        self
147    }
148
149    /// Matches the subject.
150    pub fn subject(mut self, value: impl Into<String>) -> Self {
151        self.subject = Some(value.into());
152        self
153    }
154
155    /// Matches text anywhere in the message.
156    pub fn text(mut self, value: impl Into<String>) -> Self {
157        self.text = Some(value.into());
158        self
159    }
160
161    /// Filters by the IMAP `\\Seen` flag.
162    pub fn seen(mut self, seen: bool) -> Self {
163        self.seen = Some(seen);
164        self
165    }
166}
167
168/// A set of IMAP message UIDs in one mailbox.
169#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
170#[serde(rename_all = "camelCase")]
171pub struct MessageSet {
172    mailbox: String,
173    uid_set: String,
174}
175
176impl MessageSet {
177    /// Creates a message set from a mailbox and IMAP UID-set expression.
178    #[must_use]
179    pub fn new(mailbox: impl Into<String>, uid_set: impl Into<String>) -> Self {
180        Self {
181            mailbox: mailbox.into(),
182            uid_set: uid_set.into(),
183        }
184    }
185
186    /// Returns the source mailbox.
187    #[must_use]
188    pub fn mailbox(&self) -> &str {
189        &self.mailbox
190    }
191
192    /// Returns the IMAP UID-set expression.
193    #[must_use]
194    pub fn uids(&self) -> &str {
195        &self.uid_set
196    }
197}
198
199/// Filters used when querying JMAP email identifiers.
200#[derive(Debug, Clone, Default, Serialize)]
201#[serde(rename_all = "camelCase")]
202#[must_use]
203pub struct JmapEmailQuery {
204    #[serde(skip_serializing_if = "Option::is_none")]
205    in_mailbox: Option<String>,
206    #[serde(skip_serializing_if = "Option::is_none")]
207    text: Option<String>,
208    #[serde(skip_serializing_if = "Option::is_none")]
209    from: Option<String>,
210    #[serde(skip_serializing_if = "Option::is_none")]
211    to: Option<String>,
212    #[serde(skip_serializing_if = "Option::is_none")]
213    subject: Option<String>,
214    #[serde(skip_serializing_if = "Option::is_none")]
215    has_attachment: Option<bool>,
216}
217
218impl JmapEmailQuery {
219    /// Creates an unfiltered email query.
220    pub fn new() -> Self {
221        Self::default()
222    }
223
224    /// Restricts results to one mailbox identifier.
225    pub fn in_mailbox(mut self, id: impl Into<String>) -> Self {
226        self.in_mailbox = Some(id.into());
227        self
228    }
229
230    /// Matches text across supported message properties.
231    pub fn text(mut self, value: impl Into<String>) -> Self {
232        self.text = Some(value.into());
233        self
234    }
235
236    /// Matches a sender address or name.
237    pub fn from(mut self, value: impl Into<String>) -> Self {
238        self.from = Some(value.into());
239        self
240    }
241
242    /// Matches a recipient address or name.
243    pub fn to(mut self, value: impl Into<String>) -> Self {
244        self.to = Some(value.into());
245        self
246    }
247
248    /// Matches the email subject.
249    pub fn subject(mut self, value: impl Into<String>) -> Self {
250        self.subject = Some(value.into());
251        self
252    }
253
254    /// Filters by attachment presence.
255    pub fn has_attachment(mut self, value: bool) -> Self {
256        self.has_attachment = Some(value);
257        self
258    }
259}