diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-07 11:51:11 +0000 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2020-01-08 09:20:30 -0800 |
commit | b1725639a21a8661c8e8dfe30811b650abf028b0 (patch) | |
tree | 8f35d0390648777b83dc0abdde7d17f1b62fae5e /nitrocli/src/arg_util.rs | |
parent | 5e20a29b4fdc8a2d442d1093681b396dcb4b816b (diff) | |
download | nitrocli-b1725639a21a8661c8e8dfe30811b650abf028b0.tar.gz nitrocli-b1725639a21a8661c8e8dfe30811b650abf028b0.tar.bz2 |
Refactor the Enum! macro into Enum! and Command!
For an easier transition to structopt, this patch splits the two cases
of the Enum! macro into two separate macros (that internally both call
the new enum_int! macro).
Diffstat (limited to 'nitrocli/src/arg_util.rs')
-rw-r--r-- | nitrocli/src/arg_util.rs | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs index e2e7b1d..3e4ba85 100644 --- a/nitrocli/src/arg_util.rs +++ b/nitrocli/src/arg_util.rs @@ -24,13 +24,16 @@ macro_rules! count { } } -/// A macro for generating an enum with a set of simple (i.e., no -/// parameters) variants and their textual representations. -// TODO: Right now we hard code the derives we create. We may want to -// make this set configurable. -macro_rules! Enum { +macro_rules! Command { ( $name:ident, [ $( $var:ident => ($str:expr, $exec:expr), ) *] ) => { - Enum! {$name, [ + #[derive(Debug, PartialEq)] + pub enum $name { + $( + $var, + )* + } + + enum_int! {$name, [ $( $var => $str, )* ]} @@ -49,6 +52,13 @@ macro_rules! Enum { } } }; +} + +/// A macro for generating an enum with a set of simple (i.e., no +/// parameters) variants and their textual representations. +// TODO: Right now we hard code the derives we create. We may want to +// make this set configurable. +macro_rules! Enum { ( $name:ident, [ $( $var:ident => $str:expr, ) *] ) => { #[derive(Clone, Copy, Debug, PartialEq)] pub enum $name { @@ -57,6 +67,14 @@ macro_rules! Enum { )* } + enum_int! {$name, [ + $( $var => $str, )* + ]} + }; +} + +macro_rules! enum_int { + ( $name:ident, [ $( $var:ident => $str:expr, ) *] ) => { impl $name { #[allow(unused)] pub fn all(&self) -> [$name; count!($($var),*) ] { |