matched_enums_macro 1.3.0

Contains the macro for matchable enums.
Documentation
extern crate alloc;

use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};

use quote::quote;
use rstest::rstest;
use syn::ItemEnum;

use super::Variant;

#[rstest::fixture]
fn variants() -> BTreeMap<String, syn::Variant> {
    syn::parse2::<ItemEnum>(quote! {
        #[derive(Matched)]
        #[matched_enum(value_types=[i8, u16])]
        enum Foo {
            NoAttributes,

            #[matches = 0..1]
            NonListAttributeType,

            #[matches(true, i_hope_this_keyword_does_not_exist)]
            InvalidAttributeValue,

            #[matches(true)]
            #[matches(true)]
            MultipleDefaultMatchers,

            #[matches(true)]
            #[matches(true, bind=u16)]
            OneDefaultMatcherOneBoundMatcher,

            #[matches(true, bind=u16)]
            #[matches(true, bind=u16)]
            MultipleBoundMatchersOnOneType,

            #[matches(true, bind=isize)]
            MatcherBoundToUnsupportedType,
        }
    })
    .unwrap()
    .variants
    .iter()
    .map(|variant| (variant.ident.to_string(), variant.clone()))
    .collect()
}

#[rstest]
#[case("NoAttributes", true)]
#[case("NonListAttributeType", false)]
#[case("InvalidAttributeValue", false)]
#[case("MultipleDefaultMatchers", false)]
#[case("OneDefaultMatcherOneBoundMatcher", true)]
#[case("MultipleBoundMatchersOnOneType", false)]
#[case("MatcherBoundToUnsupportedType", true)]
fn test_attribute(
    variants: BTreeMap<String, syn::Variant>,
    #[case] name: &'static str,
    #[case] should_pass: bool,
) {
    let variant = variants.get(name).unwrap().clone();
    assert!(Variant::try_from(variant).is_ok() == should_pass);
}