Skip to content

Commit

Permalink
feat(macros): arg_enum! and simple_enum! auto-implement Display
Browse files Browse the repository at this point in the history
enums created with simple_enum! and arg_enum! now auto-implement
std::fmt::Display where the variant only is displayed.

Closes #120
  • Loading branch information
kbknapp committed May 15, 2015
1 parent 0c264a8 commit d1219f0
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)),)+
}
}
}
};
}

Expand All @@ -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
///
///
Expand Down Expand Up @@ -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 {
Expand All @@ -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,)+)]
Expand Down Expand Up @@ -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,)+)]
Expand Down Expand Up @@ -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)),)+
}
}
}
};
}

Expand Down

0 comments on commit d1219f0

Please sign in to comment.