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
This commit is contained in:
Kevin K 2015-05-15 15:34:41 -04:00
parent 0c264a8ca5
commit d1219f0d13

View file

@ -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 /// Convenience macro generated a simple enum with variants to be used as a type when parsing
/// arguments. /// arguments.
/// ///
/// **NOTE:** This macro automaically implements std::str::FromStr and std::fmt::Display
///
/// # Example /// # Example
/// ///
/// ```no_run /// ```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:** 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 /// 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:ident { $($v:ident),+ } ) => {
pub enum $e { 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:ident),+)] enum $e:ident { $($v:ident),+ } ) => {
#[derive($($d,)+)] #[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:ident),+)] pub enum $e:ident { $($v:ident),+ } ) => {
#[derive($($d,)+)] #[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)),)+
}
}
}
}; };
} }