Files
Daniel Eggert d67ffdf153 Fix encode/parse round-trip bugs (#868)
* Separate LIST from its independent select options

`.listIndependent([.remote], …)` encoded as `LIST(REMOTE) …`.

RFC 5258: `list = "LIST" [SP list-select-opts] SP mailbox SP mbox-or-pat`.

* Keep leading zeros in fractional seconds

`12:34:56.05` parsed as `fraction: 5` and re-encoded as `12:34:56.5`,
changing the value.

RFC 3339: `time-secfrac = "." 1*DIGIT`. `FullTime.fraction` becomes
`FullTime.Fraction`, which stores the digits; `init?(_:)` validates,
literals trap, `String.init` reads.

* Model the RFC 3339 time offset

`;EXPIRE=2025-12-31T23:59:59Z` failed to parse inside a URL: the
parser stopped before `Z`. The encoder never wrote an offset.

RFC 4467: `expire = ";EXPIRE=" date-time`; RFC 3339: `full-time =
partial-time time-offset`, `time-offset = "Z" / time-numoffset`. Adds
the required `FullTime.offset`.

* Encode LIST extended data

`MailboxInfo.extensions` was parsed but never written, so
`* LIST (\Marked) "/" "Fruit" ("CHILDINFO" ("SUBSCRIBED"))` lost
`("CHILDINFO" …)` on re-encode.

RFC 5258: `mailbox-list = … mailbox [SP mbox-list-extended]`.

* Round-trip multi-value LIST option values

`.array(["a", "b"])` encoded as `(("a" "b"))`; the parser rejected both
that and `("a" "b")`: it took the `astring` branch first and could not
backtrack.

RFC 5258: `option-value = "(" option-val-comp ")"`, `option-val-comp =
astring / option-val-comp *(SP option-val-comp) / "(" option-val-comp
")"`. The parser reads a sequence of elements; the encoder writes
several values inside the `option-value` parentheses.

* Validate JMAPACCESS URLs

`.jmapAccess(URL(string: "jmap", relativeTo: base))` encoded as
`JMAPACCESS "jmap -- https://example.com/"` (`URL.description`); `http`
URLs were written though the parser rejects them.

The payload becomes `ResponsePayload.JMAPAccessURL`: `init?(_:)` resolves
against the base and requires `https`, a host, and no escaping inside
`quoted`; `URL.init` reads. The parser uses the same check.

* Make RESETKEY mechanisms require a mailbox

`.resetKey(mailbox: nil, mechanisms: [.internal])` encoded as bare
`RESETKEY`, silently dropping the mechanisms and resetting every key.

RFC 4467: `resetkey = "RESETKEY" [SP mailbox *(SP mechanism)]`. The
payload becomes `Command.ResetKeyScope`: `.allMailboxes` or
`.mailbox(_:mechanisms:)`.

* Hoist a #require the compiler flagged as redundant

* Cite RFC 7162 for SEARCH MODSEQ entry types

The docs described these as RFC 5464 GETMETADATA options, with
GETMETADATA examples. The grammar is RFC 7162 `search-modseq-ext`.

* Fix doc nits

- `ExtendedSearchScopeOptions.content` is a dictionary, not an array.
- `NamespaceDescription` init in the `NamespaceResponse` example needs
  `try` and `responseExtensions:`.
- `TaggedExtensionLabel` example: `SELECT INBOX (CONDSTORE)` parses as
  `.condStore`; use `RELEVANCY`.
- Stray `’` in a parser hint.
2026-11-04 17:41:53 +00:00

61 lines
2.6 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import struct NIO.ByteBuffer
import struct OrderedCollections.OrderedDictionary
/// Optional scope options for extended multi-mailbox search operations (RFC 7377).
///
/// **Requires server capability:** ``Capability/multiSearch``
///
/// Scope options provide a catch-all mechanism for future extensions to the MULTIMAILBOX SEARCH protocol,
/// allowing servers to support additional scope-related parameters beyond those explicitly defined.
/// These options are specified as key-value pairs and are optional in search commands.
/// See [RFC 7377 Section 2.1.1](https://datatracker.ietf.org/doc/html/rfc7377#section-2.1.1) for details.
///
/// ## Related types
///
/// - See ``ExtendedSearchSourceOptions`` for mailbox selection
/// - See ``ExtendedSearchOptions`` for complete search options
///
/// - SeeAlso: [RFC 7377](https://datatracker.ietf.org/doc/html/rfc7377)
public struct ExtendedSearchScopeOptions: Hashable, Sendable {
/// An ordered dictionary of scope option key-value pairs.
///
/// The dictionary must contain at least one key-value pair and is never empty. This provides
/// a flexible mechanism for supporting scope parameters not yet defined in the base specification.
public let content: OrderedDictionary<ExtensionKey<ExtendedSearchScopeOptions>, ParameterValue?>
/// Creates a new `ExtendedSearchScopeOptions` from one or more scope options.
///
/// - parameter options: One or more key-value scope option pairs. Must not be empty.
/// - returns: A new `ExtendedSearchScopeOptions` if `options` is non-empty, otherwise `nil`.
init?(_ options: OrderedDictionary<ExtensionKey<ExtendedSearchScopeOptions>, ParameterValue?>) {
guard options.count >= 1 else {
return nil
}
self.content = options
}
}
// MARK: - Encoding
extension EncodeBuffer {
@discardableResult mutating func writeExtendedSearchScopeOptions(_ options: ExtendedSearchScopeOptions) -> Int {
self.writeOrderedDictionary(options.content, parenthesis: false) { (option, buffer) -> Int in
buffer.writeParameter(option)
}
}
}