From d1219f0d1371d872061bd0718057eca4ef47b739 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Fri, 15 May 2015 15:34:41 -0400 Subject: [PATCH] feat(macros): arg_enum! and simple_enum! auto-implement Display enums created with simple_enum! and arg_enum! now auto-implement std::fmt::Display where the variant only is displayed. Closes #120 --- src/macros.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/macros.rs b/src/macros.rs index 72d8e51a712..5091638527b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -277,6 +277,8 @@ macro_rules! value_t_or_exit { /// Convenience macro generated a simple enum with variants to be used as a type when parsing /// arguments. /// +/// **NOTE:** This macro automaically implements std::str::FromStr and std::fmt::Display +/// /// # Example /// /// ```no_run @@ -322,6 +324,14 @@ macro_rules! simple_enum { } } } + + impl ::std::fmt::Display for $e { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + match *self { + $($e::$v => write!(f, stringify!($v)),)+ + } + } + } }; } @@ -330,6 +340,8 @@ macro_rules! simple_enum { /// /// **NOTE:** Case insensitivity is supported for ASCII characters /// +/// **NOTE:** This macro automaically implements std::str::FromStr and std::fmt::Display +/// /// These enums support pub (or not) and use of the #[derive()] traits /// /// @@ -385,6 +397,14 @@ macro_rules! arg_enum { } } } + + impl ::std::fmt::Display for $e { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + match *self { + $($e::$v => write!(f, stringify!($v)),)+ + } + } + } }; (pub enum $e:ident { $($v:ident),+ } ) => { pub enum $e { @@ -411,6 +431,14 @@ macro_rules! arg_enum { } } } + + impl ::std::fmt::Display for $e { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + match *self { + $($e::$v => write!(f, stringify!($v)),)+ + } + } + } }; (#[derive($($d:ident),+)] enum $e:ident { $($v:ident),+ } ) => { #[derive($($d,)+)] @@ -438,6 +466,14 @@ macro_rules! arg_enum { } } } + + impl ::std::fmt::Display for $e { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + match *self { + $($e::$v => write!(f, stringify!($v)),)+ + } + } + } }; (#[derive($($d:ident),+)] pub enum $e:ident { $($v:ident),+ } ) => { #[derive($($d,)+)] @@ -465,6 +501,14 @@ macro_rules! arg_enum { } } } + + impl ::std::fmt::Display for $e { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + match *self { + $($e::$v => write!(f, stringify!($v)),)+ + } + } + } }; }