Expand description
Read and write classic local-mail storage formats.
Covered:
- mbox (RFC 4155) — all four variants:
Mboxo,Mboxrd,Mboxcl,Mboxcl2. Streaming reader and writer. - Maildir (DJB spec) — atomic delivery, flag parsing, basic
cur//new//tmp/iteration. - Locking for mbox concurrent access:
LockStrategy::{Dotlock, Flock, Fcntl, FcntlThenDotlock}plus the explicitNone. Unix first-class; Windows degradation is documented.
§Quickstart
use std::io::Cursor;
use mailbox_formats::{MboxReader, MboxVariant, RawMessage};
let bytes = b"From alice@example.com Tue Mar 17 09:30:00 2026\r\n\
From: Alice <alice@example.com>\r\n\
Subject: Hello\r\n\r\n\
Hi there.\r\n\r\n";
let mut reader = MboxReader::new(Cursor::new(&bytes[..]), MboxVariant::Mboxrd);
let msg: RawMessage = reader.next().expect("one message")?;
assert_eq!(msg.header("Subject"), Some(&b"Hello"[..]));§Design choices
- Byte-preserving:
RawMessageholds headers asVec<(String, Vec<u8>)>and the body asVec<u8>. No MIME decoding, no charset assumptions. Pair withmail-parserif you need decoded headers. - Streaming: the mbox reader is
BufRead-based with bounded memory; a 10GB mbox streams without loading into memory. - Honest variants:
MboxVariant::Autosniffs the first few messages forContent-Length:headers and>Fromescaping. If it can’t tell, it returnsError::UndetectedMboxVariantrather than guessing. - Locking is explicit: callers pick a
LockStrategy; the default isLockStrategy::None(caller responsibility).
§Out of scope (v0.1.0)
- MH format: rare in modern environments; add only if users file an issue.
- Maildir++ subfolders (
.foo.bar/): basic Maildir only. - MIME parsing or header decoding: keep
mailbox-formatsfocused on file-format semantics.
Structs§
- Flags
- IMAP-style flags shared across mbox (when reading
Status:/X-Status:headers) and Maildir (filename suffix encoding). - Lock
- RAII guard that releases the lock on drop.
- Maildir
- Handle to a Maildir directory.
- Maildir
Entry - One Maildir entry. Path is the on-disk location;
unique_idis the portion before the separator;flagsis parsed from the suffix. - Mbox
Reader - Streaming reader. Yields one
RawMessageper call tonext. - Mbox
Writer - Writes
RawMessagevalues to an underlyingWritein the requested mbox variant. - RawMessage
- One message as it lives in an mbox file or a Maildir folder.
Enums§
- Error
- Errors returned by the mailbox-formats crate.
- Lock
Strategy - Locking policy. Default is
LockStrategy::None— the caller takes responsibility. - Mbox
Variant - Which mbox dialect the reader/writer should use.
Type Aliases§
- Result
- Crate-local result alias.