aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-07 12:42:28 +0000
committerDaniel Mueller <deso@posteo.net>2020-01-07 12:42:28 +0000
commit971b6c22a7f3d70840ca373bc54fcdae1f86262a (patch)
treead54bbf2cde9528be95127f263cb4458ea2a3c6c /src
parent85eb8cb7bc94570550d88526585d55973610b9bd (diff)
downloadnitrocli-971b6c22a7f3d70840ca373bc54fcdae1f86262a.tar.gz
nitrocli-971b6c22a7f3d70840ca373bc54fcdae1f86262a.tar.bz2
Allow adding fields to an enum variant in Command!
To be able to use the enums generated by Command! with structopt, we have to be able to add fields to them. This patch adds a new variant to the Command! macro that supports fields.
Diffstat (limited to 'src')
-rw-r--r--src/arg_util.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/arg_util.rs b/src/arg_util.rs
index 317d9e3..06a6ee6 100644
--- a/src/arg_util.rs
+++ b/src/arg_util.rs
@@ -25,6 +25,58 @@ macro_rules! count {
}
macro_rules! Command {
+ ( $name:ident, [ $( $var:ident($inner:ident) => ($str:expr, $exec:expr), ) *] ) => {
+ #[derive(Debug, PartialEq, structopt::StructOpt)]
+ pub enum $name {
+ $(
+ $var($inner),
+ )*
+ }
+
+ impl ::std::convert::AsRef<str> for $name {
+ fn as_ref(&self) -> &'static str {
+ match *self {
+ $(
+ $name::$var(_) => $str,
+ )*
+ }
+ }
+ }
+
+ impl ::std::fmt::Display for $name {
+ fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
+ write!(f, "{}", self.as_ref())
+ }
+ }
+
+ impl ::std::str::FromStr for $name {
+ type Err = &'static str;
+
+ fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
+ match s {
+ $(
+ $str => Ok($name::$var(::std::default::Default::default())),
+ )*
+ _ => Err("[error]"),
+ }
+ }
+ }
+
+ #[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, $exec:expr), ) *] ) => {
#[derive(Debug, PartialEq)]
pub enum $name {