diff options
author | Daniel Mueller <deso@posteo.net> | 2019-01-14 16:27:21 -0800 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-14 16:27:21 -0800 |
commit | 8e0badc9988775b1b0340db5b2cb975c3ecf407d (patch) | |
tree | cc8cbbf41d09ffd2d76f5dc5c0e84727e765120c /nitrocli/src/arg_util.rs | |
parent | 4cf1b0c003a8bd0214e548a77bf0336148235931 (diff) | |
download | nitrocli-8e0badc9988775b1b0340db5b2cb975c3ecf407d.tar.gz nitrocli-8e0badc9988775b1b0340db5b2cb975c3ecf407d.tar.bz2 |
Auto-generate execute methods for generated command enums
Not too long ago we added a macro to auto generate the command enums and
the required trait implementations from a concise declarative
representation. This change extends this mechanism to the execute method
implementation that some of those enums provide.
When a tuple is specified as the "destination", e.g., here:
> Enum! {ConfigCommand, [
> Get => ("get", config_get),
> Set => ("set", config_set)
> ]}
the second component of this tuple will be interpreted as the function
to invoke when this variant used in the newly generated execute method.
Diffstat (limited to 'nitrocli/src/arg_util.rs')
-rw-r--r-- | nitrocli/src/arg_util.rs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs index 6dd14ed..b188085 100644 --- a/nitrocli/src/arg_util.rs +++ b/nitrocli/src/arg_util.rs @@ -22,9 +22,28 @@ // 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, $exec:expr) ), *] ) => { + Enum! {$name, [ + $( $var => $str ),* + ]} + + #[allow(unused_qualifications)] + impl $name { + fn execute( + self, + ctx: &mut crate::args::ExecCtx<'_>, + args: ::std::vec::Vec<::std::string::String>, + ) -> crate::Result<()> { + match self { + $( + $name::$var => $exec(ctx, args), + )* + } + } + } + }; ( $name:ident, [ $( $var:ident => $str:expr ), *] ) => { #[derive(Clone, Copy, Debug, PartialEq)] - #[allow(unused)] pub enum $name { $( $var, @@ -59,7 +78,7 @@ macro_rules! Enum { } } } - } + }; } #[cfg(test)] |