#enums #accessor #proc-macro #macro #generate

macro enum_access

Custom derive for automatically generating the accessor methods for Enums

10 releases

Uses old Rust 2015

0.4.0 Jan 22, 2020
0.3.2 Aug 17, 2019
0.3.1 Dec 22, 2018
0.2.0 Nov 9, 2018
0.1.2 Aug 30, 2018

#910 in Procedural macros

Download history 4430/week @ 2024-07-19 3922/week @ 2024-07-26 3356/week @ 2024-08-02 3235/week @ 2024-08-09 3293/week @ 2024-08-16 3113/week @ 2024-08-23 3220/week @ 2024-08-30 3402/week @ 2024-09-06 2101/week @ 2024-09-13 2899/week @ 2024-09-20 2016/week @ 2024-09-27 1921/week @ 2024-10-04 2923/week @ 2024-10-11 2707/week @ 2024-10-18 1921/week @ 2024-10-25 3189/week @ 2024-11-01

11,240 downloads per month

MIT/Apache

26KB
527 lines

Build Status

EnumAccess

Custom derive for automatically generating the accessor methods for Enums.

#[macro_use]
extern crate enum_access;

#[derive(Clone, PartialEq, Debug, EnumAccess, EnumDisplay)]
#[enum_access(get(name), get_some(index, value), iter(inputs))]
enum Enum<T> {
    #[enum_display("Variant1 name:{}, input:{}", input, gen)]
    Variant1 {
        name: String,
        #[enum_alias(inputs)]
        input: i32,
        gen: T,
    },
    #[enum_inner_struct]
    Variant2 {
        index: u32,
        name: String,
        value: i32,
    },
    #[enum_inner_struct]
    Variant3 {
        name: String,
        #[enum_alias(inputs, value)]
        lhs: i32,
        #[enum_alias(inputs)]
        rhs: i32,
        #[enum_ignore]
        input: i32,
    },
    #[enum_display("Variant4 index:{}, name:{}", 0, 3)]
    Variant4(
        #[enum_alias(index)] u32,
        #[enum_alias(inputs)] i32,
        #[enum_alias(inputs)] i32,
        #[enum_alias(name)] String,
    ),
}

#[test]
fn it_works() {
    let mut v: Enum<u32> = Enum::Variant1 {
        name: "var1".to_string(),
        input: 9,
        gen: 0,
    };

    assert_eq!(v.name(), &"var1".to_string());
    assert_eq!(v.index(), None);
    assert_eq!(v.value(), None);
    assert_eq!(v.inputs(), vec![&9]);

    assert_eq!(v.to_string(), "Variant1 name:9, input:0");

    *v.name_mut() = "var1'".to_string();
    assert_eq!(v.name(), &"var1'".to_string());

    let mut v: Enum<u32> = Enum::Variant2 {
        index: 0,
        name: "var2".to_string(),
        value: 23,
    };

    let variant2: EnumVariant2Inner = v.clone().into();
    assert_eq!(v, Enum::from(variant2));

    assert_eq!(v.name(), &"var2".to_string());
    assert_eq!(v.index(), Some(&0));
    assert_eq!(v.value(), Some(&23));
    assert_eq!(v.inputs(), Vec::<&i32>::new());

    assert_eq!(v.to_string(), "");

    *v.index_mut().unwrap() = 100;
    assert_eq!(v.index(), Some(&100));

    let mut v: Enum<u32> = Enum::Variant3 {
        name: "var3".to_string(),
        lhs: 1,
        rhs: 2,
        input: 3,
    };

    let variant3: EnumVariant3Inner = v.clone().into();
    assert_eq!(v, Enum::from(variant3));

    assert_eq!(v.name(), &"var3".to_string());
    assert_eq!(v.index(), None);
    assert_eq!(v.value(), Some(&1));
    assert_eq!(v.inputs(), vec![&1, &2]);

    for n in v.inputs_mut() {
        *n += 10;
    }
    assert_eq!(v.inputs(), vec![&11, &12]);

    let v: Enum<u32> = Enum::Variant4(10u32, 11i32, 12i32, "var4".to_string());
    assert_eq!(v.name(), &"var4".to_string());
    assert_eq!(v.index(), Some(&10));
    assert_eq!(v.value(), None);
    assert_eq!(v.inputs(), vec![&11, &12]);

    assert_eq!(v.to_string(), "Variant4 index:10, name:var4");
}

Dependencies

~1.5MB
~39K SLoC