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.
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
IMAPInputBufferfilled from any transport and write into anyEncoderOutputBuffer. - 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:
- Commands: Client commands are represented as strongly-typed values (e.g.,
Command.login(username:password:)) - Responses: Server responses are parsed into comprehensive response types (e.g.,
Response.untagged(.mailboxData(.exists(count)))) - 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
NIOIMAPmodule provides event-driven SwiftNIOChannelHandlers andByteBufferconformances. It re-exports NIOIMAPCore. - The
IMAPCommandsmodule provides a high-level, command-centricasync/awaitclient built on top ofNIOIMAP.
Both share the NIOIMAPCore command and response types described here.