4 releases (2 breaking)

Uses new Rust 2021

0.3.0 Nov 17, 2022
0.2.1 Nov 8, 2022
0.2.0 Oct 29, 2022
0.1.1 Oct 20, 2022
0.1.0 Oct 20, 2022

#873 in Rust patterns

Download history 35/week @ 2022-10-15 16/week @ 2022-10-22 24/week @ 2022-10-29 25/week @ 2022-11-05 61/week @ 2022-11-12 22/week @ 2022-11-19

132 downloads per month

MIT license

7KB

tagname

github crates.io docs.rs build status

This library exports a trait called TagName that exposes a tag_name method which is used for retrieving a name (tag) of a currently hold variant within an enum value.

More importantly, together with TagName trait comes a derive(TagName) macro that can automatically implement the trait.

use tagname::TagName;

#[derive(TagName)]
enum MyTaggedUnion {
    [tag(case = "lower")]
    Yes,
    [tag(case = "upper")]
    No,
    Maybe(usize),
}

#[test]
fn return_correct_tag_names() {
    let v1 = MyTaggedUnion::Yes;
    let v2 = MyTaggedUnion::No;
    let v3 = MyTaggedUnion::Maybe(1);
    assert_eq!(v1.tag_name(), "yes");
    assert_eq!(v2.tag_name(), "NO");
    assert_eq!(v3.tag_name(), "Maybe");
}

Dependencies

~210–610KB
~15K SLoC