enum-try-as-inner
A deriving proc-macro for generating functions to automatically give access to the inner members of enum.
This is a fork of enum-as-inner
, this crate focuses on returning Result<Variant, EnumError>
instead.
Basic unnamed field case
The basic case is meant for single item enums, like:
use EnumTryAsInner;
let one = One;
assert_eq!;
assert_eq!;
where the result is either a reference for inner items or a tuple containing the inner items.
Unit case
This will return true if enum's variant matches the expected type
use EnumTryAsInner;
let unit = Two;
assert!;
Multiple, unnamed field case
This will return a tuple of the inner types:
use EnumTryAsInner;
let many = Three;
assert!;
assert_eq!;
assert_eq!;
Multiple, named field case
This will return a tuple of the inner types, like the unnamed option:
use EnumTryAsInner;
let many = Three ;
assert!;
assert_eq!;
assert_eq!;
Error
This macro generates an error type for each enum which contains information about which variant was expected,
which variant was found at runtime, and if using the try_into_*
functions, the actual value.
Error derives
By default, the generated error does not implement any traits, including std::error::Error
.
Derive macros can be forwarded to the error implementation using the derive_err
attribute.
If the Debug
derive is provided, an implementation of Display
and Error
will be automatically provided.
If you would like to implement your own Display
format, you will need to also implement Debug
and Error
yourself.
use EnumTryAsInner;
let one = One;
let err = one.try_into_two.unwrap_err;
let cloned = err.clone;
assert_eq!;
println!;
println!;