netpkt/imap.rs#
Source: lib/netpkt/imap.rs
imap -- IMAP without I/O (a subset of RFC 9051 and RFC 3501): command and response lines with
tags, quoted strings, literals and parenthesized lists, the readers of both directions, sequence
sets, flags, FETCH items, SEARCH keys and the ENVELOPE of a message
PORT_IMAP (143), verb_known
Item Atom | Quoted | Literal | List | Nil: one value of a line; text()
Line { text, lits } a whole logical line: the bytes with its literals apart
LineReader bytes in any pieces: push(b) then next() -> Some(Read::Line(line)) or
Some(Read::NeedLiteral(n)), a client that waits for "+" (a server
answers it)
tokens(&Line) -> Vec<Item> the values of a line
Command { tag, verb, args } Command::parse(&line) (the verb in capitals); line(tag, verb,
args) the bytes of a simple command (CR, LF, NUL left out but for
whole literals)
Status, Kind, Response Response::parse(&line): a tagged status, an untagged one, untagged
data, a continuation; response code, text, the values
ok / no / bad / untagged / cont server lines ("a1 OK done\r\n")
astring(s) / quoted(s) an atom when it can be, else quoted, a literal for other bytes
SeqSet "1:3,5,*": parse, contains(n, max), to_text
FetchItem, Section parse_fetch(item): what FETCH asks (ALL, FAST, FULL too)
SearchKey parse_search(args), matches through a closure of facts
envelope(&Message) the ENVELOPE of RFC 9051 7.5.2 as text
internal_date(unix) "01-Jan-2026 00:00:00 +0000"
header_of(data) / text_of(data) / header_fields(data, names, not) the parts FETCH names
SEEN, ANSWERED, FLAGGED, DELETED, DRAFT, RECENT the flags
A line ends with CRLF. A literal {n} (or {n+}) at its end means n bytes follow the CRLF and the
line goes on after them; Line keeps those bytes in `lits` and marks their place in `text` with a
NUL, which IMAP text never has. Words are not checked here beyond their shape: what a verb takes
is the server's business. Strings a server sends in the body of its messages are literals.
PORT_IMAP: u16 = 143
SEEN: &str = "\\Seen"
ANSWERED: &str = "\\Answered"
FLAGGED: &str = "\\Flagged"
DELETED: &str = "\\Deleted"
DRAFT: &str = "\\Draft"
RECENT: &str = "\\Recent"
fn verb_known(v: &str) -> boolthe commands of RFC 9051 and 3501 and of the extensions a dissector meetsenum Itemone value of a lineAtom(String)Quoted(String)Literal(Vec<u8>)List(Vec<Item>)Nil
impl Item
fn text(&self) -> Option<String>the value as text (an atom, a string, a literal; a list or NIL has none)fn atom(s: &str) -> Itemstruct Linea whole logical line: its bytes (CRLF gone, NUL where a literal stands) and its literalstext: Vec<u8>lits: Vec<Vec<u8>>
impl Line
fn plain(s: &str) -> Linea line without literals (tests, tools)enum Readwhat a reader foundLine(Line)NeedLiteral(usize)-- a literal of n bytes without "+" in its braces was announced: a server says "+ ..." now
struct LineReadersplits bytes into logical lines
impl LineReader
fn new() -> LineReaderfn push(&mut self, b: &[u8])fn pending(&self) -> usizebytes it holds that make up no line yetfn next(&mut self) -> Option<Read>the next thing that is completefn tokens_from(l: &Line, from: usize) -> Vec<Item>the values of a line, fromfromonfn tokens(l: &Line) -> Vec<Item>the values of a whole linestruct Commanda command: its tag, the verb in capitals, its valuestag: Stringverb: Stringargs: Vec<Item>
impl Command
fn parse(l: &Line) -> Option<Command>a command line; None without a tag and a verbfn arg(&self, k: usize) -> Option<String>the text of argument kfn line(tag: &str, verb: &str, args: &str) -> Vec<u8>a simple command line of a client; CR, LF and NUL of tag, verb and arguments are left out (they would end the line and start another command), but for the line end after a literal's "{n}" ("{n+}") in the arguments and the n bytes of the literal after it (astring)enum Statusthe status of a responseOkNoBadByePreAuth
impl Status
fn word(self) -> &'static strenum Kindwhat sort of line a response isTagged(String)-- "a1 OK ...": the end of a commandUntagged-- "* ..."Cont-- "+ ...": the server waits for more
struct Responsea response linekind: Kindstatus: Option<Status>code: Option<String>-- the response code of a status line, without its brackets ("UIDNEXT 7")text: String-- the text of a status line or a continuationitems: Vec<Item>-- the values of untagged data ("1", "FETCH", (...))
impl Response
fn parse(l: &Line) -> Option<Response>fn name(&self) -> Option<&str>the data's name: the first atom after an optional number ("FETCH", "LIST", "EXISTS")fn number(&self) -> Option<u32>the number before the namefn ok(tag: &str, code: Option<&str>, text: &str) -> Vec<u8>a status line OK / NO / BAD of a commandfn no(tag: &str, code: Option<&str>, text: &str) -> Vec<u8>fn bad(tag: &str, text: &str) -> Vec<u8>fn untagged(text: &str) -> Vec<u8>"* text"fn cont(text: &str) -> Vec<u8>"+ text"fn quoted(s: &str) -> Stringa string in quotes (without CR, LF and NUL, which a quoted string cannot hold)fn astring(s: &str) -> Stringan atom when it can be one, else a quoted string, else a literal (for other bytes)struct SeqSeta set of message numbers or UIDs: "1:3,5,*"
impl SeqSet
fn parse(s: &str) -> Option<SeqSet>fn contains(&self, n: u32, max: u32) -> booln is in the set;maxis what "*" stands forfn to_text(&self) -> Stringenum Sectionthe part of a message BODY[...] namesFullHeaderFields(Vec<String>, bool)-- HEADER.FIELDS (or .NOT,not)Text
enum FetchItemwhat FETCH asks forFlagsUidRfc822SizeInternalDateEnvelopeBody { peek: bool, section: Section, name: String }-- BODY[...] or BODY.PEEK[...] (peek: \Seen stays as it is); the text shows it in the answerRfc822Rfc822HeaderRfc822Text
fn parse_fetch(arg: &Item) -> Option<Vec<FetchItem>>the items of FETCH's argument: one atom or a list; None for one this does not knowenum SearchKeya SEARCH keyAllSeenUnseenAnsweredUnansweredFlaggedUnflaggedDeletedUndeletedRecentFrom(String)To(String)Subject(String)Body(String)Text(String)Larger(u64)Smaller(u64)Uid(SeqSet)Seq(SeqSet)Not(alloc::boxed::Box<SearchKey>)Or(alloc::boxed::Box<SearchKey>, alloc::boxed::Box<SearchKey>)And(Vec<SearchKey>)
fn parse_search(args: &[Item]) -> Option<Vec<SearchKey>>the keys of SEARCH (all must hold); None for what this does not knowfn envelope(m: &Message) -> Stringthe ENVELOPE of a message as its text (RFC 9051 7.5.2): date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to, message-idfn internal_date(unix: i64) -> String"01-Jan-2026 00:00:00 +0000" of seconds since 1970 (UTC)fn header_of(data: &[u8]) -> &[u8]the header of a message with its empty linefn text_of(data: &[u8]) -> &[u8]the text after the headerfn header_fields(data: &[u8], names: &[String], not: bool) -> Vec<u8>the fields of the header with these names (not: all others), each with its continuation lines, and the empty line