• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

duesee / imap-codec / 18808752820

25 Oct 2025 09:31PM UTC coverage: 91.227% (-0.5%) from 91.776%
18808752820

push

github

duesee
feat: add NAMESPACE support

Co-Authored-by: Damian Poddebniak <poddebniak@mailbox.org>

69 of 141 new or added lines in 6 files covered. (48.94%)

8 existing lines in 1 file now uncovered.

10378 of 11376 relevant lines covered (91.23%)

930.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.19
/imap-types/src/command.rs
1
//! Client Commands.
2
//!
3
//! See <https://tools.ietf.org/html/rfc3501#section-6>.
4

5
use std::borrow::Cow;
6
#[cfg(feature = "ext_condstore_qresync")]
7
use std::num::NonZeroU32;
8
#[cfg(feature = "ext_condstore_qresync")]
9
use std::num::NonZeroU64;
10

11
#[cfg(feature = "arbitrary")]
12
use arbitrary::Arbitrary;
13
use bounded_static_derive::ToStatic;
14
#[cfg(feature = "serde")]
15
use serde::{Deserialize, Serialize};
16

17
#[cfg(feature = "ext_id")]
18
use crate::core::{IString, NString};
19
#[cfg(feature = "ext_metadata")]
20
use crate::extensions::metadata::{Entry, EntryValue, GetMetadataOption};
21
use crate::{
22
    auth::AuthMechanism,
23
    command::error::{AppendError, CopyError, ListError, LoginError, RenameError},
24
    core::{AString, Charset, Literal, Tag, Vec1},
25
    datetime::DateTime,
26
    extensions::{
27
        binary::LiteralOrLiteral8, compress::CompressionAlgorithm, enable::CapabilityEnable,
28
        quota::QuotaSet, sort::SortCriterion, thread::ThreadingAlgorithm,
29
    },
30
    fetch::MacroOrMessageDataItemNames,
31
    flag::{Flag, StoreResponse, StoreType},
32
    mailbox::{ListMailbox, Mailbox},
33
    search::SearchKey,
34
    secret::Secret,
35
    sequence::SequenceSet,
36
    status::StatusDataItemName,
37
};
38

39
/// Command.
40
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
41
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
42
#[derive(Debug, Clone, PartialEq, Eq, Hash, ToStatic)]
43
pub struct Command<'a> {
44
    /// Tag.
45
    pub tag: Tag<'a>,
46
    /// Body, e.g., CAPABILITY, LOGIN, SELECT, etc.
47
    pub body: CommandBody<'a>,
48
}
49

50
impl<'a> Command<'a> {
51
    /// Create a new command.
52
    pub fn new<T>(tag: T, body: CommandBody<'a>) -> Result<Self, T::Error>
178✔
53
    where
178✔
54
        T: TryInto<Tag<'a>>,
178✔
55
    {
56
        Ok(Self {
57
            tag: tag.try_into()?,
178✔
58
            body,
160✔
59
        })
60
    }
178✔
61

62
    /// Get the command name.
63
    pub fn name(&self) -> &'static str {
×
64
        self.body.name()
×
65
    }
×
66
}
67

68
/// Command body.
69
///
70
/// This enum is used to encode all the different commands.
71
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
72
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
73
#[cfg_attr(feature = "serde", serde(tag = "type", content = "content"))]
74
#[derive(Debug, Clone, PartialEq, Eq, Hash, ToStatic)]
75
pub enum CommandBody<'a> {
76
    // ----- Any State (see https://tools.ietf.org/html/rfc3501#section-6.1) -----
77
    /// ### 6.1.1.  CAPABILITY Command
78
    ///
79
    /// * Arguments:  none
80
    /// * Responses:  REQUIRED untagged response: CAPABILITY
81
    /// * Result:
82
    ///   * OK - capability completed
83
    ///   * BAD - command unknown or arguments invalid
84
    ///
85
    /// The CAPABILITY command requests a listing of capabilities that the
86
    /// server supports.  The server MUST send a single untagged
87
    /// CAPABILITY response with "IMAP4rev1" as one of the listed
88
    /// capabilities before the (tagged) OK response.
89
    ///
90
    /// A capability name which begins with "AUTH=" indicates that the
91
    /// server supports that particular authentication mechanism.  All
92
    /// such names are, by definition, part of this specification.  For
93
    /// example, the authorization capability for an experimental
94
    /// "blurdybloop" authenticator would be "AUTH=XBLURDYBLOOP" and not
95
    /// "XAUTH=BLURDYBLOOP" or "XAUTH=XBLURDYBLOOP".
96
    ///
97
    /// Other capability names refer to extensions, revisions, or
98
    /// amendments to this specification.  See the documentation of the
99
    /// CAPABILITY response for additional information.  No capabilities,
100
    /// beyond the base IMAP4rev1 set defined in this specification, are
101
    /// enabled without explicit client action to invoke the capability.
102
    ///
103
    /// Client and server implementations MUST implement the STARTTLS,
104
    /// LOGINDISABLED, and AUTH=PLAIN (described in [IMAP-TLS])
105
    /// capabilities.  See the Security Considerations section for
106
    /// important information.
107
    ///
108
    /// See the section entitled "Client Commands -
109
    /// Experimental/Expansion" for information about the form of site or
110
    /// implementation-specific capabilities.
111
    Capability,
112

113
    /// ### 6.1.2.  NOOP Command
114
    ///
115
    /// * Arguments:  none
116
    /// * Responses:  no specific responses for this command (but see below)
117
    /// * Result:
118
    ///   * OK - noop completed
119
    ///   * BAD - command unknown or arguments invalid
120
    ///
121
    /// The NOOP command always succeeds.  It does nothing.
122
    ///
123
    /// Since any command can return a status update as untagged data, the
124
    /// NOOP command can be used as a periodic poll for new messages or
125
    /// message status updates during a period of inactivity (this is the
126
    /// preferred method to do this).  The NOOP command can also be used
127
    /// to reset any inactivity autologout timer on the server.
128
    Noop,
129

130
    /// ### 6.1.3.  LOGOUT Command
131
    ///
132
    /// * Arguments:  none
133
    /// * Responses:  REQUIRED untagged response: BYE
134
    /// * Result:
135
    ///   * OK - logout completed
136
    ///   * BAD - command unknown or arguments invalid
137
    ///
138
    /// The LOGOUT command informs the server that the client is done with
139
    /// the connection.  The server MUST send a BYE untagged response
140
    /// before the (tagged) OK response, and then close the network
141
    /// connection.
142
    Logout,
143

144
    // ----- Not Authenticated State (https://tools.ietf.org/html/rfc3501#section-6.2) -----
145
    /// ### 6.2.1.  STARTTLS Command
146
    ///
147
    /// * Arguments:  none
148
    /// * Responses:  no specific response for this command
149
    /// * Result:
150
    ///   * OK - starttls completed, begin TLS negotiation
151
    ///   * BAD - command unknown or arguments invalid
152
    ///
153
    /// A \[TLS\] negotiation begins immediately after the CRLF at the end
154
    /// of the tagged OK response from the server.  Once a client issues a
155
    /// STARTTLS command, it MUST NOT issue further commands until a
156
    /// server response is seen and the \[TLS\] negotiation is complete.
157
    ///
158
    /// The server remains in the non-authenticated state, even if client
159
    /// credentials are supplied during the \[TLS\] negotiation.  This does
160
    /// not preclude an authentication mechanism such as EXTERNAL (defined
161
    /// in \[SASL\]) from using client identity determined by the \[TLS\]
162
    /// negotiation.
163
    ///
164
    /// Once \[TLS\] has been started, the client MUST discard cached
165
    /// information about server capabilities and SHOULD re-issue the
166
    /// CAPABILITY command.  This is necessary to protect against man-in-
167
    /// the-middle attacks which alter the capabilities list prior to
168
    /// STARTTLS.  The server MAY advertise different capabilities after
169
    /// STARTTLS.
170
    ///
171
    /// <div class="warning">
172
    /// This must only be used when the server advertised support for it sending the STARTTLS capability.
173
    ///
174
    /// Try to avoid STARTTLS using implicit TLS on port 993.
175
    /// </div>
176
    #[cfg(feature = "starttls")]
177
    #[cfg_attr(docsrs, doc(cfg(feature = "starttls")))]
178
    StartTLS,
179

180
    /// ### 6.2.2.  AUTHENTICATE Command
181
    ///
182
    /// * Arguments:  authentication mechanism name
183
    /// * Responses:  continuation data can be requested
184
    /// * Result:
185
    ///   * OK - authenticate completed, now in authenticated state
186
    ///   * NO - authenticate failure: unsupported authentication  mechanism, credentials rejected
187
    ///   * BAD - command unknown or arguments invalid, authentication exchange cancelled
188
    ///
189
    /// The AUTHENTICATE command indicates a \[SASL\] authentication
190
    /// mechanism to the server.  If the server supports the requested
191
    /// authentication mechanism, it performs an authentication protocol
192
    /// exchange to authenticate and identify the client.  It MAY also
193
    /// negotiate an OPTIONAL security layer for subsequent protocol
194
    /// interactions.  If the requested authentication mechanism is not
195
    /// supported, the server SHOULD reject the AUTHENTICATE command by
196
    /// sending a tagged NO response.
197
    ///
198
    /// The AUTHENTICATE command does not support the optional "initial
199
    /// response" feature of \[SASL\].  Section 5.1 of \[SASL\] specifies how
200
    /// to handle an authentication mechanism which uses an initial
201
    /// response.
202
    ///
203
    /// The service name specified by this protocol's profile of \[SASL\] is
204
    /// "imap".
205
    ///
206
    /// The authentication protocol exchange consists of a series of
207
    /// server challenges and client responses that are specific to the
208
    /// authentication mechanism.  A server challenge consists of a
209
    /// command continuation request response with the "+" token followed
210
    /// by a BASE64 encoded string.  The client response consists of a
211
    /// single line consisting of a BASE64 encoded string.  If the client
212
    /// wishes to cancel an authentication exchange, it issues a line
213
    /// consisting of a single "*".  If the server receives such a
214
    /// response, it MUST reject the AUTHENTICATE command by sending a
215
    /// tagged BAD response.
216
    ///
217
    /// If a security layer is negotiated through the \[SASL\]
218
    /// authentication exchange, it takes effect immediately following the
219
    /// CRLF that concludes the authentication exchange for the client,
220
    /// and the CRLF of the tagged OK response for the server.
221
    ///
222
    /// While client and server implementations MUST implement the
223
    /// AUTHENTICATE command itself, it is not required to implement any
224
    /// authentication mechanisms other than the PLAIN mechanism described
225
    /// in [IMAP-TLS].  Also, an authentication mechanism is not required
226
    /// to support any security layers.
227
    ///
228
    ///   Note: a server implementation MUST implement a
229
    ///   configuration in which it does NOT permit any plaintext
230
    ///   password mechanisms, unless either the STARTTLS command
231
    ///   has been negotiated or some other mechanism that
232
    ///   protects the session from password snooping has been
233
    ///   provided.  Server sites SHOULD NOT use any configuration
234
    ///   which permits a plaintext password mechanism without
235
    ///   such a protection mechanism against password snooping.
236
    ///   Client and server implementations SHOULD implement
237
    ///   additional \[SASL\] mechanisms that do not use plaintext
238
    ///   passwords, such the GSSAPI mechanism described in \[SASL\]
239
    ///   and/or the [DIGEST-MD5] mechanism.
240
    ///
241
    /// Servers and clients can support multiple authentication
242
    /// mechanisms.  The server SHOULD list its supported authentication
243
    /// mechanisms in the response to the CAPABILITY command so that the
244
    /// client knows which authentication mechanisms to use.
245
    ///
246
    /// A server MAY include a CAPABILITY response code in the tagged OK
247
    /// response of a successful AUTHENTICATE command in order to send
248
    /// capabilities automatically.  It is unnecessary for a client to
249
    /// send a separate CAPABILITY command if it recognizes these
250
    /// automatic capabilities.  This should only be done if a security
251
    /// layer was not negotiated by the AUTHENTICATE command, because the
252
    /// tagged OK response as part of an AUTHENTICATE command is not
253
    /// protected by encryption/integrity checking.  \[SASL\] requires the
254
    /// client to re-issue a CAPABILITY command in this case.
255
    ///
256
    /// If an AUTHENTICATE command fails with a NO response, the client
257
    /// MAY try another authentication mechanism by issuing another
258
    /// AUTHENTICATE command.  It MAY also attempt to authenticate by
259
    /// using the LOGIN command (see section 6.2.3 for more detail).  In
260
    /// other words, the client MAY request authentication types in
261
    /// decreasing order of preference, with the LOGIN command as a last
262
    /// resort.
263
    ///
264
    /// The authorization identity passed from the client to the server
265
    /// during the authentication exchange is interpreted by the server as
266
    /// the user name whose privileges the client is requesting.
267
    Authenticate {
268
        /// Authentication mechanism.
269
        mechanism: AuthMechanism<'a>,
270
        /// Initial response (if any).
271
        ///
272
        /// This type holds the raw binary data, i.e., a `Vec<u8>`, *not* the BASE64 string.
273
        ///
274
        /// <div class="warning">
275
        /// This extension must only be used when the server advertised support for it sending the SASL-IR capability.
276
        /// </div>
277
        initial_response: Option<Secret<Cow<'a, [u8]>>>,
278
    },
279

280
    /// ### 6.2.3.  LOGIN Command
281
    ///
282
    /// * Arguments:
283
    ///   * user name
284
    ///   * password
285
    /// * Responses:  no specific responses for this command
286
    /// * Result:
287
    ///   * OK - login completed, now in authenticated state
288
    ///   * NO - login failure: user name or password rejected
289
    ///   * BAD - command unknown or arguments invalid
290
    ///
291
    /// The LOGIN command identifies the client to the server and carries
292
    /// the plaintext password authenticating this user.
293
    ///
294
    /// A server MAY include a CAPABILITY response code in the tagged OK
295
    /// response to a successful LOGIN command in order to send
296
    /// capabilities automatically.  It is unnecessary for a client to
297
    /// send a separate CAPABILITY command if it recognizes these
298
    /// automatic capabilities.
299
    ///
300
    ///   Note: Use of the LOGIN command over an insecure network
301
    ///   (such as the Internet) is a security risk, because anyone
302
    ///   monitoring network traffic can obtain plaintext passwords.
303
    ///   The LOGIN command SHOULD NOT be used except as a last
304
    ///   resort, and it is recommended that client implementations
305
    ///   have a means to disable any automatic use of the LOGIN
306
    ///   command.
307
    ///
308
    ///   Unless either the STARTTLS command has been negotiated or
309
    ///   some other mechanism that protects the session from
310
    ///   password snooping has been provided, a server
311
    ///   implementation MUST implement a configuration in which it
312
    ///   advertises the LOGINDISABLED capability and does NOT permit
313
    ///   the LOGIN command.  Server sites SHOULD NOT use any
314
    ///   configuration which permits the LOGIN command without such
315
    ///   a protection mechanism against password snooping.  A client
316
    ///   implementation MUST NOT send a LOGIN command if the
317
    ///   LOGINDISABLED capability is advertised.
318
    Login {
319
        /// Username.
320
        username: AString<'a>,
321
        /// Password.
322
        password: Secret<AString<'a>>,
323
    },
324

325
    // ----- Authenticated State (https://tools.ietf.org/html/rfc3501#section-6.3) -----
326
    /// ### 6.3.1.  SELECT Command
327
    ///
328
    /// * Arguments:  mailbox name
329
    /// * Responses:
330
    ///   * REQUIRED untagged responses: FLAGS, EXISTS, RECENT
331
    ///   * REQUIRED OK untagged responses: UNSEEN, PERMANENTFLAGS, UIDNEXT, UIDVALIDITY
332
    /// * Result:
333
    ///   * OK - select completed, now in selected state
334
    ///   * NO - select failure, now in authenticated state: no such mailbox, can't access mailbox
335
    ///   * BAD - command unknown or arguments invalid
336
    ///
337
    /// The SELECT command selects a mailbox so that messages in the
338
    /// mailbox can be accessed.  Before returning an OK to the client,
339
    /// the server MUST send the following untagged data to the client.
340
    /// Note that earlier versions of this protocol only required the
341
    /// FLAGS, EXISTS, and RECENT untagged data; consequently, client
342
    /// implementations SHOULD implement default behavior for missing data
343
    /// as discussed with the individual item.
344
    ///
345
    ///   FLAGS       Defined flags in the mailbox.  See the description
346
    ///               of the FLAGS response for more detail.
347
    ///
348
    ///   \<n\> EXISTS  The number of messages in the mailbox.  See the
349
    ///               description of the EXISTS response for more detail.
350
    ///
351
    ///   \<n\> RECENT  The number of messages with the \Recent flag set.
352
    ///               See the description of the RECENT response for more
353
    ///               detail.
354
    ///
355
    ///   OK [UNSEEN \<n\>]
356
    ///               The message sequence number of the first unseen
357
    ///               message in the mailbox.  If this is missing, the
358
    ///               client can not make any assumptions about the first
359
    ///               unseen message in the mailbox, and needs to issue a
360
    ///               SEARCH command if it wants to find it.
361
    ///
362
    ///   OK [PERMANENTFLAGS (\<list of flags\>)]
363
    ///               A list of message flags that the client can change
364
    ///               permanently.  If this is missing, the client should
365
    ///               assume that all flags can be changed permanently.
366
    ///
367
    ///   OK [UIDNEXT \<n\>]
368
    ///               The next unique identifier value.  Refer to section
369
    ///               2.3.1.1 for more information.  If this is missing,
370
    ///               the client can not make any assumptions about the
371
    ///               next unique identifier value.
372
    ///
373
    ///   OK [UIDVALIDITY \<n\>]
374
    ///               The unique identifier validity value.  Refer to
375
    ///               section 2.3.1.1 for more information.  If this is
376
    ///               missing, the server does not support unique
377
    ///               identifiers.
378
    ///
379
    /// Only one mailbox can be selected at a time in a connection;
380
    /// simultaneous access to multiple mailboxes requires multiple
381
    /// connections.  The SELECT command automatically deselects any
382
    /// currently selected mailbox before attempting the new selection.
383
    /// Consequently, if a mailbox is selected and a SELECT command that
384
    /// fails is attempted, no mailbox is selected.
385
    ///
386
    /// If the client is permitted to modify the mailbox, the server
387
    /// SHOULD prefix the text of the tagged OK response with the
388
    /// "[READ-WRITE]" response code.
389
    ///
390
    /// If the client is not permitted to modify the mailbox but is
391
    /// permitted read access, the mailbox is selected as read-only, and
392
    /// the server MUST prefix the text of the tagged OK response to
393
    /// SELECT with the "[READ-ONLY]" response code.  Read-only access
394
    /// through SELECT differs from the EXAMINE command in that certain
395
    /// read-only mailboxes MAY permit the change of permanent state on a
396
    /// per-user (as opposed to global) basis.  Netnews messages marked in
397
    /// a server-based .newsrc file are an example of such per-user
398
    /// permanent state that can be modified with read-only mailboxes.
399
    Select {
400
        /// Mailbox.
401
        mailbox: Mailbox<'a>,
402
        #[cfg(feature = "ext_condstore_qresync")]
403
        parameters: Vec<SelectParameter>,
404
    },
405

406
    /// Unselect a mailbox.
407
    ///
408
    /// This should bring the client back to the AUTHENTICATED state.
409
    ///
410
    /// <div class="warning">
411
    /// This extension must only be used when the server advertised support for it sending the UNSELECT capability.
412
    /// </div>
413
    Unselect,
414

415
    /// 6.3.2.  EXAMINE Command
416
    ///
417
    /// Arguments:  mailbox name
418
    /// Responses:  REQUIRED untagged responses: FLAGS, EXISTS, RECENT
419
    ///             REQUIRED OK untagged responses:  UNSEEN,  PERMANENTFLAGS,
420
    ///             UIDNEXT, UIDVALIDITY
421
    /// Result:     OK - examine completed, now in selected state
422
    ///             NO - examine failure, now in authenticated state: no
423
    ///                  such mailbox, can't access mailbox
424
    ///             BAD - command unknown or arguments invalid
425
    ///
426
    /// The EXAMINE command is identical to SELECT and returns the same
427
    /// output; however, the selected mailbox is identified as read-only.
428
    /// No changes to the permanent state of the mailbox, including
429
    /// per-user state, are permitted; in particular, EXAMINE MUST NOT
430
    /// cause messages to lose the \Recent flag.
431
    ///
432
    /// The text of the tagged OK response to the EXAMINE command MUST
433
    /// begin with the "[READ-ONLY]" response code.
434
    Examine {
435
        /// Mailbox.
436
        mailbox: Mailbox<'a>,
437
        #[cfg(feature = "ext_condstore_qresync")]
438
        parameters: Vec<SelectParameter>,
439
    },
440

441
    /// ### 6.3.3.  CREATE Command
442
    ///
443
    /// * Arguments:  mailbox name
444
    /// * Responses:  no specific responses for this command
445
    /// * Result:
446
    ///   * OK - create completed
447
    ///   * NO - create failure: can't create mailbox with that name
448
    ///   * BAD - command unknown or arguments invalid
449
    ///
450
    /// The CREATE command creates a mailbox with the given name.  An OK
451
    /// response is returned only if a new mailbox with that name has been
452
    /// created.  It is an error to attempt to create INBOX or a mailbox
453
    /// with a name that refers to an extant mailbox.  Any error in
454
    /// creation will return a tagged NO response.
455
    ///
456
    /// If the mailbox name is suffixed with the server's hierarchy
457
    /// separator character (as returned from the server by a LIST
458
    /// command), this is a declaration that the client intends to create
459
    /// mailbox names under this name in the hierarchy.  Server
460
    /// implementations that do not require this declaration MUST ignore
461
    /// the declaration.  In any case, the name created is without the
462
    /// trailing hierarchy delimiter.
463
    ///
464
    /// If the server's hierarchy separator character appears elsewhere in
465
    /// the name, the server SHOULD create any superior hierarchical names
466
    /// that are needed for the CREATE command to be successfully
467
    /// completed.  In other words, an attempt to create "foo/bar/zap" on
468
    /// a server in which "/" is the hierarchy separator character SHOULD
469
    /// create foo/ and foo/bar/ if they do not already exist.
470
    ///
471
    /// If a new mailbox is created with the same name as a mailbox which
472
    /// was deleted, its unique identifiers MUST be greater than any
473
    /// unique identifiers used in the previous incarnation of the mailbox
474
    /// UNLESS the new incarnation has a different unique identifier
475
    /// validity value.  See the description of the UID command for more
476
    /// detail.
477
    ///
478
    ///   Note: The interpretation of this example depends on whether
479
    ///   "/" was returned as the hierarchy separator from LIST.  If
480
    ///   "/" is the hierarchy separator, a new level of hierarchy
481
    ///   named "owatagusiam" with a member called "blurdybloop" is
482
    ///   created.  Otherwise, two mailboxes at the same hierarchy
483
    ///   level are created.
484
    Create {
485
        /// Mailbox.
486
        mailbox: Mailbox<'a>,
487
    },
488

489
    /// 6.3.4.  DELETE Command
490
    ///
491
    /// Arguments:  mailbox name
492
    /// Responses:  no specific responses for this command
493
    /// Result:     OK - delete completed
494
    ///             NO - delete failure: can't delete mailbox with that name
495
    ///             BAD - command unknown or arguments invalid
496
    ///
497
    /// The DELETE command permanently removes the mailbox with the given
498
    /// name.  A tagged OK response is returned only if the mailbox has
499
    /// been deleted.  It is an error to attempt to delete INBOX or a
500
    /// mailbox name that does not exist.
501
    ///
502
    /// The DELETE command MUST NOT remove inferior hierarchical names.
503
    /// For example, if a mailbox "foo" has an inferior "foo.bar"
504
    /// (assuming "." is the hierarchy delimiter character), removing
505
    /// "foo" MUST NOT remove "foo.bar".  It is an error to attempt to
506
    /// delete a name that has inferior hierarchical names and also has
507
    /// the \Noselect mailbox name attribute (see the description of the
508
    /// LIST response for more details).
509
    ///
510
    /// It is permitted to delete a name that has inferior hierarchical
511
    /// names and does not have the \Noselect mailbox name attribute.  In
512
    /// this case, all messages in that mailbox are removed, and the name
513
    /// will acquire the \Noselect mailbox name attribute.
514
    ///
515
    /// The value of the highest-used unique identifier of the deleted
516
    /// mailbox MUST be preserved so that a new mailbox created with the
517
    /// same name will not reuse the identifiers of the former
518
    /// incarnation, UNLESS the new incarnation has a different unique
519
    /// identifier validity value.  See the description of the UID command
520
    /// for more detail.
521
    Delete {
522
        /// Mailbox.
523
        mailbox: Mailbox<'a>,
524
    },
525

526
    /// 6.3.5.  RENAME Command
527
    ///
528
    /// Arguments:  existing mailbox name
529
    ///             new mailbox name
530
    /// Responses:  no specific responses for this command
531
    /// Result:     OK - rename completed
532
    ///             NO - rename failure: can't rename mailbox with that name,
533
    ///                  can't rename to mailbox with that name
534
    ///             BAD - command unknown or arguments invalid
535
    ///
536
    /// The RENAME command changes the name of a mailbox.  A tagged OK
537
    /// response is returned only if the mailbox has been renamed.  It is
538
    /// an error to attempt to rename from a mailbox name that does not
539
    /// exist or to a mailbox name that already exists.  Any error in
540
    /// renaming will return a tagged NO response.
541
    ///
542
    /// If the name has inferior hierarchical names, then the inferior
543
    /// hierarchical names MUST also be renamed.  For example, a rename of
544
    /// "foo" to "zap" will rename "foo/bar" (assuming "/" is the
545
    /// hierarchy delimiter character) to "zap/bar".
546
    ///
547
    /// If the server's hierarchy separator character appears in the name,
548
    /// the server SHOULD create any superior hierarchical names that are
549
    /// needed for the RENAME command to complete successfully.  In other
550
    /// words, an attempt to rename "foo/bar/zap" to baz/rag/zowie on a
551
    /// server in which "/" is the hierarchy separator character SHOULD
552
    /// create baz/ and baz/rag/ if they do not already exist.
553
    ///
554
    /// The value of the highest-used unique identifier of the old mailbox
555
    /// name MUST be preserved so that a new mailbox created with the same
556
    /// name will not reuse the identifiers of the former incarnation,
557
    /// UNLESS the new incarnation has a different unique identifier
558
    /// validity value.  See the description of the UID command for more
559
    /// detail.
560
    ///
561
    /// Renaming INBOX is permitted, and has special behavior.  It moves
562
    /// all messages in INBOX to a new mailbox with the given name,
563
    /// leaving INBOX empty.  If the server implementation supports
564
    /// inferior hierarchical names of INBOX, these are unaffected by a
565
    /// rename of INBOX.
566
    Rename {
567
        /// Current name.
568
        from: Mailbox<'a>,
569
        /// New name.
570
        to: Mailbox<'a>,
571
    },
572

573
    /// ### 6.3.6.  SUBSCRIBE Command
574
    ///
575
    /// * Arguments:  mailbox
576
    /// * Responses:  no specific responses for this command
577
    /// * Result:
578
    ///   * OK - subscribe completed
579
    ///   * NO - subscribe failure: can't subscribe to that name
580
    ///   * BAD - command unknown or arguments invalid
581
    ///
582
    /// The SUBSCRIBE command adds the specified mailbox name to the
583
    /// server's set of "active" or "subscribed" mailboxes as returned by
584
    /// the LSUB command.  This command returns a tagged OK response only
585
    /// if the subscription is successful.
586
    ///
587
    /// A server MAY validate the mailbox argument to SUBSCRIBE to verify
588
    /// that it exists.  However, it MUST NOT unilaterally remove an
589
    /// existing mailbox name from the subscription list even if a mailbox
590
    /// by that name no longer exists.
591
    ///
592
    ///   Note: This requirement is because a server site can
593
    ///   choose to routinely remove a mailbox with a well-known
594
    ///   name (e.g., "system-alerts") after its contents expire,
595
    ///   with the intention of recreating it when new contents
596
    ///   are appropriate.
597
    Subscribe {
598
        /// Mailbox.
599
        mailbox: Mailbox<'a>,
600
    },
601

602
    /// 6.3.7.  UNSUBSCRIBE Command
603
    ///
604
    /// Arguments:  mailbox name
605
    /// Responses:  no specific responses for this command
606
    /// Result:     OK - unsubscribe completed
607
    ///             NO - unsubscribe failure: can't unsubscribe that name
608
    ///             BAD - command unknown or arguments invalid
609
    ///
610
    /// The UNSUBSCRIBE command removes the specified mailbox name from
611
    /// the server's set of "active" or "subscribed" mailboxes as returned
612
    /// by the LSUB command.  This command returns a tagged OK response
613
    /// only if the unsubscription is successful.
614
    Unsubscribe {
615
        /// Mailbox.
616
        mailbox: Mailbox<'a>,
617
    },
618

619
    /// ### 6.3.8.  LIST Command
620
    ///
621
    /// * Arguments:
622
    ///   * reference name
623
    ///   * mailbox name with possible wildcards
624
    /// * Responses:  untagged responses: LIST
625
    /// * Result:
626
    ///   * OK - list completed
627
    ///   * NO - list failure: can't list that reference or name
628
    ///   * BAD - command unknown or arguments invalid
629
    ///
630
    /// The LIST command returns a subset of names from the complete set
631
    /// of all names available to the client.  Zero or more untagged LIST
632
    /// replies are returned, containing the name attributes, hierarchy
633
    /// delimiter, and name; see the description of the LIST reply for
634
    /// more detail.
635
    ///
636
    /// The LIST command SHOULD return its data quickly, without undue
637
    /// delay.  For example, it SHOULD NOT go to excess trouble to
638
    /// calculate the \Marked or \Unmarked status or perform other
639
    /// processing; if each name requires 1 second of processing, then a
640
    /// list of 1200 names would take 20 minutes!
641
    ///
642
    /// An empty ("" string) reference name argument indicates that the
643
    /// mailbox name is interpreted as by SELECT.  The returned mailbox
644
    /// names MUST match the supplied mailbox name pattern.  A non-empty
645
    /// reference name argument is the name of a mailbox or a level of
646
    /// mailbox hierarchy, and indicates the context in which the mailbox
647
    /// name is interpreted.
648
    ///
649
    /// An empty ("" string) mailbox name argument is a special request to
650
    /// return the hierarchy delimiter and the root name of the name given
651
    /// in the reference.  The value returned as the root MAY be the empty
652
    /// string if the reference is non-rooted or is an empty string.  In
653
    /// all cases, a hierarchy delimiter (or NIL if there is no hierarchy)
654
    /// is returned.  This permits a client to get the hierarchy delimiter
655
    /// (or find out that the mailbox names are flat) even when no
656
    /// mailboxes by that name currently exist.
657
    ///
658
    /// The reference and mailbox name arguments are interpreted into a
659
    /// canonical form that represents an unambiguous left-to-right
660
    /// hierarchy.  The returned mailbox names will be in the interpreted
661
    /// form.
662
    ///
663
    ///   Note: The interpretation of the reference argument is
664
    ///   implementation-defined.  It depends upon whether the
665
    ///   server implementation has a concept of the "current
666
    ///   working directory" and leading "break out characters",
667
    ///   which override the current working directory.
668
    ///
669
    ///   For example, on a server which exports a UNIX or NT
670
    ///   filesystem, the reference argument contains the current
671
    ///   working directory, and the mailbox name argument would
672
    ///   contain the name as interpreted in the current working
673
    ///   directory.
674
    ///
675
    /// If a server implementation has no concept of break out
676
    /// characters, the canonical form is normally the reference
677
    /// name appended with the mailbox name.  Note that if the
678
    /// server implements the namespace convention (section
679
    /// 5.1.2), "#" is a break out character and must be treated
680
    /// as such.
681
    ///
682
    /// If the reference argument is not a level of mailbox
683
    /// hierarchy (that is, it is a \NoInferiors name), and/or
684
    /// the reference argument does not end with the hierarchy
685
    /// delimiter, it is implementation-dependent how this is
686
    /// interpreted.  For example, a reference of "foo/bar" and
687
    /// mailbox name of "rag/baz" could be interpreted as
688
    /// "foo/bar/rag/baz", "foo/barrag/baz", or "foo/rag/baz".
689
    /// A client SHOULD NOT use such a reference argument except
690
    /// at the explicit request of the user.  A hierarchical
691
    /// browser MUST NOT make any assumptions about server
692
    /// interpretation of the reference unless the reference is
693
    /// a level of mailbox hierarchy AND ends with the hierarchy
694
    /// delimiter.
695
    ///
696
    /// Any part of the reference argument that is included in the
697
    /// interpreted form SHOULD prefix the interpreted form.  It SHOULD
698
    /// also be in the same form as the reference name argument.  This
699
    /// rule permits the client to determine if the returned mailbox name
700
    /// is in the context of the reference argument, or if something about
701
    /// the mailbox argument overrode the reference argument.  Without
702
    /// this rule, the client would have to have knowledge of the server's
703
    /// naming semantics including what characters are "breakouts" that
704
    /// override a naming context.
705
    ///
706
    ///   For example, here are some examples of how references
707
    ///   and mailbox names might be interpreted on a UNIX-based
708
    ///   server:
709
    ///
710
    /// ```text
711
    /// Reference     Mailbox Name  Interpretation
712
    /// ------------  ------------  --------------
713
    /// ~smith/Mail/  foo.*         ~smith/Mail/foo.*
714
    /// archive/      %             archive/%
715
    /// #news.        comp.mail.*   #news.comp.mail.*
716
    /// ~smith/Mail/  /usr/doc/foo  /usr/doc/foo
717
    /// archive/      ~fred/Mail/*  ~fred/Mail/*
718
    /// ```
719
    ///
720
    ///   The first three examples demonstrate interpretations in
721
    ///   the context of the reference argument.  Note that
722
    ///   "~smith/Mail" SHOULD NOT be transformed into something
723
    ///   like "/u2/users/smith/Mail", or it would be impossible
724
    ///   for the client to determine that the interpretation was
725
    ///   in the context of the reference.
726
    ///
727
    /// The character "*" is a wildcard, and matches zero or more
728
    /// characters at this position.  The character "%" is similar to "*",
729
    /// but it does not match a hierarchy delimiter.  If the "%" wildcard
730
    /// is the last character of a mailbox name argument, matching levels
731
    /// of hierarchy are also returned.  If these levels of hierarchy are
732
    /// not also selectable mailboxes, they are returned with the
733
    /// \Noselect mailbox name attribute (see the description of the LIST
734
    /// response for more details).
735
    ///
736
    /// Server implementations are permitted to "hide" otherwise
737
    /// accessible mailboxes from the wildcard characters, by preventing
738
    /// certain characters or names from matching a wildcard in certain
739
    /// situations.  For example, a UNIX-based server might restrict the
740
    /// interpretation of "*" so that an initial "/" character does not
741
    /// match.
742
    ///
743
    /// The special name INBOX is included in the output from LIST, if
744
    /// INBOX is supported by this server for this user and if the
745
    /// uppercase string "INBOX" matches the interpreted reference and
746
    /// mailbox name arguments with wildcards as described above.  The
747
    /// criteria for omitting INBOX is whether SELECT INBOX will return
748
    /// failure; it is not relevant whether the user's real INBOX resides
749
    /// on this or some other server.
750
    List {
751
        /// Reference.
752
        reference: Mailbox<'a>,
753
        /// Mailbox (wildcard).
754
        mailbox_wildcard: ListMailbox<'a>,
755
    },
756

757
    /// ### 6.3.9.  LSUB Command
758
    ///
759
    /// * Arguments:
760
    ///   * reference name
761
    ///   * mailbox name with possible wildcards
762
    /// * Responses:  untagged responses: LSUB
763
    /// * Result:
764
    ///   * OK - lsub completed
765
    ///   * NO - lsub failure: can't list that reference or name
766
    ///   * BAD - command unknown or arguments invalid
767
    ///
768
    /// The LSUB command returns a subset of names from the set of names
769
    /// that the user has declared as being "active" or "subscribed".
770
    /// Zero or more untagged LSUB replies are returned.  The arguments to
771
    /// LSUB are in the same form as those for LIST.
772
    ///
773
    /// The returned untagged LSUB response MAY contain different mailbox
774
    /// flags from a LIST untagged response.  If this should happen, the
775
    /// flags in the untagged LIST are considered more authoritative.
776
    ///
777
    /// A special situation occurs when using LSUB with the % wildcard.
778
    /// Consider what happens if "foo/bar" (with a hierarchy delimiter of
779
    /// "/") is subscribed but "foo" is not.  A "%" wildcard to LSUB must
780
    /// return foo, not foo/bar, in the LSUB response, and it MUST be
781
    /// flagged with the \Noselect attribute.
782
    ///
783
    /// The server MUST NOT unilaterally remove an existing mailbox name
784
    /// from the subscription list even if a mailbox by that name no
785
    /// longer exists.
786
    Lsub {
787
        /// Reference.
788
        reference: Mailbox<'a>,
789
        /// Mailbox (wildcard).
790
        mailbox_wildcard: ListMailbox<'a>,
791
    },
792

793
    /// ### 6.3.10. STATUS Command
794
    ///
795
    /// * Arguments:
796
    ///   * mailbox name
797
    ///   * status data item names
798
    /// * Responses:  untagged responses: STATUS
799
    /// * Result:
800
    ///   * OK - status completed
801
    ///   * NO - status failure: no status for that name
802
    ///   * BAD - command unknown or arguments invalid
803
    ///
804
    /// The STATUS command requests the status of the indicated mailbox.
805
    /// It does not change the currently selected mailbox, nor does it
806
    /// affect the state of any messages in the queried mailbox (in
807
    /// particular, STATUS MUST NOT cause messages to lose the \Recent
808
    /// flag).
809
    ///
810
    /// The STATUS command provides an alternative to opening a second
811
    /// IMAP4rev1 connection and doing an EXAMINE command on a mailbox to
812
    /// query that mailbox's status without deselecting the current
813
    /// mailbox in the first IMAP4rev1 connection.
814
    ///
815
    /// Unlike the LIST command, the STATUS command is not guaranteed to
816
    /// be fast in its response.  Under certain circumstances, it can be
817
    /// quite slow.  In some implementations, the server is obliged to
818
    /// open the mailbox read-only internally to obtain certain status
819
    /// information.  Also unlike the LIST command, the STATUS command
820
    /// does not accept wildcards.
821
    ///
822
    ///   Note: The STATUS command is intended to access the
823
    ///   status of mailboxes other than the currently selected
824
    ///   mailbox.  Because the STATUS command can cause the
825
    ///   mailbox to be opened internally, and because this
826
    ///   information is available by other means on the selected
827
    ///   mailbox, the STATUS command SHOULD NOT be used on the
828
    ///   currently selected mailbox.
829
    ///
830
    ///   The STATUS command MUST NOT be used as a "check for new
831
    ///   messages in the selected mailbox" operation (refer to
832
    ///   sections 7, 7.3.1, and 7.3.2 for more information about
833
    ///   the proper method for new message checking).
834
    ///
835
    ///   Because the STATUS command is not guaranteed to be fast
836
    ///   in its results, clients SHOULD NOT expect to be able to
837
    ///   issue many consecutive STATUS commands and obtain
838
    ///   reasonable performance.
839
    Status {
840
        /// Mailbox.
841
        mailbox: Mailbox<'a>,
842
        /// Status data items.
843
        item_names: Cow<'a, [StatusDataItemName]>,
844
    },
845

846
    /// 6.3.11. APPEND Command
847
    ///
848
    /// Arguments:  mailbox name
849
    ///             OPTIONAL flag parenthesized list
850
    ///             OPTIONAL date/time string
851
    ///             message literal
852
    /// Responses:  no specific responses for this command
853
    /// Result:     OK - append completed
854
    ///             NO - append error: can't append to that mailbox, error
855
    ///                  in flags or date/time or message text
856
    ///             BAD - command unknown or arguments invalid
857
    ///
858
    /// The APPEND command appends the literal argument as a new message
859
    /// to the end of the specified destination mailbox.  This argument
860
    /// SHOULD be in the format of an [RFC-2822] message.  8-bit
861
    /// characters are permitted in the message.  A server implementation
862
    /// that is unable to preserve 8-bit data properly MUST be able to
863
    /// reversibly convert 8-bit APPEND data to 7-bit using a [MIME-IMB]
864
    /// content transfer encoding.
865
    ///
866
    ///   Note: There MAY be exceptions, e.g., draft messages, in
867
    ///   which required [RFC-2822] header lines are omitted in
868
    ///   the message literal argument to APPEND.  The full
869
    ///   implications of doing so MUST be understood and
870
    ///   carefully weighed.
871
    ///
872
    /// If a flag parenthesized list is specified, the flags SHOULD be set
873
    /// in the resulting message; otherwise, the flag list of the
874
    /// resulting message is set to empty by default.  In either case, the
875
    /// Recent flag is also set.
876
    ///
877
    /// If a date-time is specified, the internal date SHOULD be set in
878
    /// the resulting message; otherwise, the internal date of the
879
    /// resulting message is set to the current date and time by default.
880
    ///
881
    /// If the append is unsuccessful for any reason, the mailbox MUST be
882
    /// restored to its state before the APPEND attempt; no partial
883
    /// appending is permitted.
884
    ///
885
    /// If the destination mailbox does not exist, a server MUST return an
886
    /// error, and MUST NOT automatically create the mailbox.  Unless it
887
    /// is certain that the destination mailbox can not be created, the
888
    /// server MUST send the response code "\[TRYCREATE\]" as the prefix of
889
    /// the text of the tagged NO response.  This gives a hint to the
890
    /// client that it can attempt a CREATE command and retry the APPEND
891
    /// if the CREATE is successful.
892
    ///
893
    /// If the mailbox is currently selected, the normal new message
894
    /// actions SHOULD occur.  Specifically, the server SHOULD notify the
895
    /// client immediately via an untagged EXISTS response.  If the server
896
    /// does not do so, the client MAY issue a NOOP command (or failing
897
    /// that, a CHECK command) after one or more APPEND commands.
898
    ///
899
    ///   Note: The APPEND command is not used for message delivery,
900
    ///   because it does not provide a mechanism to transfer \[SMTP\]
901
    ///   envelope information.
902
    Append {
903
        /// Mailbox.
904
        mailbox: Mailbox<'a>,
905
        /// Flags.
906
        flags: Vec<Flag<'a>>,
907
        /// Datetime.
908
        date: Option<DateTime>,
909
        /// Message to append.
910
        ///
911
        /// <div class="warning">
912
        /// Use [`LiteralOrLiteral8::Literal8`] only when the server advertised [`Capability::Binary`](crate::response::Capability::Binary).
913
        /// </div>
914
        message: LiteralOrLiteral8<'a>,
915
    },
916

917
    // ----- Selected State (https://tools.ietf.org/html/rfc3501#section-6.4) -----
918
    /// ### 6.4.1.  CHECK Command
919
    ///
920
    /// * Arguments:  none
921
    /// * Responses:  no specific responses for this command
922
    /// * Result:
923
    ///   * OK - check completed
924
    ///   * BAD - command unknown or arguments invalid
925
    ///
926
    /// The CHECK command requests a checkpoint of the currently selected
927
    /// mailbox.  A checkpoint refers to any implementation-dependent
928
    /// housekeeping associated with the mailbox (e.g., resolving the
929
    /// server's in-memory state of the mailbox with the state on its
930
    /// disk) that is not normally executed as part of each command.  A
931
    /// checkpoint MAY take a non-instantaneous amount of real time to
932
    /// complete.  If a server implementation has no such housekeeping
933
    /// considerations, CHECK is equivalent to NOOP.
934
    ///
935
    /// There is no guarantee that an EXISTS untagged response will happen
936
    /// as a result of CHECK.  NOOP, not CHECK, SHOULD be used for new
937
    /// message polling.
938
    Check,
939

940
    /// ### 6.4.2.  CLOSE Command
941
    ///
942
    /// * Arguments: none
943
    /// * Responses: no specific responses for this command
944
    /// * Result:
945
    ///   * OK - close completed, now in authenticated state
946
    ///   * BAD - command unknown or arguments invalid
947
    ///
948
    /// The CLOSE command permanently removes all messages that have the
949
    /// \Deleted flag set from the currently selected mailbox, and returns
950
    /// to the authenticated state from the selected state.  No untagged
951
    /// EXPUNGE responses are sent.
952
    ///
953
    /// No messages are removed, and no error is given, if the mailbox is
954
    /// selected by an EXAMINE command or is otherwise selected read-only.
955
    ///
956
    /// Even if a mailbox is selected, a SELECT, EXAMINE, or LOGOUT
957
    /// command MAY be issued without previously issuing a CLOSE command.
958
    /// The SELECT, EXAMINE, and LOGOUT commands implicitly close the
959
    /// currently selected mailbox without doing an expunge.  However,
960
    /// when many messages are deleted, a CLOSE-LOGOUT or CLOSE-SELECT
961
    /// sequence is considerably faster than an EXPUNGE-LOGOUT or
962
    /// EXPUNGE-SELECT because no untagged EXPUNGE responses (which the
963
    /// client would probably ignore) are sent.
964
    Close,
965

966
    /// 6.4.3.  EXPUNGE Command
967
    ///
968
    /// Arguments: none
969
    /// Responses: untagged responses: EXPUNGE
970
    /// Result:    OK - expunge completed
971
    ///            NO - expunge failure: can't expunge (e.g., permission denied)
972
    ///            BAD - command unknown or arguments invalid
973
    ///
974
    /// The EXPUNGE command permanently removes all messages that have the
975
    /// \Deleted flag set from the currently selected mailbox.  Before
976
    /// returning an OK to the client, an untagged EXPUNGE response is
977
    /// sent for each message that is removed.
978
    Expunge,
979

980
    /// 2.1.  UID EXPUNGE Command (RFC 4315)
981
    ///
982
    /// Arguments: sequence set
983
    /// Data:      untagged responses: EXPUNGE
984
    /// Result:    OK - expunge completed
985
    ///            NO - expunge failure (e.g., permission denied)
986
    ///            BAD - command unknown or arguments invalid
987
    ///
988
    /// The UID EXPUNGE command permanently removes all messages that both
989
    /// have the \Deleted flag set and have a UID that is included in the
990
    /// specified sequence set from the currently selected mailbox.  If a
991
    /// message either does not have the \Deleted flag set or has a UID
992
    /// that is not included in the specified sequence set, it is not
993
    /// affected.
994
    ///
995
    /// This command is particularly useful for disconnected use clients.
996
    /// By using UID EXPUNGE instead of EXPUNGE when resynchronizing with
997
    /// the server, the client can ensure that it does not inadvertantly
998
    /// remove any messages that have been marked as \Deleted by other
999
    /// clients between the time that the client was last connected and
1000
    /// the time the client resynchronizes.
1001
    ///
1002
    /// If the server does not support the UIDPLUS capability, the client
1003
    /// should fall back to using the STORE command to temporarily remove
1004
    /// the \Deleted flag from messages it does not want to remove, then
1005
    /// issuing the EXPUNGE command.  Finally, the client should use the
1006
    /// STORE command to restore the \Deleted flag on the messages in
1007
    /// which it was temporarily removed.
1008
    ///
1009
    /// Alternatively, the client may fall back to using just the EXPUNGE
1010
    /// command, risking the unintended removal of some messages.
1011
    ExpungeUid { sequence_set: SequenceSet },
1012

1013
    /// ### 6.4.4.  SEARCH Command
1014
    ///
1015
    /// * Arguments:
1016
    ///   * OPTIONAL \[CHARSET\] specification
1017
    ///   * searching criteria (one or more)
1018
    /// * Responses:  REQUIRED untagged response: SEARCH
1019
    /// * Result:
1020
    ///   * OK - search completed
1021
    ///   * NO - search error: can't search that \[CHARSET\] or criteria
1022
    ///   * BAD - command unknown or arguments invalid
1023
    ///
1024
    /// The SEARCH command searches the mailbox for messages that match
1025
    /// the given searching criteria.  Searching criteria consist of one
1026
    /// or more search keys.  The untagged SEARCH response from the server
1027
    /// contains a listing of message sequence numbers corresponding to
1028
    /// those messages that match the searching criteria.
1029
    ///
1030
    /// When multiple keys are specified, the result is the intersection
1031
    /// (AND function) of all the messages that match those keys.  For
1032
    /// example, the criteria DELETED FROM "SMITH" SINCE 1-Feb-1994 refers
1033
    /// to all deleted messages from Smith that were placed in the mailbox
1034
    /// since February 1, 1994.  A search key can also be a parenthesized
1035
    /// list of one or more search keys (e.g., for use with the OR and NOT
1036
    /// keys).
1037
    ///
1038
    /// Server implementations MAY exclude [MIME-IMB] body parts with
1039
    /// terminal content media types other than TEXT and MESSAGE from
1040
    /// consideration in SEARCH matching.
1041
    ///
1042
    /// The OPTIONAL \[CHARSET\] specification consists of the word
1043
    /// "CHARSET" followed by a registered \[CHARSET\].  It indicates the
1044
    /// \[CHARSET\] of the strings that appear in the search criteria.
1045
    /// [MIME-IMB] content transfer encodings, and [MIME-HDRS] strings in
1046
    /// [RFC-2822]/[MIME-IMB] headers, MUST be decoded before comparing
1047
    /// text in a \[CHARSET\] other than US-ASCII.  US-ASCII MUST be
1048
    /// supported; other \[CHARSET\]s MAY be supported.
1049
    ///
1050
    /// If the server does not support the specified \[CHARSET\], it MUST
1051
    /// return a tagged NO response (not a BAD).  This response SHOULD
1052
    /// contain the BADCHARSET response code, which MAY list the
1053
    /// \[CHARSET\]s supported by the server.
1054
    ///
1055
    /// In all search keys that use strings, a message matches the key if
1056
    /// the string is a substring of the field.  The matching is
1057
    /// case-insensitive.
1058
    ///
1059
    /// See [SearchKey] enum.
1060
    ///
1061
    /// Note: Since this document is restricted to 7-bit ASCII
1062
    /// text, it is not possible to show actual UTF-8 data.  The
1063
    /// "XXXXXX" is a placeholder for what would be 6 octets of
1064
    /// 8-bit data in an actual transaction.
1065
    Search {
1066
        /// Charset.
1067
        charset: Option<Charset<'a>>,
1068
        /// Criteria.
1069
        criteria: Vec1<SearchKey<'a>>,
1070
        /// Use UID variant.
1071
        uid: bool,
1072
    },
1073

1074
    /// SORT command.
1075
    ///
1076
    /// The SORT command is a variant of SEARCH with sorting semantics for the results.
1077
    ///
1078
    /// Data:
1079
    /// * untagged responses: SORT
1080
    ///
1081
    /// Result:
1082
    /// * OK - sort completed
1083
    /// * NO - sort error: can't sort that charset or criteria
1084
    /// * BAD - command unknown or arguments invalid
1085
    ///
1086
    /// <div class="warning">
1087
    /// This extension must only be used when the server advertised support for it sending the SORT capability.
1088
    /// </div>
1089
    Sort {
1090
        /// Sort criteria.
1091
        sort_criteria: Vec1<SortCriterion>,
1092
        /// Charset.
1093
        charset: Charset<'a>,
1094
        /// Search criteria.
1095
        search_criteria: Vec1<SearchKey<'a>>,
1096
        /// Use UID variant.
1097
        uid: bool,
1098
    },
1099

1100
    /// THREAD command.
1101
    ///
1102
    /// The THREAD command is a variant of SEARCH with threading semantics for the results.
1103
    ///
1104
    /// Data:
1105
    /// * untagged responses: THREAD
1106
    ///
1107
    /// Result:
1108
    /// * OK - thread completed
1109
    /// * NO - thread error: can't thread that charset or criteria
1110
    /// * BAD - command unknown or arguments invalid
1111
    ///
1112
    /// <div class="warning">
1113
    /// This extension must only be used when the server advertised support for it sending the THREAD capability.
1114
    /// </div>
1115
    Thread {
1116
        /// Threading algorithm.
1117
        algorithm: ThreadingAlgorithm<'a>,
1118
        /// Charset.
1119
        charset: Charset<'a>,
1120
        /// Search criteria.
1121
        search_criteria: Vec1<SearchKey<'a>>,
1122
        /// Use UID variant.
1123
        uid: bool,
1124
    },
1125

1126
    /// ### 6.4.5.  FETCH Command
1127
    ///
1128
    /// * Arguments:
1129
    ///   * sequence set
1130
    ///   * message data item names or macro
1131
    /// * Responses:  untagged responses: FETCH
1132
    /// * Result:
1133
    ///   * OK - fetch completed
1134
    ///   * NO - fetch error: can't fetch that data
1135
    ///   * BAD - command unknown or arguments invalid
1136
    ///
1137
    /// The FETCH command retrieves data associated with a message in the
1138
    /// mailbox.  The data items to be fetched can be either a single atom
1139
    /// or a parenthesized list.
1140
    ///
1141
    /// Most data items, identified in the formal syntax under the
1142
    /// msg-att-static rule, are static and MUST NOT change for any
1143
    /// particular message.  Other data items, identified in the formal
1144
    /// syntax under the msg-att-dynamic rule, MAY change, either as a
1145
    /// result of a STORE command or due to external events.
1146
    ///
1147
    ///   For example, if a client receives an ENVELOPE for a
1148
    ///   message when it already knows the envelope, it can
1149
    ///   safely ignore the newly transmitted envelope.
1150
    Fetch {
1151
        /// Set of messages.
1152
        sequence_set: SequenceSet,
1153
        /// Message data items (or a macro).
1154
        macro_or_item_names: MacroOrMessageDataItemNames<'a>,
1155
        /// Use UID variant.
1156
        uid: bool,
1157
        #[cfg(feature = "ext_condstore_qresync")]
1158
        modifiers: Vec<FetchModifier>,
1159
    },
1160

1161
    /// ### 6.4.6.  STORE Command
1162
    ///
1163
    /// * Arguments:
1164
    ///   * sequence set
1165
    ///   * message data item name
1166
    ///   * value for message data item
1167
    /// * Responses:  untagged responses: FETCH
1168
    /// * Result:
1169
    ///   * OK - store completed
1170
    ///   * NO - store error: can't store that data
1171
    ///   * BAD - command unknown or arguments invalid
1172
    ///
1173
    /// The STORE command alters data associated with a message in the
1174
    /// mailbox.  Normally, STORE will return the updated value of the
1175
    /// data with an untagged FETCH response.  A suffix of ".SILENT" in
1176
    /// the data item name prevents the untagged FETCH, and the server
1177
    /// SHOULD assume that the client has determined the updated value
1178
    /// itself or does not care about the updated value.
1179
    ///
1180
    ///   Note: Regardless of whether or not the ".SILENT" suffix
1181
    ///   was used, the server SHOULD send an untagged FETCH
1182
    ///   response if a change to a message's flags from an
1183
    ///   external source is observed.  The intent is that the
1184
    ///   status of the flags is determinate without a race
1185
    ///   condition.
1186
    ///
1187
    /// The currently defined data items that can be stored are:
1188
    ///
1189
    /// FLAGS \<flag list\>
1190
    ///    Replace the flags for the message (other than \Recent) with the
1191
    ///    argument.  The new value of the flags is returned as if a FETCH
1192
    ///    of those flags was done.
1193
    ///
1194
    /// FLAGS.SILENT \<flag list\>
1195
    ///    Equivalent to FLAGS, but without returning a new value.
1196
    ///
1197
    /// +FLAGS \<flag list\>
1198
    ///    Add the argument to the flags for the message.  The new value
1199
    ///    of the flags is returned as if a FETCH of those flags was done.
1200
    ///
1201
    /// +FLAGS.SILENT \<flag list\>
1202
    ///    Equivalent to +FLAGS, but without returning a new value.
1203
    ///
1204
    /// -FLAGS \<flag list\>
1205
    ///    Remove the argument from the flags for the message.  The new
1206
    ///    value of the flags is returned as if a FETCH of those flags was
1207
    ///    done.
1208
    ///
1209
    /// -FLAGS.SILENT \<flag list\>
1210
    ///    Equivalent to -FLAGS, but without returning a new value.
1211
    Store {
1212
        /// Set of messages.
1213
        sequence_set: SequenceSet,
1214
        /// Kind of storage, i.e., replace, add, or remove.
1215
        kind: StoreType,
1216
        /// Kind of response, i.e., answer or silent.
1217
        response: StoreResponse,
1218
        /// Flags.
1219
        flags: Vec<Flag<'a>>, // FIXME(misuse): must not accept "\*" or "\Recent"
1220
        /// Use UID variant.
1221
        uid: bool,
1222
        /// --- Modifiers ---
1223
        #[cfg(feature = "ext_condstore_qresync")]
1224
        modifiers: Vec<StoreModifier>,
1225
    },
1226

1227
    /// 6.4.7.  COPY Command
1228
    ///
1229
    /// Arguments:  sequence set
1230
    ///             mailbox name
1231
    /// Responses:  no specific responses for this command
1232
    /// Result:     OK - copy completed
1233
    ///             NO - copy error: can't copy those messages or to that
1234
    ///                  name
1235
    ///             BAD - command unknown or arguments invalid
1236
    ///
1237
    /// The COPY command copies the specified message(s) to the end of the
1238
    /// specified destination mailbox.  The flags and internal date of the
1239
    /// message(s) SHOULD be preserved, and the Recent flag SHOULD be set,
1240
    /// in the copy.
1241
    ///
1242
    /// If the destination mailbox does not exist, a server SHOULD return
1243
    /// an error.  It SHOULD NOT automatically create the mailbox.  Unless
1244
    /// it is certain that the destination mailbox can not be created, the
1245
    /// server MUST send the response code "\[TRYCREATE\]" as the prefix of
1246
    /// the text of the tagged NO response.  This gives a hint to the
1247
    /// client that it can attempt a CREATE command and retry the COPY if
1248
    /// the CREATE is successful.
1249
    ///
1250
    /// If the COPY command is unsuccessful for any reason, server
1251
    /// implementations MUST restore the destination mailbox to its state
1252
    /// before the COPY attempt.
1253
    Copy {
1254
        /// Set of messages.
1255
        sequence_set: SequenceSet,
1256
        /// Destination mailbox.
1257
        mailbox: Mailbox<'a>,
1258
        /// Use UID variant.
1259
        uid: bool,
1260
    },
1261

1262
    // The UID mechanism was inlined into copy, fetch, store, and search.
1263
    // as an additional parameter.
1264
    //
1265
    // ### 6.4.8.  UID Command
1266
    //
1267
    // * Arguments:
1268
    //   * command name
1269
    //   * command arguments
1270
    // * Responses:  untagged responses: FETCH, SEARCH
1271
    // * Result:
1272
    //   * OK - UID command completed
1273
    //   * NO - UID command error
1274
    //   * BAD - command unknown or arguments invalid
1275
    //
1276
    // The UID command has two forms.  In the first form, it takes as its
1277
    // arguments a COPY, FETCH, or STORE command with arguments
1278
    // appropriate for the associated command.  However, the numbers in
1279
    // the sequence set argument are unique identifiers instead of
1280
    // message sequence numbers.  Sequence set ranges are permitted, but
1281
    // there is no guarantee that unique identifiers will be contiguous.
1282
    //
1283
    // A non-existent unique identifier is ignored without any error
1284
    // message generated.  Thus, it is possible for a UID FETCH command
1285
    // to return an OK without any data or a UID COPY or UID STORE to
1286
    // return an OK without performing any operations.
1287
    //
1288
    // In the second form, the UID command takes a SEARCH command with
1289
    // SEARCH command arguments.  The interpretation of the arguments is
1290
    // the same as with SEARCH; however, the numbers returned in a SEARCH
1291
    // response for a UID SEARCH command are unique identifiers instead
1292
    // of message sequence numbers.  For example, the command UID SEARCH
1293
    // 1:100 UID 443:557 returns the unique identifiers corresponding to
1294
    // the intersection of two sequence sets, the message sequence number
1295
    // range 1:100 and the UID range 443:557.
1296
    //
1297
    //   Note: in the above example, the UID range 443:557
1298
    //   appears.  The same comment about a non-existent unique
1299
    //   identifier being ignored without any error message also
1300
    //   applies here.  Hence, even if neither UID 443 or 557
1301
    //   exist, this range is valid and would include an existing
1302
    //   UID 495.
1303
    //
1304
    //   Also note that a UID range of 559:* always includes the
1305
    //   UID of the last message in the mailbox, even if 559 is
1306
    //   higher than any assigned UID value.  This is because the
1307
    //   contents of a range are independent of the order of the
1308
    //   range endpoints.  Thus, any UID range with * as one of
1309
    //   the endpoints indicates at least one message (the
1310
    //   message with the highest numbered UID), unless the
1311
    //   mailbox is empty.
1312
    //
1313
    //   The number after the "*" in an untagged FETCH response is always a
1314
    //   message sequence number, not a unique identifier, even for a UID
1315
    //   command response.  However, server implementations MUST implicitly
1316
    //   include the UID message data item as part of any FETCH response
1317
    //   caused by a UID command, regardless of whether a UID was specified
1318
    //   as a message data item to the FETCH.
1319
    //
1320
    //   Note: The rule about including the UID message data item as part
1321
    //   of a FETCH response primarily applies to the UID FETCH and UID
1322
    //   STORE commands, including a UID FETCH command that does not
1323
    //   include UID as a message data item.  Although it is unlikely that
1324
    //   the other UID commands will cause an untagged FETCH, this rule
1325
    //   applies to these commands as well.
1326

1327
    // ----- Experimental/Expansion (https://tools.ietf.org/html/rfc3501#section-6.5) -----
1328

1329
    // ### 6.5.1.  X<atom> Command
1330
    //
1331
    // * Arguments:  implementation defined
1332
    // * Responses:  implementation defined
1333
    // * Result:
1334
    //   * OK - command completed
1335
    //   * NO - failure
1336
    //   * BAD - command unknown or arguments invalid
1337
    //
1338
    // Any command prefixed with an X is an experimental command.
1339
    // Commands which are not part of this specification, a standard or
1340
    // standards-track revision of this specification, or an
1341
    // IESG-approved experimental protocol, MUST use the X prefix.
1342
    //
1343
    // Any added untagged responses issued by an experimental command
1344
    // MUST also be prefixed with an X.  Server implementations MUST NOT
1345
    // send any such untagged responses, unless the client requested it
1346
    // by issuing the associated experimental command.
1347
    //X,
1348
    /// IDLE command.
1349
    ///
1350
    /// <div class="warning">
1351
    /// This extension must only be used when the server advertised support for it sending the IDLE capability.
1352
    /// </div>
1353
    Idle,
1354

1355
    /// ENABLE command.
1356
    ///
1357
    /// <div class="warning">
1358
    /// This extension must only be used when the server advertised support for it sending the ENABLE capability.
1359
    /// </div>
1360
    Enable {
1361
        /// Capabilities to enable.
1362
        capabilities: Vec1<CapabilityEnable<'a>>,
1363
    },
1364

1365
    /// COMPRESS command.
1366
    ///
1367
    /// <div class="warning">
1368
    /// This extension must only be used when the server advertised support for it sending the COMPRESS capability.
1369
    /// </div>
1370
    Compress {
1371
        /// Compression algorithm.
1372
        algorithm: CompressionAlgorithm,
1373
    },
1374

1375
    /// Takes the name of a quota root and returns the quota root's resource usage and limits in an untagged QUOTA response.
1376
    ///
1377
    /// Arguments:
1378
    /// * quota root
1379
    ///
1380
    /// Responses:
1381
    /// * REQUIRED untagged responses: QUOTA
1382
    ///
1383
    /// Result:
1384
    /// * OK - getquota completed
1385
    /// * NO - getquota error: no such quota root, permission denied
1386
    /// * BAD - command unknown or arguments invalid
1387
    ///
1388
    /// # Example (IMAP)
1389
    ///
1390
    /// ```imap
1391
    /// S: * CAPABILITY [...] QUOTA QUOTA=RES-STORAGE [...]
1392
    /// [...]
1393
    /// C: G0001 GETQUOTA "!partition/sda4"
1394
    /// S: * QUOTA "!partition/sda4" (STORAGE 104 10923847)
1395
    /// S: G0001 OK Getquota complete
1396
    /// ```
1397
    ///
1398
    /// <div class="warning">
1399
    /// This extension must only be used when the server advertised support for it sending the QUOTA* capability.
1400
    /// </div>
1401
    GetQuota {
1402
        /// Name of quota root.
1403
        root: AString<'a>,
1404
    },
1405

1406
    /// Takes a mailbox name and returns the list of quota roots for the mailbox in an untagged QUOTAROOT response.
1407
    /// For each listed quota root, it also returns the quota root's resource usage and limits in an untagged QUOTA response.
1408
    ///
1409
    /// Arguments:
1410
    /// * mailbox name
1411
    ///
1412
    /// Responses:
1413
    /// * REQUIRED untagged responses: QUOTAROOT, QUOTA
1414
    ///
1415
    /// Result:
1416
    /// * OK - getquotaroot completed
1417
    /// * NO - getquotaroot error: permission denied
1418
    /// * BAD - command unknown or arguments invalid
1419
    ///
1420
    /// Note that the mailbox name parameter doesn't have to reference an existing mailbox.
1421
    /// This can be handy in order to determine which quota root would apply to a mailbox when it gets created
1422
    ///
1423
    /// # Example (IMAP)
1424
    ///
1425
    /// ```imap
1426
    /// S: * CAPABILITY [...] QUOTA QUOTA=RES-STORAGE QUOTA=RES-MESSAGE
1427
    /// [...]
1428
    /// C: G0002 GETQUOTAROOT INBOX
1429
    /// S: * QUOTAROOT INBOX "#user/alice" "!partition/sda4"
1430
    /// S: * QUOTA "#user/alice" (MESSAGE 42 1000)
1431
    /// S: * QUOTA "!partition/sda4" (STORAGE 104 10923847)
1432
    /// S: G0002 OK Getquotaroot complete
1433
    /// ```
1434
    ///
1435
    /// <div class="warning">
1436
    /// This extension must only be used when the server advertised support for it sending the QUOTA* capability.
1437
    /// </div>
1438
    GetQuotaRoot {
1439
        /// Name of mailbox.
1440
        mailbox: Mailbox<'a>,
1441
    },
1442

1443
    /// Changes the mailbox quota root resource limits to the specified limits.
1444
    ///
1445
    /// Arguments:
1446
    /// * quota root list of resource limits
1447
    ///
1448
    /// Responses:
1449
    /// * untagged responses: QUOTA
1450
    ///
1451
    /// Result:
1452
    ///
1453
    /// * OK - setquota completed
1454
    /// * NO - setquota error: can't set that data
1455
    /// * BAD - command unknown or arguments invalid
1456
    ///
1457
    /// Note: requires the server to advertise the "QUOTASET" capability.
1458
    ///
1459
    /// # Example (IMAP)
1460
    ///
1461
    /// ```imap
1462
    /// S: * CAPABILITY [...] QUOTA QUOTASET QUOTA=RES-STORAGE QUOTA=RES-
1463
    /// MESSAGE [...]
1464
    /// [...]
1465
    /// C: S0000 GETQUOTA "#user/alice"
1466
    /// S: * QUOTA "#user/alice" (STORAGE 54 111 MESSAGE 42 1000)
1467
    /// S: S0000 OK Getquota completed
1468
    /// C: S0001 SETQUOTA "#user/alice" (STORAGE 510)
1469
    /// S: * QUOTA "#user/alice" (STORAGE 58 512)
1470
    /// // The server has rounded the STORAGE quota limit requested to
1471
    /// the nearest 512 blocks of 1024 octets; otherwise, another client
1472
    /// has performed a near-simultaneous SETQUOTA using a limit of 512.
1473
    /// S: S0001 OK Rounded quota
1474
    /// C: S0002 SETQUOTA "!partition/sda4" (STORAGE 99999999)
1475
    /// S: * QUOTA "!partition/sda4" (STORAGE 104 10923847)
1476
    /// // The server has not changed the quota, since this is a
1477
    /// filesystem limit, and it cannot be changed. The QUOTA
1478
    /// response here is entirely optional.
1479
    /// S: S0002 NO Cannot change system limit
1480
    /// ```
1481
    ///
1482
    /// <div class="warning">
1483
    /// This extension must only be used when the server advertised support for it sending the QUOTA* capability.
1484
    /// </div>
1485
    SetQuota {
1486
        /// Name of quota root.
1487
        root: AString<'a>,
1488
        /// List of resource limits.
1489
        quotas: Vec<QuotaSet<'a>>,
1490
    },
1491

1492
    /// MOVE command.
1493
    ///
1494
    /// <div class="warning">
1495
    /// This extension must only be used when the server advertised support for it sending the MOVE capability.
1496
    /// </div>
1497
    Move {
1498
        /// Set of messages.
1499
        sequence_set: SequenceSet,
1500
        /// Destination mailbox.
1501
        mailbox: Mailbox<'a>,
1502
        /// Use UID variant.
1503
        uid: bool,
1504
    },
1505

1506
    #[cfg(feature = "ext_id")]
1507
    /// ID command.
1508
    ///
1509
    /// <div class="warning">
1510
    /// This extension must only be used when the server advertised support for it sending the ID capability.
1511
    /// </div>
1512
    Id {
1513
        /// Parameters.
1514
        parameters: Option<Vec<(IString<'a>, NString<'a>)>>,
1515
    },
1516

1517
    #[cfg(feature = "ext_metadata")]
1518
    /// Set annotation(s).
1519
    ///
1520
    /// <div class="warning">
1521
    /// This extension must only be used when the server advertised support for it sending the METADATA* capability.
1522
    /// </div>
1523
    SetMetadata {
1524
        mailbox: Mailbox<'a>,
1525
        entry_values: Vec1<EntryValue<'a>>,
1526
    },
1527

1528
    #[cfg(feature = "ext_metadata")]
1529
    /// Retrieve server or mailbox annotation(s).
1530
    ///
1531
    /// <div class="warning">
1532
    /// This extension must only be used when the server advertised support for it sending the METADATA* capability.
1533
    /// </div>
1534
    GetMetadata {
1535
        options: Vec<GetMetadataOption>,
1536
        mailbox: Mailbox<'a>,
1537
        entries: Vec1<Entry<'a>>,
1538
    },
1539
    #[cfg(feature = "ext_namespace")]
1540
    /// Retrieve the namespaces available to the client.
1541
    ///
1542
    /// <div class="warning">
1543
    /// This extension must only be used when the server advertised support for it sending the NAMESPACE capability.
1544
    /// </div>
1545
    Namespace,
1546
}
1547

1548
impl<'a> CommandBody<'a> {
1549
    /// Prepend a tag to finalize the command body to a command.
1550
    pub fn tag<T>(self, tag: T) -> Result<Command<'a>, T::Error>
120✔
1551
    where
120✔
1552
        T: TryInto<Tag<'a>>,
120✔
1553
    {
1554
        Ok(Command {
1555
            tag: tag.try_into()?,
120✔
1556
            body: self,
120✔
1557
        })
1558
    }
120✔
1559

1560
    // ----- Constructors -----
1561

1562
    /// Construct an AUTHENTICATE command.
1563
    pub fn authenticate(mechanism: AuthMechanism<'a>) -> Self {
30✔
1564
        CommandBody::Authenticate {
30✔
1565
            mechanism,
30✔
1566
            initial_response: None,
30✔
1567
        }
30✔
1568
    }
30✔
1569

1570
    /// Construct an AUTHENTICATE command (with an initial response, SASL-IR).
1571
    ///
1572
    /// Note: Use this only when the server advertised the `SASL-IR` capability.
1573
    ///
1574
    /// <div class="warning">
1575
    /// This extension must only be used when the server advertised support for it sending the SASL-IR capability.
1576
    /// </div>
1577
    pub fn authenticate_with_ir<I>(mechanism: AuthMechanism<'a>, initial_response: I) -> Self
8✔
1578
    where
8✔
1579
        I: Into<Cow<'a, [u8]>>,
8✔
1580
    {
1581
        CommandBody::Authenticate {
8✔
1582
            mechanism,
8✔
1583
            initial_response: Some(Secret::new(initial_response.into())),
8✔
1584
        }
8✔
1585
    }
8✔
1586

1587
    /// Construct a LOGIN command.
1588
    pub fn login<U, P>(username: U, password: P) -> Result<Self, LoginError<U::Error, P::Error>>
50✔
1589
    where
50✔
1590
        U: TryInto<AString<'a>>,
50✔
1591
        P: TryInto<AString<'a>>,
50✔
1592
    {
1593
        Ok(CommandBody::Login {
1594
            username: username.try_into().map_err(LoginError::Username)?,
50✔
1595
            password: Secret::new(password.try_into().map_err(LoginError::Password)?),
44✔
1596
        })
1597
    }
50✔
1598

1599
    /// Construct a SELECT command.
1600
    pub fn select<M>(mailbox: M) -> Result<Self, M::Error>
14✔
1601
    where
14✔
1602
        M: TryInto<Mailbox<'a>>,
14✔
1603
    {
1604
        Ok(CommandBody::Select {
1605
            mailbox: mailbox.try_into()?,
14✔
1606
            #[cfg(feature = "ext_condstore_qresync")]
1607
            parameters: Vec::default(),
14✔
1608
        })
1609
    }
14✔
1610

1611
    /// Construct an EXAMINE command.
1612
    pub fn examine<M>(mailbox: M) -> Result<Self, M::Error>
12✔
1613
    where
12✔
1614
        M: TryInto<Mailbox<'a>>,
12✔
1615
    {
1616
        Ok(CommandBody::Examine {
1617
            mailbox: mailbox.try_into()?,
12✔
1618
            #[cfg(feature = "ext_condstore_qresync")]
1619
            parameters: Vec::default(),
12✔
1620
        })
1621
    }
12✔
1622

1623
    /// Construct a CREATE command.
1624
    pub fn create<M>(mailbox: M) -> Result<Self, M::Error>
2✔
1625
    where
2✔
1626
        M: TryInto<Mailbox<'a>>,
2✔
1627
    {
1628
        Ok(CommandBody::Create {
1629
            mailbox: mailbox.try_into()?,
2✔
1630
        })
1631
    }
2✔
1632

1633
    /// Construct a DELETE command.
1634
    pub fn delete<M>(mailbox: M) -> Result<Self, M::Error>
2✔
1635
    where
2✔
1636
        M: TryInto<Mailbox<'a>>,
2✔
1637
    {
1638
        Ok(CommandBody::Delete {
1639
            mailbox: mailbox.try_into()?,
2✔
1640
        })
1641
    }
2✔
1642

1643
    /// Construct a RENAME command.
1644
    pub fn rename<F, T>(mailbox: F, new_mailbox: T) -> Result<Self, RenameError<F::Error, T::Error>>
2✔
1645
    where
2✔
1646
        F: TryInto<Mailbox<'a>>,
2✔
1647
        T: TryInto<Mailbox<'a>>,
2✔
1648
    {
1649
        Ok(CommandBody::Rename {
1650
            from: mailbox.try_into().map_err(RenameError::From)?,
2✔
1651
            to: new_mailbox.try_into().map_err(RenameError::To)?,
2✔
1652
        })
1653
    }
2✔
1654

1655
    /// Construct a SUBSCRIBE command.
1656
    pub fn subscribe<M>(mailbox: M) -> Result<Self, M::Error>
2✔
1657
    where
2✔
1658
        M: TryInto<Mailbox<'a>>,
2✔
1659
    {
1660
        Ok(CommandBody::Subscribe {
1661
            mailbox: mailbox.try_into()?,
2✔
1662
        })
1663
    }
2✔
1664

1665
    /// Construct an UNSUBSCRIBE command.
1666
    pub fn unsubscribe<M>(mailbox: M) -> Result<Self, M::Error>
2✔
1667
    where
2✔
1668
        M: TryInto<Mailbox<'a>>,
2✔
1669
    {
1670
        Ok(CommandBody::Unsubscribe {
1671
            mailbox: mailbox.try_into()?,
2✔
1672
        })
1673
    }
2✔
1674

1675
    /// Construct a LIST command.
1676
    pub fn list<A, B>(
6✔
1677
        reference: A,
6✔
1678
        mailbox_wildcard: B,
6✔
1679
    ) -> Result<Self, ListError<A::Error, B::Error>>
6✔
1680
    where
6✔
1681
        A: TryInto<Mailbox<'a>>,
6✔
1682
        B: TryInto<ListMailbox<'a>>,
6✔
1683
    {
1684
        Ok(CommandBody::List {
1685
            reference: reference.try_into().map_err(ListError::Reference)?,
6✔
1686
            mailbox_wildcard: mailbox_wildcard.try_into().map_err(ListError::Mailbox)?,
6✔
1687
        })
1688
    }
6✔
1689

1690
    /// Construct a LSUB command.
1691
    pub fn lsub<A, B>(
4✔
1692
        reference: A,
4✔
1693
        mailbox_wildcard: B,
4✔
1694
    ) -> Result<Self, ListError<A::Error, B::Error>>
4✔
1695
    where
4✔
1696
        A: TryInto<Mailbox<'a>>,
4✔
1697
        B: TryInto<ListMailbox<'a>>,
4✔
1698
    {
1699
        Ok(CommandBody::Lsub {
1700
            reference: reference.try_into().map_err(ListError::Reference)?,
4✔
1701
            mailbox_wildcard: mailbox_wildcard.try_into().map_err(ListError::Mailbox)?,
4✔
1702
        })
1703
    }
4✔
1704

1705
    /// Construct a STATUS command.
1706
    pub fn status<M, I>(mailbox: M, item_names: I) -> Result<Self, M::Error>
4✔
1707
    where
4✔
1708
        M: TryInto<Mailbox<'a>>,
4✔
1709
        I: Into<Cow<'a, [StatusDataItemName]>>,
4✔
1710
    {
1711
        let mailbox = mailbox.try_into()?;
4✔
1712

1713
        Ok(CommandBody::Status {
4✔
1714
            mailbox,
4✔
1715
            item_names: item_names.into(),
4✔
1716
        })
4✔
1717
    }
4✔
1718

1719
    /// Construct an APPEND command.
1720
    pub fn append<M, D>(
4✔
1721
        mailbox: M,
4✔
1722
        flags: Vec<Flag<'a>>,
4✔
1723
        date: Option<DateTime>,
4✔
1724
        message: D,
4✔
1725
    ) -> Result<Self, AppendError<M::Error, D::Error>>
4✔
1726
    where
4✔
1727
        M: TryInto<Mailbox<'a>>,
4✔
1728
        D: TryInto<Literal<'a>>,
4✔
1729
    {
1730
        Ok(CommandBody::Append {
1731
            mailbox: mailbox.try_into().map_err(AppendError::Mailbox)?,
4✔
1732
            flags,
4✔
1733
            date,
4✔
1734
            message: LiteralOrLiteral8::Literal(message.try_into().map_err(AppendError::Data)?),
4✔
1735
        })
1736
    }
4✔
1737

1738
    /// Construct a SEARCH command.
1739
    pub fn search(charset: Option<Charset<'a>>, criteria: Vec1<SearchKey<'a>>, uid: bool) -> Self {
14✔
1740
        CommandBody::Search {
14✔
1741
            charset,
14✔
1742
            criteria,
14✔
1743
            uid,
14✔
1744
        }
14✔
1745
    }
14✔
1746

1747
    /// Construct a FETCH command.
1748
    pub fn fetch<S, I>(sequence_set: S, macro_or_item_names: I, uid: bool) -> Result<Self, S::Error>
8✔
1749
    where
8✔
1750
        S: TryInto<SequenceSet>,
8✔
1751
        I: Into<MacroOrMessageDataItemNames<'a>>,
8✔
1752
    {
1753
        let sequence_set = sequence_set.try_into()?;
8✔
1754

1755
        Ok(CommandBody::Fetch {
8✔
1756
            sequence_set,
8✔
1757
            macro_or_item_names: macro_or_item_names.into(),
8✔
1758
            uid,
8✔
1759
            #[cfg(feature = "ext_condstore_qresync")]
8✔
1760
            modifiers: Vec::default(),
8✔
1761
        })
8✔
1762
    }
8✔
1763

1764
    /// Construct a STORE command.
1765
    pub fn store<S>(
6✔
1766
        sequence_set: S,
6✔
1767
        kind: StoreType,
6✔
1768
        response: StoreResponse,
6✔
1769
        flags: Vec<Flag<'a>>,
6✔
1770
        uid: bool,
6✔
1771
    ) -> Result<Self, S::Error>
6✔
1772
    where
6✔
1773
        S: TryInto<SequenceSet>,
6✔
1774
    {
1775
        let sequence_set = sequence_set.try_into()?;
6✔
1776

1777
        Ok(CommandBody::Store {
6✔
1778
            sequence_set,
6✔
1779
            kind,
6✔
1780
            response,
6✔
1781
            flags,
6✔
1782
            uid,
6✔
1783
            #[cfg(feature = "ext_condstore_qresync")]
6✔
1784
            modifiers: Vec::default(),
6✔
1785
        })
6✔
1786
    }
6✔
1787

1788
    /// Construct a COPY command.
1789
    pub fn copy<S, M>(
4✔
1790
        sequence_set: S,
4✔
1791
        mailbox: M,
4✔
1792
        uid: bool,
4✔
1793
    ) -> Result<Self, CopyError<S::Error, M::Error>>
4✔
1794
    where
4✔
1795
        S: TryInto<SequenceSet>,
4✔
1796
        M: TryInto<Mailbox<'a>>,
4✔
1797
    {
1798
        Ok(CommandBody::Copy {
1799
            sequence_set: sequence_set.try_into().map_err(CopyError::Sequence)?,
4✔
1800
            mailbox: mailbox.try_into().map_err(CopyError::Mailbox)?,
4✔
1801
            uid,
4✔
1802
        })
1803
    }
4✔
1804

1805
    /// Get the name of the command.
1806
    pub fn name(&self) -> &'static str {
66✔
1807
        match self {
66✔
1808
            Self::Capability => "CAPABILITY",
2✔
1809
            Self::Noop => "NOOP",
2✔
1810
            Self::Logout => "LOGOUT",
2✔
1811
            #[cfg(feature = "starttls")]
1812
            Self::StartTLS => "STARTTLS",
2✔
1813
            Self::Authenticate { .. } => "AUTHENTICATE",
2✔
1814
            Self::Login { .. } => "LOGIN",
2✔
1815
            Self::Select { .. } => "SELECT",
2✔
1816
            Self::Sort { .. } => "SORT",
×
1817
            Self::Thread { .. } => "THREAD",
×
1818
            Self::Unselect => "UNSELECT",
2✔
1819
            Self::Examine { .. } => "EXAMINE",
2✔
1820
            Self::Create { .. } => "CREATE",
2✔
1821
            Self::Delete { .. } => "DELETE",
2✔
1822
            Self::Rename { .. } => "RENAME",
2✔
1823
            Self::Subscribe { .. } => "SUBSCRIBE",
2✔
1824
            Self::Unsubscribe { .. } => "UNSUBSCRIBE",
2✔
1825
            Self::List { .. } => "LIST",
2✔
1826
            Self::Lsub { .. } => "LSUB",
2✔
1827
            Self::Status { .. } => "STATUS",
2✔
1828
            Self::Append { .. } => "APPEND",
4✔
1829
            Self::Check => "CHECK",
2✔
1830
            Self::Close => "CLOSE",
2✔
1831
            Self::Expunge => "EXPUNGE",
2✔
1832
            Self::ExpungeUid { .. } => "EXPUNGE",
×
1833
            Self::Search { .. } => "SEARCH",
2✔
1834
            Self::Fetch { .. } => "FETCH",
2✔
1835
            Self::Store { .. } => "STORE",
2✔
1836
            Self::Copy { .. } => "COPY",
2✔
1837
            Self::Idle => "IDLE",
2✔
1838
            Self::Enable { .. } => "ENABLE",
2✔
1839
            Self::Compress { .. } => "COMPRESS",
2✔
1840
            Self::GetQuota { .. } => "GETQUOTA",
2✔
1841
            Self::GetQuotaRoot { .. } => "GETQUOTAROOT",
2✔
1842
            Self::SetQuota { .. } => "SETQUOTA",
2✔
1843
            Self::Move { .. } => "MOVE",
2✔
1844
            #[cfg(feature = "ext_id")]
1845
            Self::Id { .. } => "ID",
×
1846
            #[cfg(feature = "ext_metadata")]
1847
            Self::SetMetadata { .. } => "SETMETADATA",
×
1848
            #[cfg(feature = "ext_metadata")]
1849
            Self::GetMetadata { .. } => "GETMETADATA",
×
1850
            #[cfg(feature = "ext_namespace")]
NEW
1851
            Self::Namespace => "NAMESPACE",
×
1852
        }
1853
    }
66✔
1854
}
1855

1856
#[cfg(feature = "ext_condstore_qresync")]
1857
#[cfg_attr(docsrs, doc(cfg("ext_condstore_qresync")))]
1858
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
1859
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1860
#[cfg_attr(feature = "serde", serde(tag = "type", content = "content"))]
1861
#[derive(Debug, Clone, PartialEq, Eq, Hash, ToStatic)]
1862
pub enum SelectParameter {
1863
    CondStore,
1864
    QResync {
1865
        uid_validity: NonZeroU32,
1866
        mod_sequence_value: NonZeroU64,
1867
        known_uids: Option<SequenceSet>, // TODO(misuse): "*" is not allowed.
1868
        seq_match_data: Option<(SequenceSet, SequenceSet)>, // TODO(misuse): ensure both have the same length?
1869
    },
1870
}
1871

1872
#[cfg(feature = "ext_condstore_qresync")]
1873
#[cfg_attr(docsrs, doc(cfg("ext_condstore_qresync")))]
1874
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
1875
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1876
#[cfg_attr(feature = "serde", serde(tag = "type", content = "content"))]
1877
#[derive(Debug, Clone, PartialEq, Eq, Hash, ToStatic)]
1878
pub enum FetchModifier {
1879
    ChangedSince(NonZeroU64),
1880
    Vanished,
1881
}
1882

1883
#[cfg(feature = "ext_condstore_qresync")]
1884
#[cfg_attr(docsrs, doc(cfg("ext_condstore_qresync")))]
1885
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
1886
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1887
#[cfg_attr(feature = "serde", serde(tag = "type", content = "content"))]
1888
#[derive(Debug, Clone, PartialEq, Eq, Hash, ToStatic)]
1889
pub enum StoreModifier {
1890
    UnchangedSince(u64),
1891
}
1892

1893
/// Error-related types.
1894
pub mod error {
1895
    use thiserror::Error;
1896

1897
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
1898
    pub enum LoginError<U, P> {
1899
        #[error("Invalid username: {0}")]
1900
        Username(U),
1901
        #[error("Invalid password: {0}")]
1902
        Password(P),
1903
    }
1904

1905
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
1906
    pub enum RenameError<F, T> {
1907
        #[error("Invalid (from) mailbox: {0}")]
1908
        From(F),
1909
        #[error("Invalid (to) mailbox: {0}")]
1910
        To(T),
1911
    }
1912

1913
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
1914
    pub enum ListError<R, M> {
1915
        #[error("Invalid reference: {0}")]
1916
        Reference(R),
1917
        #[error("Invalid mailbox: {0}")]
1918
        Mailbox(M),
1919
    }
1920

1921
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
1922
    pub enum AppendError<M, D> {
1923
        #[error("Invalid mailbox: {0}")]
1924
        Mailbox(M),
1925
        #[error("Invalid data: {0}")]
1926
        Data(D),
1927
    }
1928

1929
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
1930
    pub enum CopyError<S, M> {
1931
        #[error("Invalid sequence: {0}")]
1932
        Sequence(S),
1933
        #[error("Invalid mailbox: {0}")]
1934
        Mailbox(M),
1935
    }
1936
}
1937

1938
#[cfg(test)]
1939
mod tests {
1940
    use chrono::DateTime as ChronoDateTime;
1941

1942
    use super::*;
1943
    #[cfg(feature = "ext_utf8")]
1944
    use crate::extensions::{enable::CapabilityEnable, utf8::Utf8Kind};
1945
    use crate::{
1946
        auth::AuthMechanism,
1947
        core::{AString, Charset, IString, Literal, LiteralMode, Vec1},
1948
        datetime::DateTime,
1949
        extensions::{binary::Literal8, compress::CompressionAlgorithm},
1950
        fetch::{Macro, MacroOrMessageDataItemNames, MessageDataItemName, Part, Section},
1951
        flag::{Flag, StoreType},
1952
        mailbox::{ListMailbox, Mailbox},
1953
        search::SearchKey,
1954
        secret::Secret,
1955
        sequence::{SeqOrUid, Sequence, SequenceSet},
1956
        status::StatusDataItemName,
1957
    };
1958

1959
    #[test]
1960
    fn test_conversion_command_body() {
2✔
1961
        let cmds = vec![
2✔
1962
            CommandBody::Capability,
2✔
1963
            CommandBody::Noop,
2✔
1964
            CommandBody::Logout,
2✔
1965
            #[cfg(feature = "starttls")]
1966
            CommandBody::StartTLS,
2✔
1967
            CommandBody::authenticate(AuthMechanism::Plain),
2✔
1968
            CommandBody::authenticate(AuthMechanism::Login),
2✔
1969
            CommandBody::authenticate_with_ir(AuthMechanism::Plain, b"XXXXXXXX".as_ref()),
2✔
1970
            CommandBody::authenticate_with_ir(AuthMechanism::Login, b"YYYYYYYY".as_ref()),
2✔
1971
            CommandBody::login("alice", "I_am_an_atom").unwrap(),
2✔
1972
            CommandBody::login("alice", "I am \\ \"quoted\"").unwrap(),
2✔
1973
            CommandBody::login("alice", "I am a literal²").unwrap(),
2✔
1974
            CommandBody::login(
2✔
1975
                AString::Atom("alice".try_into().unwrap()),
2✔
1976
                AString::String(crate::core::IString::Literal(
2✔
1977
                    vec![0xff, 0xff, 0xff].try_into().unwrap(),
2✔
1978
                )),
2✔
1979
            )
1980
            .unwrap(),
2✔
1981
            CommandBody::select("inbox").unwrap(),
2✔
1982
            CommandBody::select("atom").unwrap(),
2✔
1983
            CommandBody::select("C:\\").unwrap(),
2✔
1984
            CommandBody::select("²").unwrap(),
2✔
1985
            CommandBody::select("Trash").unwrap(),
2✔
1986
            CommandBody::examine("inbox").unwrap(),
2✔
1987
            CommandBody::examine("atom").unwrap(),
2✔
1988
            CommandBody::examine("C:\\").unwrap(),
2✔
1989
            CommandBody::examine("²").unwrap(),
2✔
1990
            CommandBody::examine("Trash").unwrap(),
2✔
1991
            CommandBody::create("inBoX").unwrap(),
2✔
1992
            CommandBody::delete("inBOX").unwrap(),
2✔
1993
            CommandBody::rename("iNBoS", "INboX").unwrap(),
2✔
1994
            CommandBody::subscribe("inbox").unwrap(),
2✔
1995
            CommandBody::unsubscribe("INBOX").unwrap(),
2✔
1996
            CommandBody::list("iNbOx", "test").unwrap(),
2✔
1997
            CommandBody::list("inbox", ListMailbox::Token("test".try_into().unwrap())).unwrap(),
2✔
1998
            CommandBody::lsub(
2✔
1999
                "inbox",
2000
                ListMailbox::String(IString::Quoted("\x7f".try_into().unwrap())),
2✔
2001
            )
2002
            .unwrap(),
2✔
2003
            CommandBody::list("inBoX", ListMailbox::Token("test".try_into().unwrap())).unwrap(),
2✔
2004
            CommandBody::lsub(
2✔
2005
                "INBOX",
2006
                ListMailbox::String(IString::Quoted("\x7f".try_into().unwrap())),
2✔
2007
            )
2008
            .unwrap(),
2✔
2009
            CommandBody::status("inbox", vec![StatusDataItemName::Messages]).unwrap(),
2✔
2010
            CommandBody::append(
2✔
2011
                "inbox",
2012
                vec![],
2✔
2013
                Some(
2✔
2014
                    DateTime::try_from(
2✔
2015
                        ChronoDateTime::parse_from_rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")
2✔
2016
                            .unwrap(),
2✔
2017
                    )
2✔
2018
                    .unwrap(),
2✔
2019
                ),
2✔
2020
                vec![0xff, 0xff, 0xff],
2✔
2021
            )
2022
            .unwrap(),
2✔
2023
            CommandBody::append(
2✔
2024
                "inbox",
2025
                vec![Flag::Keyword("test".try_into().unwrap())],
2✔
2026
                Some(
2✔
2027
                    DateTime::try_from(
2✔
2028
                        ChronoDateTime::parse_from_rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")
2✔
2029
                            .unwrap(),
2✔
2030
                    )
2✔
2031
                    .unwrap(),
2✔
2032
                ),
2✔
2033
                vec![0xff, 0xff, 0xff],
2✔
2034
            )
2035
            .unwrap(),
2✔
2036
            CommandBody::Check,
2✔
2037
            CommandBody::Close,
2✔
2038
            CommandBody::Expunge,
2✔
2039
            CommandBody::search(
2✔
2040
                None,
2✔
2041
                Vec1::from(SearchKey::And(
2✔
2042
                    vec![SearchKey::All, SearchKey::New, SearchKey::Unseen]
2✔
2043
                        .try_into()
2✔
2044
                        .unwrap(),
2✔
2045
                )),
2✔
2046
                false,
2047
            ),
2048
            CommandBody::search(
2✔
2049
                None,
2✔
2050
                Vec1::from(SearchKey::And(
2✔
2051
                    vec![SearchKey::All, SearchKey::New, SearchKey::Unseen]
2✔
2052
                        .try_into()
2✔
2053
                        .unwrap(),
2✔
2054
                )),
2✔
2055
                true,
2056
            ),
2057
            CommandBody::search(
2✔
2058
                None,
2✔
2059
                Vec1::from(SearchKey::And(
2✔
2060
                    vec![SearchKey::SequenceSet(SequenceSet(
2✔
2061
                        vec![Sequence::Single(SeqOrUid::Value(42.try_into().unwrap()))]
2✔
2062
                            .try_into()
2✔
2063
                            .unwrap(),
2✔
2064
                    ))]
2✔
2065
                    .try_into()
2✔
2066
                    .unwrap(),
2✔
2067
                )),
2✔
2068
                true,
2069
            ),
2070
            CommandBody::search(
2✔
2071
                None,
2✔
2072
                Vec1::from(SearchKey::SequenceSet("42".try_into().unwrap())),
2✔
2073
                true,
2074
            ),
2075
            CommandBody::search(
2✔
2076
                None,
2✔
2077
                Vec1::from(SearchKey::SequenceSet("*".try_into().unwrap())),
2✔
2078
                true,
2079
            ),
2080
            CommandBody::search(
2✔
2081
                None,
2✔
2082
                Vec1::from(SearchKey::Or(
2✔
2083
                    Box::new(SearchKey::Draft),
2✔
2084
                    Box::new(SearchKey::All),
2✔
2085
                )),
2✔
2086
                true,
2087
            ),
2088
            CommandBody::search(
2✔
2089
                Some(Charset::try_from("UTF-8").unwrap()),
2✔
2090
                Vec1::from(SearchKey::Or(
2✔
2091
                    Box::new(SearchKey::Draft),
2✔
2092
                    Box::new(SearchKey::All),
2✔
2093
                )),
2✔
2094
                true,
2095
            ),
2096
            CommandBody::fetch(
2✔
2097
                "1",
2098
                vec![MessageDataItemName::BodyExt {
2✔
2099
                    partial: None,
2✔
2100
                    section: Some(Section::Part(Part(
2✔
2101
                        vec![1.try_into().unwrap(), 1.try_into().unwrap()]
2✔
2102
                            .try_into()
2✔
2103
                            .unwrap(),
2✔
2104
                    ))),
2✔
2105
                    peek: true,
2✔
2106
                }],
2✔
2107
                false,
2108
            )
2109
            .unwrap(),
2✔
2110
            CommandBody::fetch("1:*,2,3", Macro::Full, true).unwrap(),
2✔
2111
            CommandBody::store(
2✔
2112
                "1,2:*",
2113
                StoreType::Remove,
2✔
2114
                StoreResponse::Answer,
2✔
2115
                vec![Flag::Seen, Flag::Draft],
2✔
2116
                false,
2117
            )
2118
            .unwrap(),
2✔
2119
            CommandBody::store(
2✔
2120
                "1:5",
2121
                StoreType::Add,
2✔
2122
                StoreResponse::Answer,
2✔
2123
                vec![Flag::Keyword("TEST".try_into().unwrap())],
2✔
2124
                true,
2125
            )
2126
            .unwrap(),
2✔
2127
            CommandBody::copy("1", "inbox", false).unwrap(),
2✔
2128
            CommandBody::copy("1337", "archive", true).unwrap(),
2✔
2129
        ];
2130

2131
        for (no, cmd_body) in cmds.into_iter().enumerate() {
102✔
2132
            println!("Test: {no}, {cmd_body:?}");
102✔
2133

102✔
2134
            let _ = cmd_body.tag(format!("A{no}")).unwrap();
102✔
2135
        }
102✔
2136
    }
2✔
2137

2138
    #[test]
2139
    fn test_command_body_name() {
2✔
2140
        let tests = [
2✔
2141
            (CommandBody::Capability, "CAPABILITY"),
2✔
2142
            (CommandBody::Noop, "NOOP"),
2✔
2143
            (CommandBody::Logout, "LOGOUT"),
2✔
2144
            #[cfg(feature = "starttls")]
2✔
2145
            (CommandBody::StartTLS, "STARTTLS"),
2✔
2146
            (
2✔
2147
                CommandBody::Authenticate {
2✔
2148
                    mechanism: AuthMechanism::Plain,
2✔
2149
                    initial_response: None,
2✔
2150
                },
2✔
2151
                "AUTHENTICATE",
2✔
2152
            ),
2✔
2153
            (
2✔
2154
                CommandBody::Login {
2✔
2155
                    username: AString::try_from("user").unwrap(),
2✔
2156
                    password: Secret::new(AString::try_from("pass").unwrap()),
2✔
2157
                },
2✔
2158
                "LOGIN",
2✔
2159
            ),
2✔
2160
            (
2✔
2161
                CommandBody::Select {
2✔
2162
                    mailbox: Mailbox::Inbox,
2✔
2163
                    #[cfg(feature = "ext_condstore_qresync")]
2✔
2164
                    parameters: Vec::default(),
2✔
2165
                },
2✔
2166
                "SELECT",
2✔
2167
            ),
2✔
2168
            (CommandBody::Unselect, "UNSELECT"),
2✔
2169
            (
2✔
2170
                CommandBody::Examine {
2✔
2171
                    mailbox: Mailbox::Inbox,
2✔
2172
                    #[cfg(feature = "ext_condstore_qresync")]
2✔
2173
                    parameters: Vec::default(),
2✔
2174
                },
2✔
2175
                "EXAMINE",
2✔
2176
            ),
2✔
2177
            (
2✔
2178
                CommandBody::Create {
2✔
2179
                    mailbox: Mailbox::Inbox,
2✔
2180
                },
2✔
2181
                "CREATE",
2✔
2182
            ),
2✔
2183
            (
2✔
2184
                CommandBody::Delete {
2✔
2185
                    mailbox: Mailbox::Inbox,
2✔
2186
                },
2✔
2187
                "DELETE",
2✔
2188
            ),
2✔
2189
            (
2✔
2190
                CommandBody::Rename {
2✔
2191
                    from: Mailbox::Inbox,
2✔
2192
                    to: Mailbox::Inbox,
2✔
2193
                },
2✔
2194
                "RENAME",
2✔
2195
            ),
2✔
2196
            (
2✔
2197
                CommandBody::Subscribe {
2✔
2198
                    mailbox: Mailbox::Inbox,
2✔
2199
                },
2✔
2200
                "SUBSCRIBE",
2✔
2201
            ),
2✔
2202
            (
2✔
2203
                CommandBody::Unsubscribe {
2✔
2204
                    mailbox: Mailbox::Inbox,
2✔
2205
                },
2✔
2206
                "UNSUBSCRIBE",
2✔
2207
            ),
2✔
2208
            (
2✔
2209
                CommandBody::List {
2✔
2210
                    reference: Mailbox::Inbox,
2✔
2211
                    mailbox_wildcard: ListMailbox::try_from("").unwrap(),
2✔
2212
                },
2✔
2213
                "LIST",
2✔
2214
            ),
2✔
2215
            (
2✔
2216
                CommandBody::Lsub {
2✔
2217
                    reference: Mailbox::Inbox,
2✔
2218
                    mailbox_wildcard: ListMailbox::try_from("").unwrap(),
2✔
2219
                },
2✔
2220
                "LSUB",
2✔
2221
            ),
2✔
2222
            (
2✔
2223
                CommandBody::Status {
2✔
2224
                    mailbox: Mailbox::Inbox,
2✔
2225
                    item_names: vec![].into(),
2✔
2226
                },
2✔
2227
                "STATUS",
2✔
2228
            ),
2✔
2229
            (
2✔
2230
                CommandBody::Append {
2✔
2231
                    mailbox: Mailbox::Inbox,
2✔
2232
                    flags: vec![],
2✔
2233
                    date: None,
2✔
2234
                    message: LiteralOrLiteral8::Literal(Literal::try_from("").unwrap()),
2✔
2235
                },
2✔
2236
                "APPEND",
2✔
2237
            ),
2✔
2238
            (
2✔
2239
                CommandBody::Append {
2✔
2240
                    mailbox: Mailbox::Inbox,
2✔
2241
                    flags: vec![],
2✔
2242
                    date: None,
2✔
2243
                    message: LiteralOrLiteral8::Literal8(Literal8 {
2✔
2244
                        data: b"Hello\x00World\x00".as_ref().into(),
2✔
2245
                        mode: LiteralMode::NonSync,
2✔
2246
                    }),
2✔
2247
                },
2✔
2248
                "APPEND",
2✔
2249
            ),
2✔
2250
            (CommandBody::Check, "CHECK"),
2✔
2251
            (CommandBody::Close, "CLOSE"),
2✔
2252
            (CommandBody::Expunge, "EXPUNGE"),
2✔
2253
            (
2✔
2254
                CommandBody::Search {
2✔
2255
                    charset: None,
2✔
2256
                    criteria: Vec1::from(SearchKey::Recent),
2✔
2257
                    uid: true,
2✔
2258
                },
2✔
2259
                "SEARCH",
2✔
2260
            ),
2✔
2261
            (
2✔
2262
                CommandBody::Fetch {
2✔
2263
                    sequence_set: SequenceSet::try_from(1u32).unwrap(),
2✔
2264
                    macro_or_item_names: MacroOrMessageDataItemNames::Macro(Macro::Full),
2✔
2265
                    uid: true,
2✔
2266
                    #[cfg(feature = "ext_condstore_qresync")]
2✔
2267
                    modifiers: Vec::default(),
2✔
2268
                },
2✔
2269
                "FETCH",
2✔
2270
            ),
2✔
2271
            (
2✔
2272
                CommandBody::Store {
2✔
2273
                    sequence_set: SequenceSet::try_from(1).unwrap(),
2✔
2274
                    flags: vec![],
2✔
2275
                    response: StoreResponse::Silent,
2✔
2276
                    kind: StoreType::Add,
2✔
2277
                    uid: true,
2✔
2278
                    #[cfg(feature = "ext_condstore_qresync")]
2✔
2279
                    modifiers: Vec::default(),
2✔
2280
                },
2✔
2281
                "STORE",
2✔
2282
            ),
2✔
2283
            (
2✔
2284
                CommandBody::Copy {
2✔
2285
                    sequence_set: SequenceSet::try_from(1).unwrap(),
2✔
2286
                    mailbox: Mailbox::Inbox,
2✔
2287
                    uid: true,
2✔
2288
                },
2✔
2289
                "COPY",
2✔
2290
            ),
2✔
2291
            (CommandBody::Idle, "IDLE"),
2✔
2292
            #[cfg(feature = "ext_utf8")]
2✔
2293
            (
2✔
2294
                CommandBody::Enable {
2✔
2295
                    capabilities: Vec1::from(CapabilityEnable::Utf8(Utf8Kind::Only)),
2✔
2296
                },
2✔
2297
                "ENABLE",
2✔
2298
            ),
2✔
2299
            (
2✔
2300
                CommandBody::Compress {
2✔
2301
                    algorithm: CompressionAlgorithm::Deflate,
2✔
2302
                },
2✔
2303
                "COMPRESS",
2✔
2304
            ),
2✔
2305
            (
2✔
2306
                CommandBody::GetQuota {
2✔
2307
                    root: AString::try_from("root").unwrap(),
2✔
2308
                },
2✔
2309
                "GETQUOTA",
2✔
2310
            ),
2✔
2311
            (
2✔
2312
                CommandBody::GetQuotaRoot {
2✔
2313
                    mailbox: Mailbox::Inbox,
2✔
2314
                },
2✔
2315
                "GETQUOTAROOT",
2✔
2316
            ),
2✔
2317
            (
2✔
2318
                CommandBody::SetQuota {
2✔
2319
                    root: AString::try_from("root").unwrap(),
2✔
2320
                    quotas: vec![],
2✔
2321
                },
2✔
2322
                "SETQUOTA",
2✔
2323
            ),
2✔
2324
            (
2✔
2325
                CommandBody::Move {
2✔
2326
                    sequence_set: SequenceSet::try_from(1).unwrap(),
2✔
2327
                    mailbox: Mailbox::Inbox,
2✔
2328
                    uid: true,
2✔
2329
                },
2✔
2330
                "MOVE",
2✔
2331
            ),
2✔
2332
        ];
2✔
2333

2334
        for (test, expected) in tests {
68✔
2335
            assert_eq!(test.name(), expected);
66✔
2336
        }
2337
    }
2✔
2338
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc