matched_enums 1.3.0

A macro that provides the option to bin enum attribute to match-statements. This makes it easier to convert values into enums.
Documentation
//! This example shows how you can generate a structure allowing
//! you to override the matching conditions in runtime.

#[cfg(not(feature = "runtime_configurable"))]
panic!("The `runtime_configurable` feature must be enabled");

extern crate alloc;
use matched_enums::{match_when, Matched};

#[derive(Matched, PartialEq, Eq, Debug, Ord, PartialOrd)]
#[matched_enum(allow_runtime_configurable=true, value_type=isize)]
enum Foo {
    #[matches(..0)]
    Bar,

    #[matches(0..1)]
    Baz,

    #[matches(1..)]
    Fred,
}

fn main() {
    let mut matcher = FooMatcher::<isize>::default();

    // Using default behaviour
    assert_eq!(matcher.try_into_enum(&-75).unwrap(), Foo::Bar);
    assert_eq!(matcher.try_into_enum(&0).unwrap(), Foo::Baz);
    assert_eq!(matcher.try_into_enum(&10).unwrap(), Foo::Fred);

    // Modifying the predicates
    matcher.bar = match_when!(100..1000); // With helper macro
    matcher.baz = Box::new(|&value| (value % 2) == 0); // Or even, as a function
    assert_eq!(matcher.try_into_enum(&16).unwrap(), Foo::Baz);
    assert_eq!(matcher.try_into_enum(&600).unwrap(), Foo::Bar);
    assert_eq!(matcher.try_into_enum(&1001).unwrap(), Foo::Fred);

    // Oops, negatives are no longer covered, expect an error
    assert!(matcher.try_into_enum(&-75).is_err());

    matcher.bar = match_when!(_);
    // The matcher uses the top-to-bottom declaration order
    // Meaning [`Foo::Bar`] now shadows every other arm...
    // Be careful of the declaration order!
    assert_eq!(matcher.try_into_enum(&16).unwrap(), Foo::Bar);
    assert_eq!(matcher.try_into_enum(&600).unwrap(), Foo::Bar);
    assert_eq!(matcher.try_into_enum(&1001).unwrap(), Foo::Bar);

    // Finally, it should be possible to capture variables into the predicates
    const BAR_MAX: isize = 10;
    let bar_min = -10;

    matcher.bar = Box::new(move |&value| (bar_min..BAR_MAX).contains(&value));
    assert_eq!(matcher.try_into_enum(&5).unwrap(), Foo::Bar);
    assert_eq!(matcher.try_into_enum(&1001).unwrap(), Foo::Fred);
}