#imap #data-structures #type

imap-types

Misuse-resistant data structures for IMAP

12 releases (1 stable)

2.0.0-alpha.4 Sep 8, 2024
2.0.0-alpha.3 Jul 18, 2024
2.0.0-alpha.1 Jun 7, 2024
1.0.0 Aug 22, 2023
0.6.0 Jun 14, 2022

#20 in Email

Download history 157/week @ 2024-07-28 117/week @ 2024-08-04 85/week @ 2024-08-11 188/week @ 2024-08-18 89/week @ 2024-08-25 86/week @ 2024-09-01 290/week @ 2024-09-08 233/week @ 2024-09-15 145/week @ 2024-09-22 109/week @ 2024-09-29 149/week @ 2024-10-06 121/week @ 2024-10-13 78/week @ 2024-10-20 79/week @ 2024-10-27 93/week @ 2024-11-03 129/week @ 2024-11-10

383 downloads per month
Used in 6 crates (via imap-codec)

MIT/Apache

390KB
6.5K SLoC

imap-types

%%{init: {'theme': 'neutral' } }%%
flowchart LR
    imap-types -.-> imap-codec
    imap-codec -.-> imap-next
    imap-next -.-> imap-proxy
    imap-next -.-> imap-client
    
    style imap-types stroke-width:4px
    
    click imap-types href "https://github.com/duesee/imap-codec/tree/main/imap-types"
    click imap-codec href "https://github.com/duesee/imap-codec"
    click imap-next href "https://github.com/duesee/imap-next"
    click imap-proxy href "https://github.com/duesee/imap-proxy"
    click imap-client href "https://github.com/soywod/imap-client"

This crate provides a complete set of well-designed, misuse-resistant types for the IMAP4rev1 protocol and various extensions. Notably, it does not provide parsers, nor serializers, but tries to become the "standard library" for IMAP in Rust that is useful for a broad range of crates.

If you are looking for parsers, and serializers, head over to imap-codec.

Features

  • Rust's type system is used to enforce correctness and to make the library misuse-resistant. It's not possible to construct a message that violates the IMAP specification.
  • Fuzzing (via cargo fuzz) and property-based tests are used to uncover bugs. The library is fuzz-tested never to produce an invalid message.

Working with imap-types

To ensure correctness, imap-types makes use of types such as AString, Atom, IString, Quoted, and Literal. When constructing messages, imap-types can automatically choose the best representation. However, it's always possible to manually choose a specific representation.

Examples

Automatic Construction

This ...

use imap_types::command::{Command, CommandBody};

let cmd = Command::new(
    "A1",
    CommandBody::login("alice", "password").unwrap()
).unwrap();

... will produce ...

A1 LOGIN alice password

However, ...

use imap_types::command::{Command, CommandBody};

let cmd = Command::new(
    "A1",
    CommandBody::login("alice\"", b"\xCA\xFE".as_ref()).unwrap(),
).unwrap();

... will produce ...

A1 LOGIN "alice\"" {2}
\xCA\xFE

Also, the construction ...

use imap_types::command::{Command, CommandBody};

let cmd = Command::new(
    "A1",
    CommandBody::login("alice\x00", "password").unwrap(),
).unwrap();

... will fail because IMAP doesn't allow NULL bytes in the username (nor password).

Manual Construction

You can also use ...

use imap_types::{
    command::{Command, CommandBody},
    core::Literal,
};

let cmd = Command::new(
    "A1",
    CommandBody::login(Literal::try_from("alice").unwrap(), "password").unwrap(),
).unwrap();

... to produce ...

A1 LOGIN {5}
alice password

... even though "alice" could be encoded more simply with an atom or quoted string.

Also, you can use Rust literals and resort to unvalidated constructors when you are certain that your input is correct:

use imap_types::{
    command::{Command, CommandBody},
    core::{AString, Atom, Tag},
    secret::Secret,
};

// This could be provided by the email application.
struct TagGenerator;

impl TagGenerator {
    fn random() -> Tag<'static> {
        // Make this random :-)
        Tag::unvalidated("A1")
    }
}

let tag = TagGenerator::random();

let cmd = Command {
    tag,
    body: CommandBody::Login {
        username: AString::from(Atom::unvalidated("alice")),
        password: Secret::new(AString::from(Atom::unvalidated("password"))),
    },
};

In this case, imap-codec won't stand in your way. However, it won't guarantee that you produce correct messages, either.

License

This crate is dual-licensed under Apache 2.0 and MIT terms.

Dependencies

~1.5–2.6MB
~47K SLoC