diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-07 12:41:06 +0000 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2020-01-07 12:41:06 +0000 |
commit | 85eb8cb7bc94570550d88526585d55973610b9bd (patch) | |
tree | 23b91aae91fe2441b51708f6697b9747ba12c37f | |
parent | d7f1557f103dfa9c6eea644517e42161cbd6b11f (diff) | |
download | nitrocli-85eb8cb7bc94570550d88526585d55973610b9bd.tar.gz nitrocli-85eb8cb7bc94570550d88526585d55973610b9bd.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-- | src/arg_util.rs | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/arg_util.rs b/src/arg_util.rs index 6d1bc50..317d9e3 100644 --- a/src/arg_util.rs +++ b/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 { |