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

duesee / imap-codec / 9964908275

16 Jul 2024 10:20PM UTC coverage: 92.967% (-0.01%) from 92.981%
9964908275

push

github

duesee
Update imap-codec/Cargo.toml

Co-authored-by: Jakob Schikowski <jakob.schikowski@gmx.de>

11156 of 12000 relevant lines covered (92.97%)

1090.05 hits per line

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

97.55
/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

7
#[cfg(feature = "arbitrary")]
8
use arbitrary::Arbitrary;
9
use bounded_static_derive::ToStatic;
10
#[cfg(feature = "serde")]
11
use serde::{Deserialize, Serialize};
12

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

35
/// Command.
36
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
14,210✔
37
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
×
38
#[derive(Debug, Clone, PartialEq, Eq, Hash, ToStatic)]
4,076✔
39
pub struct Command<'a> {
40
    /// Tag.
41
    pub tag: Tag<'a>,
42
    /// Body, e.g., CAPABILITY, LOGIN, SELECT, etc.
43
    pub body: CommandBody<'a>,
44
}
45

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

58
    /// Get the command name.
59
    pub fn name(&self) -> &'static str {
×
60
        self.body.name()
×
61
    }
×
62
}
63

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1343
    /// ENABLE command.
1344
    ///
1345
    /// <div class="warning">
1346
    /// This extension must only be used when the server advertised support for it sending the ENABLE capability.
1347
    /// </div>
1348
    Enable {
1349
        /// Capabilities to enable.
1350
        capabilities: Vec1<CapabilityEnable<'a>>,
1351
    },
1352

1353
    /// COMPRESS command.
1354
    ///
1355
    /// <div class="warning">
1356
    /// This extension must only be used when the server advertised support for it sending the COMPRESS capability.
1357
    /// </div>
1358
    Compress {
1359
        /// Compression algorithm.
1360
        algorithm: CompressionAlgorithm,
1361
    },
1362

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

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

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

1480
    /// MOVE command.
1481
    ///
1482
    /// <div class="warning">
1483
    /// This extension must only be used when the server advertised support for it sending the MOVE capability.
1484
    /// </div>
1485
    Move {
1486
        /// Set of messages.
1487
        sequence_set: SequenceSet,
1488
        /// Destination mailbox.
1489
        mailbox: Mailbox<'a>,
1490
        /// Use UID variant.
1491
        uid: bool,
1492
    },
1493

1494
    #[cfg(feature = "ext_id")]
1495
    /// ID command.
1496
    ///
1497
    /// <div class="warning">
1498
    /// This extension must only be used when the server advertised support for it sending the ID capability.
1499
    /// </div>
1500
    Id {
1501
        /// Parameters.
1502
        parameters: Option<Vec<(IString<'a>, NString<'a>)>>,
1503
    },
1504

1505
    #[cfg(feature = "ext_metadata")]
1506
    /// Set annotation(s).
1507
    ///
1508
    /// <div class="warning">
1509
    /// This extension must only be used when the server advertised support for it sending the METADATA* capability.
1510
    /// </div>
1511
    SetMetadata {
1512
        mailbox: Mailbox<'a>,
1513
        entry_values: Vec1<EntryValue<'a>>,
1514
    },
1515

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

1529
impl<'a> CommandBody<'a> {
1530
    /// Prepend a tag to finalize the command body to a command.
1531
    pub fn tag<T>(self, tag: T) -> Result<Command<'a>, T::Error>
120✔
1532
    where
120✔
1533
        T: TryInto<Tag<'a>>,
120✔
1534
    {
120✔
1535
        Ok(Command {
120✔
1536
            tag: tag.try_into()?,
120✔
1537
            body: self,
120✔
1538
        })
1539
    }
120✔
1540

1541
    // ----- Constructors -----
1542

1543
    /// Construct an AUTHENTICATE command.
1544
    pub fn authenticate(mechanism: AuthMechanism<'a>) -> Self {
42✔
1545
        CommandBody::Authenticate {
42✔
1546
            mechanism,
42✔
1547
            initial_response: None,
42✔
1548
        }
42✔
1549
    }
42✔
1550

1551
    /// Construct an AUTHENTICATE command (with an initial response, SASL-IR).
1552
    ///
1553
    /// Note: Use this only when the server advertised the `SASL-IR` capability.
1554
    ///
1555
    /// <div class="warning">
1556
    /// This extension must only be used when the server advertised support for it sending the SASL-IR capability.
1557
    /// </div>
1558
    pub fn authenticate_with_ir<I>(mechanism: AuthMechanism<'a>, initial_response: I) -> Self
8✔
1559
    where
8✔
1560
        I: Into<Cow<'a, [u8]>>,
8✔
1561
    {
8✔
1562
        CommandBody::Authenticate {
8✔
1563
            mechanism,
8✔
1564
            initial_response: Some(Secret::new(initial_response.into())),
8✔
1565
        }
8✔
1566
    }
8✔
1567

1568
    /// Construct a LOGIN command.
1569
    pub fn login<U, P>(username: U, password: P) -> Result<Self, LoginError<U::Error, P::Error>>
50✔
1570
    where
50✔
1571
        U: TryInto<AString<'a>>,
50✔
1572
        P: TryInto<AString<'a>>,
50✔
1573
    {
50✔
1574
        Ok(CommandBody::Login {
50✔
1575
            username: username.try_into().map_err(LoginError::Username)?,
50✔
1576
            password: Secret::new(password.try_into().map_err(LoginError::Password)?),
44✔
1577
        })
1578
    }
50✔
1579

1580
    /// Construct a SELECT command.
1581
    pub fn select<M>(mailbox: M) -> Result<Self, M::Error>
14✔
1582
    where
14✔
1583
        M: TryInto<Mailbox<'a>>,
14✔
1584
    {
14✔
1585
        Ok(CommandBody::Select {
14✔
1586
            mailbox: mailbox.try_into()?,
14✔
1587
        })
1588
    }
14✔
1589

1590
    /// Construct an EXAMINE command.
1591
    pub fn examine<M>(mailbox: M) -> Result<Self, M::Error>
12✔
1592
    where
12✔
1593
        M: TryInto<Mailbox<'a>>,
12✔
1594
    {
12✔
1595
        Ok(CommandBody::Examine {
12✔
1596
            mailbox: mailbox.try_into()?,
12✔
1597
        })
1598
    }
12✔
1599

1600
    /// Construct a CREATE command.
1601
    pub fn create<M>(mailbox: M) -> Result<Self, M::Error>
2✔
1602
    where
2✔
1603
        M: TryInto<Mailbox<'a>>,
2✔
1604
    {
2✔
1605
        Ok(CommandBody::Create {
2✔
1606
            mailbox: mailbox.try_into()?,
2✔
1607
        })
1608
    }
2✔
1609

1610
    /// Construct a DELETE command.
1611
    pub fn delete<M>(mailbox: M) -> Result<Self, M::Error>
2✔
1612
    where
2✔
1613
        M: TryInto<Mailbox<'a>>,
2✔
1614
    {
2✔
1615
        Ok(CommandBody::Delete {
2✔
1616
            mailbox: mailbox.try_into()?,
2✔
1617
        })
1618
    }
2✔
1619

1620
    /// Construct a RENAME command.
1621
    pub fn rename<F, T>(mailbox: F, new_mailbox: T) -> Result<Self, RenameError<F::Error, T::Error>>
2✔
1622
    where
2✔
1623
        F: TryInto<Mailbox<'a>>,
2✔
1624
        T: TryInto<Mailbox<'a>>,
2✔
1625
    {
2✔
1626
        Ok(CommandBody::Rename {
2✔
1627
            from: mailbox.try_into().map_err(RenameError::From)?,
2✔
1628
            to: new_mailbox.try_into().map_err(RenameError::To)?,
2✔
1629
        })
1630
    }
2✔
1631

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

1642
    /// Construct an UNSUBSCRIBE command.
1643
    pub fn unsubscribe<M>(mailbox: M) -> Result<Self, M::Error>
2✔
1644
    where
2✔
1645
        M: TryInto<Mailbox<'a>>,
2✔
1646
    {
2✔
1647
        Ok(CommandBody::Unsubscribe {
2✔
1648
            mailbox: mailbox.try_into()?,
2✔
1649
        })
1650
    }
2✔
1651

1652
    /// Construct a LIST command.
1653
    pub fn list<A, B>(
6✔
1654
        reference: A,
6✔
1655
        mailbox_wildcard: B,
6✔
1656
    ) -> Result<Self, ListError<A::Error, B::Error>>
6✔
1657
    where
6✔
1658
        A: TryInto<Mailbox<'a>>,
6✔
1659
        B: TryInto<ListMailbox<'a>>,
6✔
1660
    {
6✔
1661
        Ok(CommandBody::List {
6✔
1662
            reference: reference.try_into().map_err(ListError::Reference)?,
6✔
1663
            mailbox_wildcard: mailbox_wildcard.try_into().map_err(ListError::Mailbox)?,
6✔
1664
        })
1665
    }
6✔
1666

1667
    /// Construct a LSUB command.
1668
    pub fn lsub<A, B>(
4✔
1669
        reference: A,
4✔
1670
        mailbox_wildcard: B,
4✔
1671
    ) -> Result<Self, ListError<A::Error, B::Error>>
4✔
1672
    where
4✔
1673
        A: TryInto<Mailbox<'a>>,
4✔
1674
        B: TryInto<ListMailbox<'a>>,
4✔
1675
    {
4✔
1676
        Ok(CommandBody::Lsub {
4✔
1677
            reference: reference.try_into().map_err(ListError::Reference)?,
4✔
1678
            mailbox_wildcard: mailbox_wildcard.try_into().map_err(ListError::Mailbox)?,
4✔
1679
        })
1680
    }
4✔
1681

1682
    /// Construct a STATUS command.
1683
    pub fn status<M, I>(mailbox: M, item_names: I) -> Result<Self, M::Error>
4✔
1684
    where
4✔
1685
        M: TryInto<Mailbox<'a>>,
4✔
1686
        I: Into<Cow<'a, [StatusDataItemName]>>,
4✔
1687
    {
4✔
1688
        let mailbox = mailbox.try_into()?;
4✔
1689

1690
        Ok(CommandBody::Status {
4✔
1691
            mailbox,
4✔
1692
            item_names: item_names.into(),
4✔
1693
        })
4✔
1694
    }
4✔
1695

1696
    /// Construct an APPEND command.
1697
    pub fn append<M, D>(
4✔
1698
        mailbox: M,
4✔
1699
        flags: Vec<Flag<'a>>,
4✔
1700
        date: Option<DateTime>,
4✔
1701
        message: D,
4✔
1702
    ) -> Result<Self, AppendError<M::Error, D::Error>>
4✔
1703
    where
4✔
1704
        M: TryInto<Mailbox<'a>>,
4✔
1705
        D: TryInto<Literal<'a>>,
4✔
1706
    {
4✔
1707
        Ok(CommandBody::Append {
4✔
1708
            mailbox: mailbox.try_into().map_err(AppendError::Mailbox)?,
4✔
1709
            flags,
4✔
1710
            date,
4✔
1711
            message: LiteralOrLiteral8::Literal(message.try_into().map_err(AppendError::Data)?),
4✔
1712
        })
1713
    }
4✔
1714

1715
    /// Construct a SEARCH command.
1716
    pub fn search(charset: Option<Charset<'a>>, criteria: Vec1<SearchKey<'a>>, uid: bool) -> Self {
14✔
1717
        CommandBody::Search {
14✔
1718
            charset,
14✔
1719
            criteria,
14✔
1720
            uid,
14✔
1721
        }
14✔
1722
    }
14✔
1723

1724
    /// Construct a FETCH command.
1725
    pub fn fetch<S, I>(sequence_set: S, macro_or_item_names: I, uid: bool) -> Result<Self, S::Error>
8✔
1726
    where
8✔
1727
        S: TryInto<SequenceSet>,
8✔
1728
        I: Into<MacroOrMessageDataItemNames<'a>>,
8✔
1729
    {
8✔
1730
        let sequence_set = sequence_set.try_into()?;
8✔
1731

1732
        Ok(CommandBody::Fetch {
8✔
1733
            sequence_set,
8✔
1734
            macro_or_item_names: macro_or_item_names.into(),
8✔
1735
            uid,
8✔
1736
        })
8✔
1737
    }
8✔
1738

1739
    /// Construct a STORE command.
1740
    pub fn store<S>(
6✔
1741
        sequence_set: S,
6✔
1742
        kind: StoreType,
6✔
1743
        response: StoreResponse,
6✔
1744
        flags: Vec<Flag<'a>>,
6✔
1745
        uid: bool,
6✔
1746
    ) -> Result<Self, S::Error>
6✔
1747
    where
6✔
1748
        S: TryInto<SequenceSet>,
6✔
1749
    {
6✔
1750
        let sequence_set = sequence_set.try_into()?;
6✔
1751

1752
        Ok(CommandBody::Store {
6✔
1753
            sequence_set,
6✔
1754
            kind,
6✔
1755
            response,
6✔
1756
            flags,
6✔
1757
            uid,
6✔
1758
        })
6✔
1759
    }
6✔
1760

1761
    /// Construct a COPY command.
1762
    pub fn copy<S, M>(
4✔
1763
        sequence_set: S,
4✔
1764
        mailbox: M,
4✔
1765
        uid: bool,
4✔
1766
    ) -> Result<Self, CopyError<S::Error, M::Error>>
4✔
1767
    where
4✔
1768
        S: TryInto<SequenceSet>,
4✔
1769
        M: TryInto<Mailbox<'a>>,
4✔
1770
    {
4✔
1771
        Ok(CommandBody::Copy {
4✔
1772
            sequence_set: sequence_set.try_into().map_err(CopyError::Sequence)?,
4✔
1773
            mailbox: mailbox.try_into().map_err(CopyError::Mailbox)?,
4✔
1774
            uid,
4✔
1775
        })
1776
    }
4✔
1777

1778
    /// Get the name of the command.
1779
    pub fn name(&self) -> &'static str {
66✔
1780
        match self {
66✔
1781
            Self::Capability => "CAPABILITY",
2✔
1782
            Self::Noop => "NOOP",
2✔
1783
            Self::Logout => "LOGOUT",
2✔
1784
            #[cfg(feature = "starttls")]
1785
            Self::StartTLS => "STARTTLS",
2✔
1786
            Self::Authenticate { .. } => "AUTHENTICATE",
2✔
1787
            Self::Login { .. } => "LOGIN",
2✔
1788
            Self::Select { .. } => "SELECT",
2✔
1789
            Self::Sort { .. } => "SORT",
×
1790
            Self::Thread { .. } => "THREAD",
×
1791
            Self::Unselect => "UNSELECT",
2✔
1792
            Self::Examine { .. } => "EXAMINE",
2✔
1793
            Self::Create { .. } => "CREATE",
2✔
1794
            Self::Delete { .. } => "DELETE",
2✔
1795
            Self::Rename { .. } => "RENAME",
2✔
1796
            Self::Subscribe { .. } => "SUBSCRIBE",
2✔
1797
            Self::Unsubscribe { .. } => "UNSUBSCRIBE",
2✔
1798
            Self::List { .. } => "LIST",
2✔
1799
            Self::Lsub { .. } => "LSUB",
2✔
1800
            Self::Status { .. } => "STATUS",
2✔
1801
            Self::Append { .. } => "APPEND",
4✔
1802
            Self::Check => "CHECK",
2✔
1803
            Self::Close => "CLOSE",
2✔
1804
            Self::Expunge => "EXPUNGE",
2✔
1805
            Self::ExpungeUid { .. } => "EXPUNGE",
×
1806
            Self::Search { .. } => "SEARCH",
2✔
1807
            Self::Fetch { .. } => "FETCH",
2✔
1808
            Self::Store { .. } => "STORE",
2✔
1809
            Self::Copy { .. } => "COPY",
2✔
1810
            Self::Idle => "IDLE",
2✔
1811
            Self::Enable { .. } => "ENABLE",
2✔
1812
            Self::Compress { .. } => "COMPRESS",
2✔
1813
            Self::GetQuota { .. } => "GETQUOTA",
2✔
1814
            Self::GetQuotaRoot { .. } => "GETQUOTAROOT",
2✔
1815
            Self::SetQuota { .. } => "SETQUOTA",
2✔
1816
            Self::Move { .. } => "MOVE",
2✔
1817
            #[cfg(feature = "ext_id")]
1818
            Self::Id { .. } => "ID",
×
1819
            #[cfg(feature = "ext_metadata")]
1820
            Self::SetMetadata { .. } => "SETMETADATA",
×
1821
            #[cfg(feature = "ext_metadata")]
1822
            Self::GetMetadata { .. } => "GETMETADATA",
×
1823
        }
1824
    }
66✔
1825
}
1826

1827
/// Error-related types.
1828
pub mod error {
1829
    use thiserror::Error;
1830

1831
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
4✔
1832
    pub enum LoginError<U, P> {
1833
        #[error("Invalid username: {0}")]
1834
        Username(U),
1835
        #[error("Invalid password: {0}")]
1836
        Password(P),
1837
    }
1838

1839
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
×
1840
    pub enum RenameError<F, T> {
1841
        #[error("Invalid (from) mailbox: {0}")]
1842
        From(F),
1843
        #[error("Invalid (to) mailbox: {0}")]
1844
        To(T),
1845
    }
1846

1847
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
×
1848
    pub enum ListError<R, M> {
1849
        #[error("Invalid reference: {0}")]
1850
        Reference(R),
1851
        #[error("Invalid mailbox: {0}")]
1852
        Mailbox(M),
1853
    }
1854

1855
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
×
1856
    pub enum AppendError<M, D> {
1857
        #[error("Invalid mailbox: {0}")]
1858
        Mailbox(M),
1859
        #[error("Invalid data: {0}")]
1860
        Data(D),
1861
    }
1862

1863
    #[derive(Clone, Debug, Eq, Error, Hash, Ord, PartialEq, PartialOrd)]
×
1864
    pub enum CopyError<S, M> {
1865
        #[error("Invalid sequence: {0}")]
1866
        Sequence(S),
1867
        #[error("Invalid mailbox: {0}")]
1868
        Mailbox(M),
1869
    }
1870
}
1871

1872
#[cfg(test)]
1873
mod tests {
1874
    use chrono::DateTime as ChronoDateTime;
1875

1876
    use super::*;
1877
    use crate::{
1878
        auth::AuthMechanism,
1879
        core::{AString, Charset, IString, Literal, LiteralMode, Vec1},
1880
        datetime::DateTime,
1881
        extensions::{
1882
            binary::Literal8,
1883
            compress::CompressionAlgorithm,
1884
            enable::{CapabilityEnable, Utf8Kind},
1885
        },
1886
        fetch::{Macro, MacroOrMessageDataItemNames, MessageDataItemName, Part, Section},
1887
        flag::{Flag, StoreType},
1888
        mailbox::{ListMailbox, Mailbox},
1889
        search::SearchKey,
1890
        secret::Secret,
1891
        sequence::{SeqOrUid, Sequence, SequenceSet},
1892
        status::StatusDataItemName,
1893
    };
1894

1895
    #[test]
1896
    fn test_conversion_command_body() {
2✔
1897
        let cmds = vec![
2✔
1898
            CommandBody::Capability,
2✔
1899
            CommandBody::Noop,
2✔
1900
            CommandBody::Logout,
2✔
1901
            #[cfg(feature = "starttls")]
2✔
1902
            CommandBody::StartTLS,
2✔
1903
            CommandBody::authenticate(AuthMechanism::Plain),
2✔
1904
            CommandBody::authenticate(AuthMechanism::Login),
2✔
1905
            CommandBody::authenticate_with_ir(AuthMechanism::Plain, b"XXXXXXXX".as_ref()),
2✔
1906
            CommandBody::authenticate_with_ir(AuthMechanism::Login, b"YYYYYYYY".as_ref()),
2✔
1907
            CommandBody::login("alice", "I_am_an_atom").unwrap(),
2✔
1908
            CommandBody::login("alice", "I am \\ \"quoted\"").unwrap(),
2✔
1909
            CommandBody::login("alice", "I am a literal²").unwrap(),
2✔
1910
            CommandBody::login(
2✔
1911
                AString::Atom("alice".try_into().unwrap()),
2✔
1912
                AString::String(crate::core::IString::Literal(
2✔
1913
                    vec![0xff, 0xff, 0xff].try_into().unwrap(),
2✔
1914
                )),
2✔
1915
            )
2✔
1916
            .unwrap(),
2✔
1917
            CommandBody::select("inbox").unwrap(),
2✔
1918
            CommandBody::select("atom").unwrap(),
2✔
1919
            CommandBody::select("C:\\").unwrap(),
2✔
1920
            CommandBody::select("²").unwrap(),
2✔
1921
            CommandBody::select("Trash").unwrap(),
2✔
1922
            CommandBody::examine("inbox").unwrap(),
2✔
1923
            CommandBody::examine("atom").unwrap(),
2✔
1924
            CommandBody::examine("C:\\").unwrap(),
2✔
1925
            CommandBody::examine("²").unwrap(),
2✔
1926
            CommandBody::examine("Trash").unwrap(),
2✔
1927
            CommandBody::create("inBoX").unwrap(),
2✔
1928
            CommandBody::delete("inBOX").unwrap(),
2✔
1929
            CommandBody::rename("iNBoS", "INboX").unwrap(),
2✔
1930
            CommandBody::subscribe("inbox").unwrap(),
2✔
1931
            CommandBody::unsubscribe("INBOX").unwrap(),
2✔
1932
            CommandBody::list("iNbOx", "test").unwrap(),
2✔
1933
            CommandBody::list("inbox", ListMailbox::Token("test".try_into().unwrap())).unwrap(),
2✔
1934
            CommandBody::lsub(
2✔
1935
                "inbox",
2✔
1936
                ListMailbox::String(IString::Quoted("\x7f".try_into().unwrap())),
2✔
1937
            )
2✔
1938
            .unwrap(),
2✔
1939
            CommandBody::list("inBoX", ListMailbox::Token("test".try_into().unwrap())).unwrap(),
2✔
1940
            CommandBody::lsub(
2✔
1941
                "INBOX",
2✔
1942
                ListMailbox::String(IString::Quoted("\x7f".try_into().unwrap())),
2✔
1943
            )
2✔
1944
            .unwrap(),
2✔
1945
            CommandBody::status("inbox", vec![StatusDataItemName::Messages]).unwrap(),
2✔
1946
            CommandBody::append(
2✔
1947
                "inbox",
2✔
1948
                vec![],
2✔
1949
                Some(
2✔
1950
                    DateTime::try_from(
2✔
1951
                        ChronoDateTime::parse_from_rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")
2✔
1952
                            .unwrap(),
2✔
1953
                    )
2✔
1954
                    .unwrap(),
2✔
1955
                ),
2✔
1956
                vec![0xff, 0xff, 0xff],
2✔
1957
            )
2✔
1958
            .unwrap(),
2✔
1959
            CommandBody::append(
2✔
1960
                "inbox",
2✔
1961
                vec![Flag::Keyword("test".try_into().unwrap())],
2✔
1962
                Some(
2✔
1963
                    DateTime::try_from(
2✔
1964
                        ChronoDateTime::parse_from_rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")
2✔
1965
                            .unwrap(),
2✔
1966
                    )
2✔
1967
                    .unwrap(),
2✔
1968
                ),
2✔
1969
                vec![0xff, 0xff, 0xff],
2✔
1970
            )
2✔
1971
            .unwrap(),
2✔
1972
            CommandBody::Check,
2✔
1973
            CommandBody::Close,
2✔
1974
            CommandBody::Expunge,
2✔
1975
            CommandBody::search(
2✔
1976
                None,
2✔
1977
                Vec1::from(SearchKey::And(
2✔
1978
                    vec![SearchKey::All, SearchKey::New, SearchKey::Unseen]
2✔
1979
                        .try_into()
2✔
1980
                        .unwrap(),
2✔
1981
                )),
2✔
1982
                false,
2✔
1983
            ),
2✔
1984
            CommandBody::search(
2✔
1985
                None,
2✔
1986
                Vec1::from(SearchKey::And(
2✔
1987
                    vec![SearchKey::All, SearchKey::New, SearchKey::Unseen]
2✔
1988
                        .try_into()
2✔
1989
                        .unwrap(),
2✔
1990
                )),
2✔
1991
                true,
2✔
1992
            ),
2✔
1993
            CommandBody::search(
2✔
1994
                None,
2✔
1995
                Vec1::from(SearchKey::And(
2✔
1996
                    vec![SearchKey::SequenceSet(SequenceSet(
2✔
1997
                        vec![Sequence::Single(SeqOrUid::Value(42.try_into().unwrap()))]
2✔
1998
                            .try_into()
2✔
1999
                            .unwrap(),
2✔
2000
                    ))]
2✔
2001
                    .try_into()
2✔
2002
                    .unwrap(),
2✔
2003
                )),
2✔
2004
                true,
2✔
2005
            ),
2✔
2006
            CommandBody::search(
2✔
2007
                None,
2✔
2008
                Vec1::from(SearchKey::SequenceSet("42".try_into().unwrap())),
2✔
2009
                true,
2✔
2010
            ),
2✔
2011
            CommandBody::search(
2✔
2012
                None,
2✔
2013
                Vec1::from(SearchKey::SequenceSet("*".try_into().unwrap())),
2✔
2014
                true,
2✔
2015
            ),
2✔
2016
            CommandBody::search(
2✔
2017
                None,
2✔
2018
                Vec1::from(SearchKey::Or(
2✔
2019
                    Box::new(SearchKey::Draft),
2✔
2020
                    Box::new(SearchKey::All),
2✔
2021
                )),
2✔
2022
                true,
2✔
2023
            ),
2✔
2024
            CommandBody::search(
2✔
2025
                Some(Charset::try_from("UTF-8").unwrap()),
2✔
2026
                Vec1::from(SearchKey::Or(
2✔
2027
                    Box::new(SearchKey::Draft),
2✔
2028
                    Box::new(SearchKey::All),
2✔
2029
                )),
2✔
2030
                true,
2✔
2031
            ),
2✔
2032
            CommandBody::fetch(
2✔
2033
                "1",
2✔
2034
                vec![MessageDataItemName::BodyExt {
2✔
2035
                    partial: None,
2✔
2036
                    section: Some(Section::Part(Part(
2✔
2037
                        vec![1.try_into().unwrap(), 1.try_into().unwrap()]
2✔
2038
                            .try_into()
2✔
2039
                            .unwrap(),
2✔
2040
                    ))),
2✔
2041
                    peek: true,
2✔
2042
                }],
2✔
2043
                false,
2✔
2044
            )
2✔
2045
            .unwrap(),
2✔
2046
            CommandBody::fetch("1:*,2,3", Macro::Full, true).unwrap(),
2✔
2047
            CommandBody::store(
2✔
2048
                "1,2:*",
2✔
2049
                StoreType::Remove,
2✔
2050
                StoreResponse::Answer,
2✔
2051
                vec![Flag::Seen, Flag::Draft],
2✔
2052
                false,
2✔
2053
            )
2✔
2054
            .unwrap(),
2✔
2055
            CommandBody::store(
2✔
2056
                "1:5",
2✔
2057
                StoreType::Add,
2✔
2058
                StoreResponse::Answer,
2✔
2059
                vec![Flag::Keyword("TEST".try_into().unwrap())],
2✔
2060
                true,
2✔
2061
            )
2✔
2062
            .unwrap(),
2✔
2063
            CommandBody::copy("1", "inbox", false).unwrap(),
2✔
2064
            CommandBody::copy("1337", "archive", true).unwrap(),
2✔
2065
        ];
2✔
2066

2067
        for (no, cmd_body) in cmds.into_iter().enumerate() {
102✔
2068
            println!("Test: {}, {:?}", no, cmd_body);
102✔
2069

102✔
2070
            let _ = cmd_body.tag(format!("A{}", no)).unwrap();
102✔
2071
        }
102✔
2072
    }
2✔
2073

2074
    #[test]
2075
    fn test_command_body_name() {
2✔
2076
        let tests = [
2✔
2077
            (CommandBody::Capability, "CAPABILITY"),
2✔
2078
            (CommandBody::Noop, "NOOP"),
2✔
2079
            (CommandBody::Logout, "LOGOUT"),
2✔
2080
            #[cfg(feature = "starttls")]
2✔
2081
            (CommandBody::StartTLS, "STARTTLS"),
2✔
2082
            (
2✔
2083
                CommandBody::Authenticate {
2✔
2084
                    mechanism: AuthMechanism::Plain,
2✔
2085
                    initial_response: None,
2✔
2086
                },
2✔
2087
                "AUTHENTICATE",
2✔
2088
            ),
2✔
2089
            (
2✔
2090
                CommandBody::Login {
2✔
2091
                    username: AString::try_from("user").unwrap(),
2✔
2092
                    password: Secret::new(AString::try_from("pass").unwrap()),
2✔
2093
                },
2✔
2094
                "LOGIN",
2✔
2095
            ),
2✔
2096
            (
2✔
2097
                CommandBody::Select {
2✔
2098
                    mailbox: Mailbox::Inbox,
2✔
2099
                },
2✔
2100
                "SELECT",
2✔
2101
            ),
2✔
2102
            (CommandBody::Unselect, "UNSELECT"),
2✔
2103
            (
2✔
2104
                CommandBody::Examine {
2✔
2105
                    mailbox: Mailbox::Inbox,
2✔
2106
                },
2✔
2107
                "EXAMINE",
2✔
2108
            ),
2✔
2109
            (
2✔
2110
                CommandBody::Create {
2✔
2111
                    mailbox: Mailbox::Inbox,
2✔
2112
                },
2✔
2113
                "CREATE",
2✔
2114
            ),
2✔
2115
            (
2✔
2116
                CommandBody::Delete {
2✔
2117
                    mailbox: Mailbox::Inbox,
2✔
2118
                },
2✔
2119
                "DELETE",
2✔
2120
            ),
2✔
2121
            (
2✔
2122
                CommandBody::Rename {
2✔
2123
                    from: Mailbox::Inbox,
2✔
2124
                    to: Mailbox::Inbox,
2✔
2125
                },
2✔
2126
                "RENAME",
2✔
2127
            ),
2✔
2128
            (
2✔
2129
                CommandBody::Subscribe {
2✔
2130
                    mailbox: Mailbox::Inbox,
2✔
2131
                },
2✔
2132
                "SUBSCRIBE",
2✔
2133
            ),
2✔
2134
            (
2✔
2135
                CommandBody::Unsubscribe {
2✔
2136
                    mailbox: Mailbox::Inbox,
2✔
2137
                },
2✔
2138
                "UNSUBSCRIBE",
2✔
2139
            ),
2✔
2140
            (
2✔
2141
                CommandBody::List {
2✔
2142
                    reference: Mailbox::Inbox,
2✔
2143
                    mailbox_wildcard: ListMailbox::try_from("").unwrap(),
2✔
2144
                },
2✔
2145
                "LIST",
2✔
2146
            ),
2✔
2147
            (
2✔
2148
                CommandBody::Lsub {
2✔
2149
                    reference: Mailbox::Inbox,
2✔
2150
                    mailbox_wildcard: ListMailbox::try_from("").unwrap(),
2✔
2151
                },
2✔
2152
                "LSUB",
2✔
2153
            ),
2✔
2154
            (
2✔
2155
                CommandBody::Status {
2✔
2156
                    mailbox: Mailbox::Inbox,
2✔
2157
                    item_names: vec![].into(),
2✔
2158
                },
2✔
2159
                "STATUS",
2✔
2160
            ),
2✔
2161
            (
2✔
2162
                CommandBody::Append {
2✔
2163
                    mailbox: Mailbox::Inbox,
2✔
2164
                    flags: vec![],
2✔
2165
                    date: None,
2✔
2166
                    message: LiteralOrLiteral8::Literal(Literal::try_from("").unwrap()),
2✔
2167
                },
2✔
2168
                "APPEND",
2✔
2169
            ),
2✔
2170
            (
2✔
2171
                CommandBody::Append {
2✔
2172
                    mailbox: Mailbox::Inbox,
2✔
2173
                    flags: vec![],
2✔
2174
                    date: None,
2✔
2175
                    message: LiteralOrLiteral8::Literal8(Literal8 {
2✔
2176
                        data: b"Hello\x00World\x00".as_ref().into(),
2✔
2177
                        mode: LiteralMode::NonSync,
2✔
2178
                    }),
2✔
2179
                },
2✔
2180
                "APPEND",
2✔
2181
            ),
2✔
2182
            (CommandBody::Check, "CHECK"),
2✔
2183
            (CommandBody::Close, "CLOSE"),
2✔
2184
            (CommandBody::Expunge, "EXPUNGE"),
2✔
2185
            (
2✔
2186
                CommandBody::Search {
2✔
2187
                    charset: None,
2✔
2188
                    criteria: Vec1::from(SearchKey::Recent),
2✔
2189
                    uid: true,
2✔
2190
                },
2✔
2191
                "SEARCH",
2✔
2192
            ),
2✔
2193
            (
2✔
2194
                CommandBody::Fetch {
2✔
2195
                    sequence_set: SequenceSet::try_from(1u32).unwrap(),
2✔
2196
                    macro_or_item_names: MacroOrMessageDataItemNames::Macro(Macro::Full),
2✔
2197
                    uid: true,
2✔
2198
                },
2✔
2199
                "FETCH",
2✔
2200
            ),
2✔
2201
            (
2✔
2202
                CommandBody::Store {
2✔
2203
                    sequence_set: SequenceSet::try_from(1).unwrap(),
2✔
2204
                    flags: vec![],
2✔
2205
                    response: StoreResponse::Silent,
2✔
2206
                    kind: StoreType::Add,
2✔
2207
                    uid: true,
2✔
2208
                },
2✔
2209
                "STORE",
2✔
2210
            ),
2✔
2211
            (
2✔
2212
                CommandBody::Copy {
2✔
2213
                    sequence_set: SequenceSet::try_from(1).unwrap(),
2✔
2214
                    mailbox: Mailbox::Inbox,
2✔
2215
                    uid: true,
2✔
2216
                },
2✔
2217
                "COPY",
2✔
2218
            ),
2✔
2219
            (CommandBody::Idle, "IDLE"),
2✔
2220
            (
2✔
2221
                CommandBody::Enable {
2✔
2222
                    capabilities: Vec1::from(CapabilityEnable::Utf8(Utf8Kind::Only)),
2✔
2223
                },
2✔
2224
                "ENABLE",
2✔
2225
            ),
2✔
2226
            (
2✔
2227
                CommandBody::Compress {
2✔
2228
                    algorithm: CompressionAlgorithm::Deflate,
2✔
2229
                },
2✔
2230
                "COMPRESS",
2✔
2231
            ),
2✔
2232
            (
2✔
2233
                CommandBody::GetQuota {
2✔
2234
                    root: AString::try_from("root").unwrap(),
2✔
2235
                },
2✔
2236
                "GETQUOTA",
2✔
2237
            ),
2✔
2238
            (
2✔
2239
                CommandBody::GetQuotaRoot {
2✔
2240
                    mailbox: Mailbox::Inbox,
2✔
2241
                },
2✔
2242
                "GETQUOTAROOT",
2✔
2243
            ),
2✔
2244
            (
2✔
2245
                CommandBody::SetQuota {
2✔
2246
                    root: AString::try_from("root").unwrap(),
2✔
2247
                    quotas: vec![],
2✔
2248
                },
2✔
2249
                "SETQUOTA",
2✔
2250
            ),
2✔
2251
            (
2✔
2252
                CommandBody::Move {
2✔
2253
                    sequence_set: SequenceSet::try_from(1).unwrap(),
2✔
2254
                    mailbox: Mailbox::Inbox,
2✔
2255
                    uid: true,
2✔
2256
                },
2✔
2257
                "MOVE",
2✔
2258
            ),
2✔
2259
        ];
2✔
2260

2261
        for (test, expected) in tests {
68✔
2262
            assert_eq!(test.name(), expected);
66✔
2263
        }
2264
    }
2✔
2265
}
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