From aa1f893dbe2f48c7a643c15db35802440664f60b Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 7 Jan 2020 13:07:15 +0000 Subject: Implement Default in the Enum! and Command! macros For the transition to structopt, we have to be able to easily construct enum variants once we have added fields to them. Therefore we implement the Default trait in the generated macros by choosing the first variant as the default. --- nitrocli/src/arg_util.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs index b98aa34..075dc6d 100644 --- a/nitrocli/src/arg_util.rs +++ b/nitrocli/src/arg_util.rs @@ -76,6 +76,8 @@ macro_rules! Command { } } } + + impl_default!($name => $( $name::$var(::std::default::Default::default()) , )*); }; ( $name:ident, [ $( $var:ident => ($str:expr, $exec:expr), ) *] ) => { #[derive(Debug, PartialEq)] @@ -128,6 +130,8 @@ macro_rules! Command { } } } + + impl_default!($name => $( $name::$var , )*); }; } @@ -204,6 +208,19 @@ macro_rules! enum_int { } } } + + impl_default!($name => $( $name::$var , )*); + + }; +} + +macro_rules! impl_default { + ( $name:ident => $def:expr , $( $other:expr , ) *) => { + impl ::std::default::Default for $name { + fn default() -> Self { + $def + } + } }; } -- cgit v1.2.3