Renum
Derive the From or TryFrom traits for enums with integer values.
Designed to provide a consistant mechanism to work with C-style Enums in Rust such as when implementing a parser.
Usage
[]
= "0.1.0"
use Renum;
let x = try_from;
assert_eq!
Enum Options
normally_ok
default behavior
The Result returned by the derived try_from will be Ok(Enum::Variant) unless the variant is explicitly tagged #[renum(Err)]
The default case will return Err(Enum::Default) unless explicitly tagged #[renum(Ok)]
normally_err
Inverts the normally_ok behavior
The Result returned by the derived try_from will be Err(Enum::Variant) unless explicitly tagged #[renum(Ok)]
The default case will return Ok(Enum::Default) unless explicitly tagged #[renum(Err)]
example usage: status code enums
// ...
let status_code = try_from?;
allow_panic
If an enum does not have a default variant allow_panic will allow the derived method to panic on undefined values.
Example:
Error
Only Valid when deriving TryFrom
// custom
The error type can be controlled with the Error option; REPR, and ENUM are builtin values which return the value passed in or enum variant.
If a custom error is used it must implement From::<#ErrorFrom> (discussed below), by default this will be the repr type.
ErrorFrom
Only Valid when deriving TryFrom
The value used to initialize custom errors can be set to REPR, ENUM, or any sequence of ([REPR|ENUM],*)
Example:
;
if let Err = try_from
Variant Options
Ok
Only Valid when deriving TryFrom
Variant = 0
The variant will always return Ok(Enum::Variant) when matched even if it would otherwise be an error condition.
Err
Only Valid when deriving TryFrom
Variant = 0
The variant will always return Err(Enum::Variant) when matched even if it would otherwise be an error condition.
Default
Variant
If no values match this variant will be returned, if no variants are explicitly annotated with the default option, default is automatically selected to be the variant without a desciminant or values attribute.
Values
// this shouldn't be needed, use a discriminant
// REPR::MIN to 9
// REPR::MIN to 10
// Range 5 to 9
// RangeInclusive 5 to 10
// any combination of values for non-consecutive ranges
Specify values for a variant where it may take on multiple values.