impl_more

Macro impl_display_enum

source
macro_rules! impl_display_enum {
    ($ty:ty, $($variant:ident => $stringified:literal),+) => { ... };
    ($ty:ty, $($variant:ident => $stringified:literal),+ ,) => { ... };
    ($ty:ty, $($variant:ident ($($inner:ident),+) => $format:literal),+) => { ... };
    ($ty:ty, $($variant:ident ($($inner:ident),+) => $format:literal),+ ,) => { ... };
    ($ty:ty, $($variant:ident { $($inner:ident),+ } => $format:literal),+) => { ... };
    ($ty:ty, $($variant:ident { $($inner:ident),+ } => $format:literal),+ ,) => { ... };
}
Expand description

Implements Display for enums using a static string or format args for each variant.

ยงExamples

use impl_more::impl_display_enum;

enum Foo {
    Bar,
    Qux,
}

impl_display_enum!(Foo, Bar => "bar", Qux => "qux");

assert_eq!(Foo::Bar.to_string(), "bar");
assert_eq!(Foo::Qux.to_string(), "qux");

enum CoordOrMsg {
    Coord(i64, i64),
    Msg(&'static str),
}

impl_display_enum!(CoordOrMsg, Coord(x, y) => "{x}, {y}", Msg(msg) => "message: {msg}");

assert_eq!(CoordOrMsg::Coord(4, 2).to_string(), "4, 2");
assert_eq!(CoordOrMsg::Msg("hi").to_string(), "message: hi");