jmap_mail_types/mailbox.rs
1//! RFC 8621 §2 Mailbox object and its component types.
2//!
3//! Provides [`Mailbox`], [`MailboxRole`], and [`MailboxRights`].
4//! Mailboxes are the folder containers for [`crate::Email`] objects.
5
6use jmap_types::{impl_string_enum, Id};
7use serde::{Deserialize, Serialize};
8
9/// The role of a Mailbox, identifying its common purpose (RFC 8621 §2).
10///
11/// Values correspond to IMAP Mailbox Name Attributes (RFC 8457), converted to
12/// lowercase. An account is not required to have Mailboxes with any particular
13/// role, and at most one Mailbox per account may hold each role.
14#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15#[non_exhaustive]
16pub enum MailboxRole {
17 /// The primary inbox for new incoming messages.
18 Inbox,
19 /// Deleted messages.
20 Trash,
21 /// Sent messages.
22 Sent,
23 /// Unsent draft messages.
24 Drafts,
25 /// Messages identified as likely spam.
26 Junk,
27 /// Archived messages.
28 Archive,
29 /// Messages flagged for follow-up.
30 Flagged,
31 /// Messages considered important.
32 Important,
33 /// Virtual mailbox containing all messages.
34 All,
35 /// Any role string not recognized by this implementation.
36 ///
37 /// RFC 8621 §2: "An unrecognized role SHOULD be treated as if no role were set."
38 ///
39 /// The inner string retains the original value received from the server, so
40 /// this variant round-trips correctly. When sending a `Mailbox/set` request
41 /// for a mailbox whose role came from the server, it is safe to echo the role
42 /// back — or omit it by setting `role` to `None`.
43 Other(String),
44}
45
46impl_string_enum!(MailboxRole, "a JMAP Mailbox role string",
47 "inbox" => Inbox,
48 "trash" => Trash,
49 "sent" => Sent,
50 "drafts" => Drafts,
51 "junk" => Junk,
52 "archive" => Archive,
53 "flagged" => Flagged,
54 "important" => Important,
55 "all" => All,
56);
57
58impl MailboxRole {
59 /// Return the RFC 8621 wire-format string for this role.
60 ///
61 /// Because this method is defined inside the crate that owns `MailboxRole`,
62 /// the match is exhaustive even though the enum is `#[non_exhaustive]`.
63 /// Adding a new variant without updating this method is a compile error.
64 pub fn to_wire_str(&self) -> &str {
65 match self {
66 Self::Inbox => "inbox",
67 Self::Trash => "trash",
68 Self::Sent => "sent",
69 Self::Drafts => "drafts",
70 Self::Junk => "junk",
71 Self::Archive => "archive",
72 Self::Flagged => "flagged",
73 Self::Important => "important",
74 Self::All => "all",
75 Self::Other(s) => s.as_str(),
76 }
77 }
78}
79
80/// Access control rights the authenticated user holds for a Mailbox (RFC 8621 §2).
81///
82/// Backwards compatible with IMAP ACLs (RFC 4314).
83///
84/// `Default` produces all-false (no access), which is the most restrictive valid value
85/// and a safe starting point when constructing rights in tests or server code.
86#[non_exhaustive]
87#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
88#[serde(rename_all = "camelCase")]
89pub struct MailboxRights {
90 /// User may use this Mailbox in Email/query filters and read its emails.
91 pub may_read_items: bool,
92 /// User may add mail to this Mailbox.
93 pub may_add_items: bool,
94 /// User may remove mail from this Mailbox.
95 pub may_remove_items: bool,
96 /// User may add or remove the `$seen` keyword on emails in this Mailbox.
97 pub may_set_seen: bool,
98 /// User may add or remove keywords other than `$seen` on emails.
99 pub may_set_keywords: bool,
100 /// User may create a child Mailbox under this one.
101 pub may_create_child: bool,
102 /// User may rename this Mailbox or move it under another parent.
103 pub may_rename: bool,
104 /// User may delete this Mailbox.
105 pub may_delete: bool,
106 /// Messages may be submitted directly to this Mailbox.
107 pub may_submit: bool,
108 /// Catch-all for vendor / site / private extension fields not covered
109 /// by the typed fields above. Preserves unknown fields across
110 /// deserialize/serialize round-trip per workspace extras-preservation
111 /// policy (see workspace AGENTS.md).
112 #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
113 pub extra: serde_json::Map<String, serde_json::Value>,
114}
115
116/// A JMAP Mailbox object (RFC 8621 §2).
117///
118/// Mailboxes are the containers for Email objects. Each Email must belong to
119/// at least one Mailbox. Mailboxes form an acyclic forest via `parent_id`.
120#[non_exhaustive]
121#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
122#[serde(rename_all = "camelCase")]
123pub struct Mailbox {
124 /// Server-assigned immutable identifier.
125 pub id: Id,
126 /// User-visible name for this Mailbox.
127 pub name: String,
128 /// Id of the parent Mailbox, or `None` if top-level.
129 #[serde(skip_serializing_if = "Option::is_none")]
130 pub parent_id: Option<Id>,
131 /// Well-known role identifying the Mailbox's common purpose.
132 #[serde(skip_serializing_if = "Option::is_none")]
133 pub role: Option<MailboxRole>,
134 /// Client UI sort position; lower values sort first among siblings.
135 pub sort_order: u32,
136 /// Total number of Emails in this Mailbox (server-set).
137 pub total_emails: u32,
138 /// Number of Emails without `$seen` or `$draft` (server-set).
139 pub unread_emails: u32,
140 /// Number of Threads with at least one Email in this Mailbox (server-set).
141 pub total_threads: u32,
142 /// Number of unread Threads in this Mailbox (server-set).
143 pub unread_threads: u32,
144 /// ACL rights the authenticated user has on this Mailbox (server-set).
145 pub my_rights: MailboxRights,
146 /// Whether the user has subscribed to this Mailbox.
147 pub is_subscribed: bool,
148 /// Catch-all for vendor / site / private extension fields not covered
149 /// by the typed fields above. Preserves unknown fields across
150 /// deserialize/serialize round-trip per workspace extras-preservation
151 /// policy (see workspace AGENTS.md).
152 #[serde(flatten, default, skip_serializing_if = "serde_json::Map::is_empty")]
153 pub extra: serde_json::Map<String, serde_json::Value>,
154}
155
156impl Mailbox {
157 /// Construct a [`Mailbox`] from its required fields.
158 ///
159 /// `parent_id` and `role` default to `None`.
160 // Nine arguments because Mailbox has nine required RFC 8621 properties; all
161 // are needed for construction since #[non_exhaustive] prevents struct
162 // literals outside this crate.
163 #[allow(clippy::too_many_arguments)]
164 pub fn new(
165 id: Id,
166 name: impl Into<String>,
167 sort_order: u32,
168 total_emails: u32,
169 unread_emails: u32,
170 total_threads: u32,
171 unread_threads: u32,
172 my_rights: MailboxRights,
173 is_subscribed: bool,
174 ) -> Self {
175 Self {
176 id,
177 name: name.into(),
178 sort_order,
179 total_emails,
180 unread_emails,
181 total_threads,
182 unread_threads,
183 my_rights,
184 is_subscribed,
185 parent_id: None,
186 role: None,
187 extra: serde_json::Map::new(),
188 }
189 }
190}
191
192/// Deserialize `parent_id` so that an explicit JSON `null` is preserved as
193/// `Some(Value::Null)` instead of being collapsed to `None`, while
194/// rejecting wire values that RFC 8621 §2.3 does not permit (numbers,
195/// booleans, arrays, nested objects).
196///
197/// `#[serde(default)]` on the field handles the absent case (produces `None`
198/// without calling this function). When the field is present, serde calls
199/// this function with the value, which always wraps a spec-valid result
200/// in `Some(...)`. This is the only way to distinguish `{}` from
201/// `{"parentId": null}` for a field of type `Option<T>` — serde's
202/// default `Option<T>` Deserialize impl treats both as `None`.
203///
204/// Per RFC 8621 §2.3 the only valid `parentId` wire shapes are JSON
205/// `null` and a JSON string (Mailbox `Id`). Anything else is a
206/// non-conformant peer; rejecting at the deserialize layer surfaces
207/// the error to the caller instead of silently round-tripping
208/// wire-invalid data.
209fn deserialize_parent_id_three_way<'de, D>(
210 deserializer: D,
211) -> Result<Option<serde_json::Value>, D::Error>
212where
213 D: serde::Deserializer<'de>,
214{
215 use serde::Deserialize;
216 let v = serde_json::Value::deserialize(deserializer)?;
217 match &v {
218 serde_json::Value::Null | serde_json::Value::String(_) => Ok(Some(v)),
219 other => Err(serde::de::Error::custom(format!(
220 "parentId must be null or a string (Mailbox Id) per RFC 8621 §2.3; got {}",
221 match other {
222 serde_json::Value::Bool(_) => "boolean",
223 serde_json::Value::Number(_) => "number",
224 serde_json::Value::Array(_) => "array",
225 serde_json::Value::Object(_) => "object",
226 // Null and String already returned above.
227 _ => "unexpected value",
228 }
229 ))),
230 }
231}
232
233/// Filter condition for `Mailbox/query` (RFC 8621 §2.3).
234///
235/// All fields are optional; a condition with no fields set matches every Mailbox.
236///
237/// ## `parentId` semantics
238///
239/// The `parentId` field has three distinct states that must be preserved:
240/// - **absent** (`None`) — do not filter by parent; return mailboxes at any level.
241/// - **`null`** (`Some(serde_json::Value::Null)`) — return only top-level mailboxes
242/// (those with no parent).
243/// - **`"<id>"`** (`Some(serde_json::Value::String(...))`) — return only mailboxes
244/// whose `parentId` equals the given `Id`.
245///
246/// The combination of `#[serde(default, deserialize_with = ...)]` preserves
247/// this three-way distinction. Without the custom deserializer, serde's
248/// default `Option<T>` Deserialize impl would collapse a JSON `null` to
249/// `None`, making `null` and absent indistinguishable.
250#[non_exhaustive]
251#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
252#[serde(rename_all = "camelCase")]
253pub struct MailboxFilterCondition {
254 /// See type-level docs for three-way semantics.
255 #[serde(
256 default,
257 deserialize_with = "deserialize_parent_id_three_way",
258 skip_serializing_if = "Option::is_none"
259 )]
260 pub parent_id: Option<serde_json::Value>,
261
262 /// Mailbox name must contain this string (case-sensitive substring match).
263 #[serde(skip_serializing_if = "Option::is_none")]
264 pub name: Option<String>,
265
266 /// Mailbox role must equal this string (e.g. `"inbox"`, `"trash"`).
267 #[serde(skip_serializing_if = "Option::is_none")]
268 pub role: Option<String>,
269
270 /// If `true`, only mailboxes with a non-null role are returned.
271 #[serde(skip_serializing_if = "Option::is_none")]
272 pub has_any_role: Option<bool>,
273
274 /// If `true`, only subscribed mailboxes are returned.
275 #[serde(skip_serializing_if = "Option::is_none")]
276 pub is_subscribed: Option<bool>,
277}
278
279/// The wire-format field names accepted by [`MailboxFilterCondition`]
280/// (RFC 8621 §2.3). Server crates use this to pre-validate
281/// `Mailbox/query.filter` against unknown keys per RFC 8620 §5.5
282/// (return `unsupportedFilter`) before deserialising into the typed
283/// struct. The drift-check unit test in this module asserts that the
284/// list matches every field actually serialised by the struct, so a
285/// new field on `MailboxFilterCondition` cannot quietly diverge from
286/// this list — `cargo test -p jmap-mail-types` will fail.
287pub const MAILBOX_FILTER_CONDITION_KEYS: &[&str] =
288 &["parentId", "name", "role", "hasAnyRole", "isSubscribed"];
289
290#[cfg(test)]
291mod tests {
292 use super::*;
293 use serde_json::json;
294
295 // ── Extras-preservation policy tests (JMAP-lbdy.2) ───────────────────
296
297 /// `MailboxRights.extra` captures vendor fields and preserves them.
298 #[test]
299 fn mailbox_rights_preserves_vendor_extras() {
300 let raw = json!({
301 "mayReadItems": true,
302 "mayAddItems": true,
303 "mayRemoveItems": false,
304 "maySetSeen": true,
305 "maySetKeywords": false,
306 "mayCreateChild": false,
307 "mayRename": false,
308 "mayDelete": false,
309 "maySubmit": false,
310 "acmeCorpMayPin": true
311 });
312 let rights: MailboxRights = serde_json::from_value(raw).unwrap();
313 assert_eq!(
314 rights.extra.get("acmeCorpMayPin").and_then(|v| v.as_bool()),
315 Some(true)
316 );
317 let back = serde_json::to_value(&rights).unwrap();
318 assert_eq!(back["acmeCorpMayPin"], true);
319 }
320
321 /// `Mailbox.extra` captures vendor fields and preserves them.
322 #[test]
323 fn mailbox_preserves_vendor_extras() {
324 let raw = json!({
325 "id": "m1",
326 "name": "Inbox",
327 "sortOrder": 0,
328 "totalEmails": 0,
329 "unreadEmails": 0,
330 "totalThreads": 0,
331 "unreadThreads": 0,
332 "myRights": {
333 "mayReadItems": true, "mayAddItems": true, "mayRemoveItems": true,
334 "maySetSeen": true, "maySetKeywords": true, "mayCreateChild": true,
335 "mayRename": true, "mayDelete": false, "maySubmit": true
336 },
337 "isSubscribed": true,
338 "acmeCorpColor": "#ff0000"
339 });
340 let mbox: Mailbox = serde_json::from_value(raw).unwrap();
341 assert_eq!(
342 mbox.extra.get("acmeCorpColor").and_then(|v| v.as_str()),
343 Some("#ff0000")
344 );
345 let back = serde_json::to_value(&mbox).unwrap();
346 assert_eq!(back["acmeCorpColor"], "#ff0000");
347 }
348
349 // ── parentId three-way semantics (RFC 8621 §2.3) ────────────────────
350 //
351 // The Mailbox/query filter distinguishes three states for `parentId`:
352 // absent (no filter), explicit JSON null (top-level only), explicit string
353 // (specific parent). The default `Option<T>` Deserialize impl collapses
354 // `null` to `None`, so `MailboxFilterCondition.parent_id` uses a custom
355 // deserializer to preserve `Some(Value::Null)`. These tests lock that
356 // behavior.
357
358 /// Oracle: absent `parentId` deserializes as `None` and serializes back to
359 /// an object without the field.
360 #[test]
361 fn mailbox_filter_parent_id_absent_round_trips() {
362 let cond: MailboxFilterCondition = serde_json::from_value(json!({})).unwrap();
363 assert!(cond.parent_id.is_none(), "absent must deserialize as None");
364 let back = serde_json::to_value(&cond).unwrap();
365 assert!(
366 back.get("parentId").is_none(),
367 "absent parentId must not appear in serialized output"
368 );
369 }
370
371 /// Oracle: explicit JSON `null` for `parentId` deserializes as
372 /// `Some(Value::Null)` (NOT `None`) and serializes back to `null`.
373 /// This is the wire-level signal for "top-level mailboxes only".
374 #[test]
375 fn mailbox_filter_parent_id_null_round_trips() {
376 let cond: MailboxFilterCondition =
377 serde_json::from_value(json!({"parentId": null})).unwrap();
378 assert!(
379 matches!(cond.parent_id, Some(serde_json::Value::Null)),
380 "explicit null must deserialize as Some(Value::Null), got {:?}",
381 cond.parent_id
382 );
383 let back = serde_json::to_value(&cond).unwrap();
384 assert_eq!(
385 back["parentId"],
386 serde_json::Value::Null,
387 "Some(Value::Null) must serialize back as null"
388 );
389 }
390
391 /// Oracle: explicit string `parentId` deserializes as
392 /// `Some(Value::String(...))` and serializes back to the same string.
393 #[test]
394 fn mailbox_filter_parent_id_string_round_trips() {
395 let cond: MailboxFilterCondition =
396 serde_json::from_value(json!({"parentId": "mbox-42"})).unwrap();
397 match cond.parent_id {
398 Some(serde_json::Value::String(ref s)) => assert_eq!(s, "mbox-42"),
399 other => panic!("expected Some(String), got {other:?}"),
400 }
401 let back = serde_json::to_value(&cond).unwrap();
402 assert_eq!(back["parentId"], "mbox-42");
403 }
404
405 /// RFC 8621 §2.3 admits only JSON `null` and a JSON string for
406 /// `parentId`. Numbers, booleans, arrays, and nested objects MUST
407 /// be rejected at deserialize time rather than silently round-
408 /// tripped as opaque `serde_json::Value` blobs.
409 ///
410 /// Independent oracle: the spec text itself. Each rejected case
411 /// asserts the error message references the spec section, so a
412 /// future shape change that loosened the deserializer would be
413 /// caught.
414 #[test]
415 fn mailbox_filter_parent_id_rejects_non_spec_shapes() {
416 for (bad, label) in [
417 (json!({"parentId": 42}), "number"),
418 (json!({"parentId": true}), "boolean"),
419 (json!({"parentId": ["m1"]}), "array"),
420 (json!({"parentId": {"id": "m1"}}), "object"),
421 ] {
422 let err = serde_json::from_value::<MailboxFilterCondition>(bad)
423 .expect_err(&format!("{label} must be rejected"));
424 let msg = err.to_string();
425 assert!(
426 msg.contains("parentId must be null or a string"),
427 "{label} rejection must cite the spec rule; got: {msg}"
428 );
429 }
430 }
431
432 /// Oracle: [`MAILBOX_FILTER_CONDITION_KEYS`] is a single source of truth
433 /// for the wire-format keys of [`MailboxFilterCondition`]. Mirror the
434 /// const against the actual serialised field names, derived by
435 /// fully-populating the struct and asking serde for the JSON keys. A
436 /// future contributor who adds a new field but forgets to update the
437 /// const trips this test (`cargo test -p jmap-mail-types`), avoiding
438 /// the silent server-side drift documented in `bd:JMAP-j7pa.3`.
439 ///
440 /// Independent oracle: the JSON object produced by `serde_json::to_value`
441 /// over a hand-built struct value. The struct is owned by this crate;
442 /// the const is owned by this crate; the test compares one against the
443 /// other at compile-equivalent time. No JMAP-server code is involved.
444 #[test]
445 fn mailbox_filter_condition_keys_matches_struct_fields() {
446 use std::collections::BTreeSet;
447
448 // Every field gets a non-None value so every key is serialised.
449 let mut cond = MailboxFilterCondition {
450 parent_id: Some(serde_json::Value::String("any-id".into())),
451 name: Some("inbox".into()),
452 role: Some("inbox".into()),
453 has_any_role: Some(true),
454 is_subscribed: Some(true),
455 };
456 // Touch the `cond` binding via serialisation; the let-binding is
457 // intentionally mutable to force this test to be updated alongside
458 // any future field addition that lands without an obvious default.
459 let _ = &mut cond;
460
461 let value = serde_json::to_value(&cond).expect("serialisation must succeed");
462 let serialised_keys: BTreeSet<&str> = value
463 .as_object()
464 .expect("filter condition must serialise as an object")
465 .keys()
466 .map(String::as_str)
467 .collect();
468
469 let declared_keys: BTreeSet<&str> = MAILBOX_FILTER_CONDITION_KEYS.iter().copied().collect();
470
471 assert_eq!(
472 declared_keys, serialised_keys,
473 "MAILBOX_FILTER_CONDITION_KEYS must match every serialised field of MailboxFilterCondition; \
474 declared={declared_keys:?}, serialised={serialised_keys:?}. \
475 If you added a field to MailboxFilterCondition, add its camelCase wire name to the const."
476 );
477 }
478}