Files
swift-nio-imap/Sources/NIOIMAPCore/NIOIMAPCore.docc/NIOIMAPCore.md
T
Daniel Eggert 3e2faedee1 Drop NIO from NIOIMAPCore (mostly scripted)
NIOIMAPCore and its tests no longer depend on swift-nio; NIOIMAP declares
the dependency it was getting transitively. Hand edits: Package.swift and
NIOIMAPCore.md. Everything else is the output of, on the parent
commit (zsh):

  F=($(grep -rlE '^import ((struct|enum|class|protocol) )?NIO(Core|TestUtils)?(\.[A-Za-z]+)?$' \
      Sources/NIOIMAPCore Tests/NIOIMAPCoreTests))
  perl -0pi -e 's/^import (?:(?:struct|enum|class|protocol) )?NIO(?:Core|TestUtils)?(?:\.\w+)?\n//mg;
      s/\n\n\n+/\n\n/g' $F

Public break: depending on NIOIMAPCore no longer provides swift-nio.
2026-11-25 21:19:41 +09:00

2.9 KiB

NIOIMAPCore

Parse and encode IMAP4rev1 protocol messages with type safety.

Overview

NIOIMAPCore is the foundation of the swift-nio-imap project. It provides comprehensive, type-safe parsing and encoding of IMAP4rev1 protocol messages as described in RFC 3501 and related RFCs.

Use this module as a building block for creating IMAP clients and servers. It handles the complexity of the IMAP wire protocol, converting between byte streams and strongly-typed Swift data structures.

Key Features

  • Type-Safe Protocol Representation: IMAP commands, responses, and all protocol elements are represented as type-safe Swift enums and structures
  • Complete Protocol Support: Full IMAP4rev1 implementation with extensive support for common extensions (see doc:SupportedExtensions)
  • Bidirectional Conversion: Convert between IMAP wire format and Swift data structures in both directions
  • Streaming Support: Handle large messages and streaming responses efficiently
  • No Networking Dependency: The module depends on the standard library and the Swift Collections packages, not on SwiftNIO. Read from an IMAPInputBuffer filled from any transport and write into any EncoderOutputBuffer.
  • Parser Combinator Architecture: Composable, maintainable parsing logic built on reusable parser primitives

How It Works

The IMAP protocol is text-based and involves continuous streams of commands and responses. NIOIMAPCore abstracts this complexity:

  1. Commands: Client commands are represented as strongly-typed values (e.g., Command.login(username:password:))
  2. Responses: Server responses are parsed into comprehensive response types (e.g., Response.untagged(.mailboxData(.exists(count))))
  3. Encoding: Swift types are automatically encoded back to proper IMAP wire format

Example

Here's an example of parsing and encoding an IMAP response:

// Parse: "* 18 EXISTS"
let response: Response = .untagged(.mailboxData(.exists(18)))

// Encode back to wire format
var buffer = ResponseEncodeBuffer()
buffer.writeResponse(response)
// Result: "* 18 EXISTS\r\n"

Module Separation

NIOIMAPCore focuses purely on protocol parsing and encoding, and it does that without a networking stack: it names no SwiftNIO type and does not depend on the package. For SwiftNIO integration, the package layers two client interfaces on top — offered on purpose, with neither preferred over the other:

  • The NIOIMAP module provides event-driven SwiftNIO ChannelHandlers and ByteBuffer conformances. It re-exports NIOIMAPCore.
  • The IMAPCommands module provides a high-level, command-centric async/await client built on top of NIOIMAP.

Both share the NIOIMAPCore command and response types described here.

Topics

Guides

Collections