5 个不稳定版本

0.3.2 2023年12月29日
0.3.1 2023年12月28日
0.3.0 2023年6月6日
0.2.0 2022年10月28日
0.1.0 2022年9月9日

#102电子邮件 中排名

Download history 24/week @ 2024-04-22 493/week @ 2024-04-29 788/week @ 2024-05-06 783/week @ 2024-05-13 73/week @ 2024-05-20 30/week @ 2024-05-27 38/week @ 2024-06-03 396/week @ 2024-06-10 580/week @ 2024-06-17 48/week @ 2024-06-24 103/week @ 2024-07-01 61/week @ 2024-07-08 32/week @ 2024-07-15 15/week @ 2024-07-22 192/week @ 2024-07-29 444/week @ 2024-08-05

每月下载量:692

Apache-2.0 OR MIT

400KB
11K SLoC

jmap-client

crates.io build docs.rs crates.io

jmap-client 是一个用 Rust 编写的 JSON 元应用协议 (JMAP) 库。该库是完全实现 JMAP RFCs 的,包括

功能

  • 异步和阻塞支持(使用 cargo feature blocking 启用阻塞)。
  • WebSocket 异步流(使用 cargo feature websockets 启用 JMAP over WebSocket)。
  • EventSource 异步流。
  • 辅助函数以减少样板代码并快速构建 JMAP 请求。
  • JMAP 请求的快速解析和编码。

使用示例

// Connect to the JMAP server using Basic authentication.
// (just for demonstration purposes, Bearer tokens should be used instead)
let client = Client::new()
    .credentials(("[email protected]", "secret"))
    .connect("https://jmap.example.org")
    .await
    .unwrap();

// Create a mailbox.
let mailbox_id = client
    .mailbox_create("My Mailbox", None::<String>, Role::None)
    .await
    .unwrap()
    .take_id();

// Import a message into the mailbox.
client
    .email_import(
        b"From: [email protected]\nSubject: test\n\n test".to_vec(),
        [&mailbox_id],
        ["$draft"].into(),
        None,
    )
    .await
    .unwrap();

// Obtain all e-mail ids matching a filter.
let email_id = client
    .email_query(
        Filter::and([
            email::query::Filter::subject("test"),
            email::query::Filter::in_mailbox(&mailbox_id),
            email::query::Filter::has_keyword("$draft"),
        ])
        .into(),
        [email::query::Comparator::from()].into(),
    )
    .await
    .unwrap()
    .take_ids()
    .pop()
    .unwrap();

// Fetch an e-mail message.
let email = client
    .email_get(
        &email_id,
        [Property::Subject, Property::Preview, Property::Keywords].into(),
    )
    .await
    .unwrap()
    .unwrap();
assert_eq!(email.preview().unwrap(), "test");
assert_eq!(email.subject().unwrap(), "test");
assert_eq!(email.keywords(), ["$draft"]);

// Fetch only the updated properties of all mailboxes that changed
// since a state.
let mut request = client.build();
let changes_request = request.changes_mailbox("n").max_changes(0);
let properties_ref = changes_request.updated_properties_reference();
let updated_ref = changes_request.updated_reference();
request
    .get_mailbox()
    .ids_ref(updated_ref)
    .properties_ref(properties_ref);
for mailbox in request
    .send()
    .await
    .unwrap()
    .unwrap_method_responses()
    .pop()
    .unwrap()
    .unwrap_get_mailbox()
    .unwrap()
    .take_list()
{
    println!("Changed mailbox: {:#?}", mailbox);
}

// Delete the mailbox including any messages
client.mailbox_destroy(&mailbox_id, true).await.unwrap();

// Open an EventSource connection with the JMAP server.
let mut stream = client
    .event_source(
        [
            TypeState::Email,
            TypeState::EmailDelivery,
            TypeState::Mailbox,
            TypeState::EmailSubmission,
            TypeState::Identity,
        ]
        .into(),
        false,
        60.into(),
        None,
    )
    .await
    .unwrap();

// Consume events received over EventSource.
while let Some(event) = stream.next().await {
    let changes = event.unwrap();
    println!("-> Change id: {:?}", changes.id());
    for account_id in changes.changed_accounts() {
        println!(" Account {} has changes:", account_id);
        if let Some(account_changes) = changes.changes(account_id) {
            for (type_state, state_id) in account_changes {
                println!("   Type {:?} has a new state {}.", type_state, state_id);
            }
        }
    }
}

更多示例可在 examples 目录下找到。

测试

要运行测试套件

 $ cargo test --all-features

符合 RFC

许可证

在以下任一许可证下发布

根据您的选择。

版权所有 (C) 2022, Stalwart Labs Ltd.

依赖项

~6–20MB
~311K SLoC