Skip to content

derive macro that gives you a name of a current tag inside a variant as a string

License

Notifications You must be signed in to change notification settings

khrynczenko/tagname

Folders and files

NameName
Last commit message
Last commit date

Latest commit

912e04f · Jan 10, 2023

History

30 Commits
Oct 17, 2022
Oct 20, 2022
Dec 17, 2022
Nov 17, 2022
Oct 17, 2022
Dec 17, 2022
Dec 17, 2022
Oct 20, 2022
Jan 10, 2023
Nov 8, 2022

Repository files navigation

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");
}