Skip to content

Commit

Permalink
Implement Default in the Enum! and Command! macros
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robinkrahl authored and d-e-s-o committed Jan 7, 2020
1 parent 971b6c2 commit ddd3482
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/arg_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -128,6 +130,8 @@ macro_rules! Command {
}
}
}

impl_default!($name => $( $name::$var , )*);
};
}

Expand Down Expand Up @@ -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
}
}
};
}

Expand Down

0 comments on commit ddd3482

Please sign in to comment.