mailrs-jmap 1.1.2

JMAP (RFC 8620 + RFC 8621) server-side dispatcher and method handlers — framework-agnostic, BYO mail store via the MailStore trait.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! In-memory [`MailStore`](crate::store::MailStore) implementation suitable
//! for tests, examples, and downstream-consumer test harnesses.
//!
//! **Intended use is testing.** The store keeps every value in process memory
//! and never persists across restarts; do not wire it into a real deployment.
//!
//! ## Quick start
//!
//! ```
//! use mailrs_jmap::fixtures::{InMemoryStore, EXAMPLE_USER, make_message};
//!
//! let store = InMemoryStore::new()
//!     .with_message(make_message(1, 10, EXAMPLE_USER));
//! ```
//!
//! ## What it gives you
//!
//! - Stateful in-memory storage with a builder API — `with_mailbox`,
//!   `with_message`, `with_message_raw`, `with_parsed_body`,
//!   `with_mailbox_counts`.
//! - Per-method error injection via `<method>_fails` setters so each error
//!   path in your handler-driving code can be isolated in a single test.
//! - Read-back helpers for assertions — `flags_for(mailbox_id, uid)`.
//! - Convenience constructors — `make_message`, `make_request`,
//!   `parsed_with_text`, `parsed_with_attachment`.
//!
//! Used internally by this crate's integration tests; the same module is
//! exposed to downstream consumers so they can drive their own handler /
//! dispatcher tests without re-implementing the store.

use std::collections::HashMap;
use std::sync::RwLock;

use async_trait::async_trait;
use serde_json::Value;

use crate::dispatch::JmapRequest;
use crate::store::{MailStore, StoreError};
use crate::types::{
    Attachment, FLAG_SEEN, Mailbox, MailboxCounts, Message, ParsedBody, SubmissionResult,
};

/// Convenience example user used by the constructors in this module. The
/// store does not assume any particular value — callers can use their own.
pub const EXAMPLE_USER: &str = "alice@example.com";

/// In-memory [`MailStore`] backed by `Vec`s under an `RwLock`.
///
/// Build via [`Self::new`] + the chainable `with_*` setters. See the module
/// docs for an example.
pub struct InMemoryStore {
    inner: RwLock<Inner>,
}

struct Inner {
    mailboxes: Vec<Mailbox>,
    messages: Vec<Message>,
    raw_bytes: HashMap<i64, Vec<u8>>,
    parsed_bodies: HashMap<Vec<u8>, ParsedBody>,
    mailbox_counts: HashMap<i64, MailboxCounts>,

    list_mailboxes_error: Option<String>,
    mailbox_status_error: Option<String>,
    list_messages_error: Option<String>,
    get_message_error: Option<String>,
    list_thread_messages_error: Option<String>,
    update_flags_error: Option<String>,
    add_flags_error: Option<String>,

    submission_result: SubmissionResult,
}

impl InMemoryStore {
    /// Construct an empty store.
    pub fn new() -> Self {
        Self {
            inner: RwLock::new(Inner {
                mailboxes: Vec::new(),
                messages: Vec::new(),
                raw_bytes: HashMap::new(),
                parsed_bodies: HashMap::new(),
                mailbox_counts: HashMap::new(),
                list_mailboxes_error: None,
                mailbox_status_error: None,
                list_messages_error: None,
                get_message_error: None,
                list_thread_messages_error: None,
                update_flags_error: None,
                add_flags_error: None,
                submission_result: SubmissionResult {
                    success: true,
                    message: None,
                },
            }),
        }
    }

    /// Append a mailbox with the given id and name.
    pub fn with_mailbox(self, id: i64, name: &str) -> Self {
        self.inner.write().unwrap().mailboxes.push(Mailbox {
            id,
            name: name.to_string(),
        });
        self
    }

    /// Append a pre-built [`Message`]. Use [`make_message`] for a sane default
    /// shape and mutate before passing in if you need overrides.
    pub fn with_message(self, msg: Message) -> Self {
        self.inner.write().unwrap().messages.push(msg);
        self
    }

    /// Map a message id to raw RFC 5322 bytes. [`MailStore::read_message_raw`]
    /// returns these bytes; without this setter that method returns `None`.
    pub fn with_message_raw(self, msg_id: i64, raw: Vec<u8>) -> Self {
        self.inner.write().unwrap().raw_bytes.insert(msg_id, raw);
        self
    }

    /// Map a specific raw byte sequence to a parsed body. The store's
    /// [`MailStore::parse_message`] returns the override when called with
    /// matching bytes, and `ParsedBody::default()` otherwise.
    pub fn with_parsed_body(self, raw: Vec<u8>, parsed: ParsedBody) -> Self {
        self.inner.write().unwrap().parsed_bodies.insert(raw, parsed);
        self
    }

    /// Override mailbox counts for a specific mailbox id. Without this the
    /// store derives total/unread from the message list.
    pub fn with_mailbox_counts(self, mb_id: i64, counts: MailboxCounts) -> Self {
        self.inner
            .write()
            .unwrap()
            .mailbox_counts
            .insert(mb_id, counts);
        self
    }

    /// Make [`MailStore::list_mailboxes`] return an error carrying `msg`.
    pub fn list_mailboxes_fails(self, msg: &str) -> Self {
        self.inner.write().unwrap().list_mailboxes_error = Some(msg.to_string());
        self
    }

    /// Make [`MailStore::mailbox_status`] return an error carrying `msg`.
    pub fn mailbox_status_fails(self, msg: &str) -> Self {
        self.inner.write().unwrap().mailbox_status_error = Some(msg.to_string());
        self
    }

    /// Make [`MailStore::list_messages`] return an error carrying `msg`.
    pub fn list_messages_fails(self, msg: &str) -> Self {
        self.inner.write().unwrap().list_messages_error = Some(msg.to_string());
        self
    }

    /// Make [`MailStore::get_message_by_db_id`] return an error carrying `msg`.
    pub fn get_message_fails(self, msg: &str) -> Self {
        self.inner.write().unwrap().get_message_error = Some(msg.to_string());
        self
    }

    /// Make [`MailStore::list_thread_messages`] return an error carrying `msg`.
    pub fn list_thread_messages_fails(self, msg: &str) -> Self {
        self.inner.write().unwrap().list_thread_messages_error = Some(msg.to_string());
        self
    }

    /// Make [`MailStore::update_flags`] return an error carrying `msg`.
    pub fn update_flags_fails(self, msg: &str) -> Self {
        self.inner.write().unwrap().update_flags_error = Some(msg.to_string());
        self
    }

    /// Make [`MailStore::add_flags`] return an error carrying `msg`.
    pub fn add_flags_fails(self, msg: &str) -> Self {
        self.inner.write().unwrap().add_flags_error = Some(msg.to_string());
        self
    }

    /// Configure [`MailStore::submit_message`] to fail with a description.
    pub fn submission_fails_with(self, msg: &str) -> Self {
        self.inner.write().unwrap().submission_result = SubmissionResult {
            success: false,
            message: Some(msg.to_string()),
        };
        self
    }

    /// Configure [`MailStore::submit_message`] to fail without a description.
    pub fn submission_fails_silently(self) -> Self {
        self.inner.write().unwrap().submission_result = SubmissionResult {
            success: false,
            message: None,
        };
        self
    }

    /// Read back the current flag bitmask for `(mailbox_id, uid)`. `None`
    /// when the row is missing. Tests use this to assert the effect of
    /// `Email/set` updates and destroys.
    pub fn flags_for(&self, mailbox_id: i64, uid: u32) -> Option<u32> {
        self.inner
            .read()
            .unwrap()
            .messages
            .iter()
            .find(|m| m.mailbox_id == mailbox_id && m.uid == uid)
            .map(|m| m.flags)
    }
}

impl Default for InMemoryStore {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl MailStore for InMemoryStore {
    async fn list_mailboxes(&self, _user: &str) -> Result<Vec<Mailbox>, StoreError> {
        let inner = self.inner.read().unwrap();
        if let Some(ref msg) = inner.list_mailboxes_error {
            return Err(msg.clone().into());
        }
        Ok(inner.mailboxes.clone())
    }

    async fn mailbox_status(&self, mailbox_id: i64) -> Result<MailboxCounts, StoreError> {
        let inner = self.inner.read().unwrap();
        if let Some(ref msg) = inner.mailbox_status_error {
            return Err(msg.clone().into());
        }
        if let Some(counts) = inner.mailbox_counts.get(&mailbox_id) {
            return Ok(*counts);
        }
        // Default: total = messages in mailbox, unread = those without FLAG_SEEN.
        let total = inner
            .messages
            .iter()
            .filter(|m| m.mailbox_id == mailbox_id)
            .count() as u32;
        let unread = inner
            .messages
            .iter()
            .filter(|m| m.mailbox_id == mailbox_id && m.flags & FLAG_SEEN == 0)
            .count() as u32;
        Ok(MailboxCounts { total, unread })
    }

    async fn list_messages(
        &self,
        mailbox_id: i64,
        offset: u32,
        limit: u32,
    ) -> Result<Vec<Message>, StoreError> {
        let inner = self.inner.read().unwrap();
        if let Some(ref msg) = inner.list_messages_error {
            return Err(msg.clone().into());
        }
        Ok(inner
            .messages
            .iter()
            .filter(|m| m.mailbox_id == mailbox_id)
            .skip(offset as usize)
            .take(limit as usize)
            .cloned()
            .collect())
    }

    async fn get_message_by_db_id(
        &self,
        user: &str,
        id: i64,
    ) -> Result<Option<Message>, StoreError> {
        let inner = self.inner.read().unwrap();
        if let Some(ref msg) = inner.get_message_error {
            return Err(msg.clone().into());
        }
        Ok(inner
            .messages
            .iter()
            .find(|m| m.id == id && m.user_address == user)
            .cloned())
    }

    async fn list_thread_messages(
        &self,
        user: &str,
        thread_id: &str,
    ) -> Result<Vec<Message>, StoreError> {
        let inner = self.inner.read().unwrap();
        if let Some(ref msg) = inner.list_thread_messages_error {
            return Err(msg.clone().into());
        }
        Ok(inner
            .messages
            .iter()
            .filter(|m| m.thread_id == thread_id && m.user_address == user)
            .cloned()
            .collect())
    }

    async fn update_flags(
        &self,
        mailbox_id: i64,
        uid: u32,
        flags: u32,
    ) -> Result<(), StoreError> {
        let mut inner = self.inner.write().unwrap();
        if let Some(ref msg) = inner.update_flags_error {
            return Err(msg.clone().into());
        }
        if let Some(m) = inner
            .messages
            .iter_mut()
            .find(|m| m.mailbox_id == mailbox_id && m.uid == uid)
        {
            m.flags = flags;
        }
        Ok(())
    }

    async fn add_flags(&self, mailbox_id: i64, uid: u32, flags: u32) -> Result<(), StoreError> {
        let mut inner = self.inner.write().unwrap();
        if let Some(ref msg) = inner.add_flags_error {
            return Err(msg.clone().into());
        }
        if let Some(m) = inner
            .messages
            .iter_mut()
            .find(|m| m.mailbox_id == mailbox_id && m.uid == uid)
        {
            m.flags |= flags;
        }
        Ok(())
    }

    async fn read_message_raw(&self, message: &Message) -> Option<Vec<u8>> {
        self.inner.read().unwrap().raw_bytes.get(&message.id).cloned()
    }

    fn parse_message(&self, raw: &[u8]) -> ParsedBody {
        self.inner
            .read()
            .unwrap()
            .parsed_bodies
            .get(raw)
            .cloned()
            .unwrap_or_default()
    }

    async fn submit_message(
        &self,
        _user: &str,
        _message: &Message,
        _raw: &[u8],
    ) -> SubmissionResult {
        self.inner.read().unwrap().submission_result.clone()
    }
}

/// Build a [`Message`] with sane defaults. Tests override only the fields
/// they care about by mutating the returned value before handing it to
/// [`InMemoryStore::with_message`].
pub fn make_message(id: i64, mailbox_id: i64, user: &str) -> Message {
    Message {
        id,
        mailbox_id,
        uid: id as u32,
        sender: "Sender <sender@example.com>".to_string(),
        recipients: user.to_string(),
        subject: format!("message {id}"),
        date: 1_700_000_000 + id,
        size: 256,
        flags: 0,
        internal_date: 1_700_000_000 + id,
        message_id: format!("msg-{id}@example.com"),
        in_reply_to: String::new(),
        thread_id: format!("thread-{id}"),
        user_address: user.to_string(),
        new_content: Some(format!("snippet {id}")),
        blob_id: format!("blob-{id}"),
    }
}

/// Assemble a [`JmapRequest`] from a slice of `(method, args, call_id)`
/// tuples. Always declares the mail capability.
pub fn make_request(calls: &[(&str, Value, &str)]) -> JmapRequest {
    JmapRequest {
        using: vec!["urn:ietf:params:jmap:mail".to_string()],
        method_calls: calls
            .iter()
            .map(|(m, a, c)| (m.to_string(), a.clone(), c.to_string()))
            .collect(),
    }
}

/// Build a [`ParsedBody`] containing only a plain-text part.
pub fn parsed_with_text(text: &str) -> ParsedBody {
    ParsedBody {
        text: Some(text.to_string()),
        html: None,
        attachments: vec![],
    }
}

/// Build a [`ParsedBody`] containing one attachment of the given size and no
/// body parts.
pub fn parsed_with_attachment(filename: &str, content_type: &str, size: u32) -> ParsedBody {
    ParsedBody {
        text: None,
        html: None,
        attachments: vec![Attachment {
            filename: filename.to_string(),
            content_type: content_type.to_string(),
            size,
        }],
    }
}