aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-07 12:41:06 +0000
committerDaniel Mueller <deso@posteo.net>2020-01-08 09:20:43 -0800
commit2dcceb1d5889f6eac6262f38506c58066a898f76 (patch)
tree368c1a51668be1929bfe833912e97b2ad36e3e5a
parent929275b7abda0939eb49e99087af44444bad0a33 (diff)
downloadnitrocli-2dcceb1d5889f6eac6262f38506c58066a898f76.tar.gz
nitrocli-2dcceb1d5889f6eac6262f38506c58066a898f76.tar.bz2
Factor out Enum! and Command! macros in arg_util
For easier refactoring, we remove the internal enum_int! macro and instead copy its code to the Enum! and Command! macros.
-rw-r--r--nitrocli/src/arg_util.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs
index fe755f3..68bd23f 100644
--- a/nitrocli/src/arg_util.rs
+++ b/nitrocli/src/arg_util.rs
@@ -33,9 +33,34 @@ macro_rules! Command {
)*
}
- enum_int! {$name, [
- $( $var => $str, )*
- ]}
+ 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),
+ )*
+ _ => Err("[error]"),
+ }
+ }
+ }
#[allow(unused_qualifications)]
impl $name {