#header #component #name #map #subject #error #standard

mail-headers

[mail/headers] 邮件crate的头部分(包括头映射和标准头实现)

7个版本

使用旧Rust 2015

0.6.6 2019年3月26日
0.6.4 2019年2月14日
0.6.2 2018年12月4日
0.6.0 2018年11月23日

#1031 in 算法

Download history 23/week @ 2024-03-11 22/week @ 2024-03-18 15/week @ 2024-03-25 56/week @ 2024-04-01 15/week @ 2024-04-08 24/week @ 2024-04-15 29/week @ 2024-04-22 13/week @ 2024-04-29 29/week @ 2024-05-06 27/week @ 2024-05-13 33/week @ 2024-05-20 24/week @ 2024-05-27 24/week @ 2024-06-03 27/week @ 2024-06-10 23/week @ 2024-06-17 19/week @ 2024-06-24

95 每月下载量
6 crate 中使用

MIT/Apache

210KB
4.5K SLoC

mail-headers

mail crate 提供特定功能的头


此crate为 mail crate 提供特定功能的头。包括

  • HeaderNameHeaderHeaderMap 作为通用API
  • HeaderTryFromHeaderTryInto 作为 TryFrom/TryInto trait 不稳定,但我们需要类似的功能。
  • 一些头,如 _To_FromSenderSubject 等(请注意,_To_From_ 开头,以防止在导入时发生名称冲突,即导入 _From as From 会遮蔽 std::convert::From,这可能导致非常令人烦恼的错误)。
  • 一些用于表示头字段内容/字段体的组件,例如 MailboxListEmail。它们位于 components 模块中。
  • 一个 headers! 宏,用于简化创建带有多个头的 HeaderMap
  • 一个 def_headers! 宏,用于定义新的自定义头

示例(HeaderMap)

头映射是一系列表示特定顺序中的多个邮件头部的集合。它可以创建如下所示

#[macro_use]
extern crate mail_headers;

// just import all headers
use mail_headers::*;
use mail_headers::error::ComponentCreationError;

fn create_headers() -> Result<HeaderMap, ComponentCreationError> {
    headers!{
        // from and to can have multiple values
        // until specialization is stable is array
        // is necessary
        _From: [("My Fancy Display Name", "[email protected]")],
        _To: [ "[email protected]", ],
        Subject: "Who are you?"
    }
}

fn main() {
    let headers = create_headers().unwrap();
    assert_eq!(headers.len(), 3);

    if let Some(subject) = headers.get_single(Subject) {
        // as long a you don't play around with custom headers AND
        // don't mix multiple implementations for the same header
        // `.unwrap()` is just fine.
        let subject = subject.expect("mixed different Subject header implementations");
        println!("found subject: {}", subject);
    }
}

示例(自定义头)

如果需要,mail crate 的用户可以创建自己的头,但应该谨慎处理。

请注意,第二个字段(即 unchecked { <name> }),期望特定的命名方案,自动生成的测试会检查是否违反了该方案,但如果你只是运行代码并忽略失败的测试,可能会出现奇怪的错误。(该方案是每个单词的首字母大写,其余字母小写,即 X-Id 是可以的,但 X-ID 则不行)。之所以如此,是因为头部查找字段的方式。虽然这并不是很好,但对于大多数用例来说,没有必要生成自定义头部,而且在未来这可能会通过自动使用 proc-derive 生成名称来绕过。

#[macro_use]
extern crate mail_headers;

use mail_headers::components;

// this will define two headers `XFooEmail` and `XBarMailbox`
// the first will add a header field named `X-Foo-Email` with
// a value which is an `components::Email` and the second will
// add field with a value which is an `components::Mailbox`.
//
// Note that through the way they are defined `XFooEmail` can
// at most appear 1 time in an header map, while `XBarMailbox`
// can appear multiple times. Be aware that this is checked through
// so called validators which needs to be explicitly run, which they
// are if this header map is used to create a mail (running them
// when adding fields would be a mess as you would have to add
// transactions which can add/remove multiple fields at once, and
// implementing auto-generation for some fields which are required if
// some other fields are given in a certain way would be harder too).

// If in scope both can be used in the `headers!` macro,
// like any other header.
//
def_headers! {
    // the name of the auto-generated test
    test_name: validate_header_names,

    // the scope from which all components should be imported
    // E.g. `DateTime` refers to `components::DateTime`.
    scope: components,

    // definitions of the headers or the form
    // <type_name>, unchecked { <filed_name> }, <component>, <validator>
    XFooEmail, unchecked { "X-Foo-Email"      }, Email ,   maxOne,
    XBarMailbox, unchecked { "X-Bar-Mailbox" }, Mailbox, None
}

fn main() {
    let headers = headers! {
        XFooEmail: "[email protected]",
        XBarMailbox: ("My Funy Name", "[email protected]"),
        XBarMailbox: "[email protected]"
    }.unwrap();
}

文档

文档可以在 docs.rs 上查看。(一旦发布 ;) )

许可

许可为以下之一

由你选择。

贡献

除非你明确声明,否则任何有意提交给作品并由你包括在内的贡献,根据 Apache-2.0 许可证的定义,都应按上述方式双重许可,不得附加任何额外的条款或条件。

依赖

~5MB
~112K SLoC