[][src]Crate enumber

Numerical enumerations

The enumber crate provides a mechanism for deriving a lot of useful helpers for your enumerations which are sets of numbers. Its main purpose is to provide convenience implementations of a number of useful traits for your enumerations automatically.

See the convert macro for details, however here is a basic example:

#[enumber::convert]
#[repr(usize)]
enum Simple {
    Foo = 1,
    Bar = 2,
}

use std::convert::TryFrom;

// You can use try_from() to go from a suitable number to an instance of
// your enumeration.
assert!(matches!(Simple::try_from(1), Ok(Simple::Foo)));

// You can convert from instances of your enumeration to a number.
assert_eq!(2 as usize, Simple::Bar.into());

// You can render instances of your enumeration to strings.
assert_eq!(&format!("{}", Simple::Foo), "Foo");

// And you can convert from a string to your enumeration, using the names
// of the enumeration items (case insensitively) or by number.  If the
// name or number is invalid, you'll get an error.

use std::str::FromStr;
assert!(matches!(Simple::from_str("Foo"), Ok(Simple::Foo)));
assert!(matches!(Simple::from_str("bAr"), Ok(Simple::Bar)));
assert!(matches!(Simple::from_str("1"), Ok(Simple::Foo)));
assert!(matches!(Simple::from_str("0x02"), Ok(Simple::Bar)));
assert!(matches!(Simple::from_str("3"), Err(ParseSimpleError::UnknownValue(_))));
assert!(matches!(Simple::from_str("wibble"), Err(ParseSimpleError::UnknownName(_))));

Attribute Macros

convert

Convert an enumber compliant enum into a proper enum with appropriate associated traits.