Skip to content

Commit

Permalink
feat(macros): arg_enum! and simple_enum! provide a Vec<&str> of varia…
Browse files Browse the repository at this point in the history
…nt names

You can now use  ArgName::variants() to get a Vec<&'static str> of
vairant names (Nice for Arg::possible_values()). It's not as good as
implementing Iterator for the enum, but that can't be done without being
able to concat AST tokens in order to make an "EnumIter" type which
implements iterator and keeps track of when to yield None. But the
overall intent of this issue was to be able to iterate variant names,
which is now possible.

Closes #119
  • Loading branch information
kbknapp committed May 15, 2015
1 parent d1219f0 commit 30fa87b
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ macro_rules! value_t_or_exit {
}

/// Convenience macro generated a simple enum with variants to be used as a type when parsing
/// arguments.
/// arguments. This enum also provides a `variants()` function which can be used to retrieve a
/// `Vec<&'static str>` of the variant names.
///
/// **NOTE:** This macro automaically implements std::str::FromStr and std::fmt::Display
///
Expand Down Expand Up @@ -332,11 +333,20 @@ macro_rules! simple_enum {
}
}
}

impl $e {
pub fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
}

/// Convenience macro to generate more complete enums with variants to be used as a type when
/// parsing arguments.
/// parsing arguments. This enum also provides a `variants()` function which can be used to retrieve a
/// `Vec<&'static str>` of the variant names.
///
/// **NOTE:** Case insensitivity is supported for ASCII characters
///
Expand Down Expand Up @@ -405,6 +415,14 @@ macro_rules! arg_enum {
}
}
}

impl $e {
fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
(pub enum $e:ident { $($v:ident),+ } ) => {
pub enum $e {
Expand Down Expand Up @@ -439,6 +457,14 @@ macro_rules! arg_enum {
}
}
}

impl $e {
pub fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
(#[derive($($d:ident),+)] enum $e:ident { $($v:ident),+ } ) => {
#[derive($($d,)+)]
Expand Down Expand Up @@ -474,6 +500,14 @@ macro_rules! arg_enum {
}
}
}

impl $e {
pub fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
(#[derive($($d:ident),+)] pub enum $e:ident { $($v:ident),+ } ) => {
#[derive($($d,)+)]
Expand Down Expand Up @@ -509,6 +543,14 @@ macro_rules! arg_enum {
}
}
}

impl $e {
pub fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
}

Expand Down

0 comments on commit 30fa87b

Please sign in to comment.