Files
swift-nio-imap/Sources/NIOIMAPCore/NIOIMAPCore.docc/ParsingAndEncoding.md
T
Daniel Eggert e4d23f0123 Add parser input types
ParseInput: a ~Escapable, BitwiseCopyable cursor over a table of region
pointers, with the primitives and combinators as methods. Input buffers:
ParserInputBuffer (+ Consumable, StableRegion refinements), conformances
for ByteBuffer / Array / Data, and IMAPInputBuffer, the staging deque that
can hand out payloads by reference.

IMAPInputBuffer keeps reads of at least copyThreshold (1 KiB) by
reference and copies smaller ones into a 1 KiB tail (an OutputSegment),
so a peer sending a byte per read costs a region per KiB, not a region,
an object and a retained receive buffer per byte. A tail is written
only past its end, only while the buffer holds the sole reference, and
never reallocated; a used-up tail with room is continued by the next
small read into an empty buffer. Only a StableAddressByteRegion (arrays,
ByteBuffer) or an IMAPBuffer over one is kept by reference; any other
large region, e.g. Data, is copied once.

A bare DispatchData.Region is copied too: it points into its
DispatchData without keeping it alive, so it is not a
StableAddressByteRegion. append(contentsOf:) is the zero-copy path for
DispatchData.

Public API additions: IMAPInputBuffer.append(contentsOf: DispatchData)
walks a read's regions with enumerateBytes, so it builds no regions
array; a large region is kept with the DispatchData that owns it (also
the owner a makeDispatchData() donation retains), small ones go to the
tail. append(_:) gains overloads for a StableAddressByteRegion and an
IMAPBuffer, so appending a ByteBuffer, an array or an IMAPBuffer pays no
dynamic cast; IMAPByteStorage.init(_:) gains one for an IMAPBuffer.

Additive; the grammar still runs on ParseBuffer. Review focus: _span(_:)
and RegionSink (the two read-side unsafe seams), the head invariant in
advance(by:), the gather's 64-frame recursion cap,
IMAPInputBuffer.writableTail(fitting:), and DispatchDataRegion, what
append(contentsOf:) keeps: its @unchecked Sendable and its
StableAddressByteRegion (fixed-address) promise.

Temporary: main's SubParser is renamed LegacySubParser so both coexist.
2026-11-21 15:39:25 +01:00

1.6 KiB

Parsing and Encoding

Parse IMAP server responses and encode client commands using these streaming parsers and encode buffers.

Overview

NIOIMAPCore provides comprehensive parsing and encoding capabilities. These types handle the conversion between the IMAP wire format and strongly-typed Swift structures, including error handling and advanced features like pipelining.

Topics

Parsers

Streaming parsers for different contexts.

  • ResponseParser
  • CommandParser
  • SynchronizingLiteralParser

Encoders

Buffers for encoding protocol elements back to wire format.

  • CommandEncodeBuffer
  • ResponseEncodeBuffer
  • CommandEncodingOptions
  • ResponseEncodingOptions

Bytes

Types for carrying raw message payloads without copying them.

  • IMAPBuffer
  • IMAPBufferList
  • IMAPByteStorage
  • ByteRegion
  • StableAddressByteRegion

Parser input

Where received bytes wait to be parsed, and how the parser reads them.

  • IMAPInputBuffer
  • ParserInputBuffer
  • ConsumableParserInputBuffer
  • StableRegionParserInputBuffer
  • ParseInput
  • RegionSink

Encoder output

Where encoded bytes go.

  • EncoderOutputBuffer
  • IMAPOutputBuffer

Pipelining

Types related to command pipelining behavior.

  • PipeliningRequirement
  • PipeliningBehavior

Errors

Parsing and encoding errors that may occur.

  • BadCommand
  • ParserError
  • TooMuchRecursion
  • ExceededMaximumMessageAttributesError
  • ExceededMaximumBodySizeError
  • ExceededLiteralSizeLimitError

Utilities

Utility types and protocol defaults.

  • ModifiedUTF7
  • IMAPDefaults