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

71 lines
2.5 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
/// An option value, which can be a single string or a nested array of values.
///
/// Allows option values to be nested arbitrarily deep, enabling representation
/// of complex structured data in the RFC 4466 extension mechanism. Values can be simple strings
/// or arrays of other option values, supporting flexible data representation.
///
/// - SeeAlso: [RFC 4466 IMAP4 Extensions](https://datatracker.ietf.org/doc/html/rfc4466)
public enum OptionValueComp: Hashable, Sendable {
/// A single value
case string(ByteBuffer)
/// An array of values
case array([OptionValueComp])
}
// MARK: - Conveniences
extension OptionValueComp: ExpressibleByArrayLiteral {
/// Option values can be nested, so this provides recursion.
public typealias ArrayLiteralElement = Self
/// Creates a new `OptionValueComp` from the given elements.
/// - parameter elements: The contents of the array.
public init(arrayLiteral elements: OptionValueComp...) {
let array = Array(elements)
self = .array(array)
}
}
// MARK: - Encoding
extension EncodeBuffer {
/// `option-value = "(" option-val-comp ")"`. Its parentheses enclose a sequence, so several
/// values aren't parenthesized again: `.array(["a", "b"])` is `("a" "b")`, not `(("a" "b"))`.
@discardableResult mutating func writeOptionValue(_ value: OptionValueComp) -> Int {
guard case .array(let array) = value, array.count > 1 else {
return self.writeString("(") + self.writeOptionValueComp(value) + self.writeString(")")
}
return self.writeArray(array) { (option, self) in
self.writeOptionValueComp(option)
}
}
@discardableResult mutating func writeOptionValueComp(_ option: OptionValueComp) -> Int {
switch option {
case .string(let string):
return self.writeIMAPString(string)
case .array(let array):
return self.writeArray(array) { (option, self) in
self.writeOptionValueComp(option)
}
}
}
}