Macro relay_common::derive_fromstr_and_display

source ·
macro_rules! derive_fromstr_and_display {
    ($type:ty, $error_type:tt, { $($variant:path => $($name:literal)|*),+ $(,)? }) => { ... };
}
Expand description

Implements FromStr and Display on a flat/C-like enum such that strings roundtrip correctly and all variants can be FromStr’d.

Usage:

use relay_common::derive_fromstr_and_display;

// derive fail for this or whatever you need. The type must be ZST though.
struct ValueTypeError;

enum ValueType {
    Foo,
    Bar,
}

derive_fromstr_and_display!(ValueType, ValueTypeError, {
    ValueType::Foo => "foo" | "foo2",  // fromstr will recognize foo/foo2, display will use foo.
    ValueType::Bar => "bar",
});