aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/arg_util.rs
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-06 16:05:36 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-06 16:05:36 -0800
commit350b3039ea0bc88c830bb3b4003af02b1000fd48 (patch)
treee99e9dc64bedc7a33fc1f9e9713fa0c673aa8fb3 /nitrocli/src/arg_util.rs
parent21a68c0cdd6237313a4cfad2de108b3785b75b49 (diff)
downloadnitrocli-350b3039ea0bc88c830bb3b4003af02b1000fd48.tar.gz
nitrocli-350b3039ea0bc88c830bb3b4003af02b1000fd48.tar.bz2
Auto generate argument enums
The argparse module we use for parsing events expects an enum in order to convey what subcommand has been supplied as an argument. Such an enum also needs to implement str::FromStr and, preferably, fmt::Display. Manually writing down those definitions is error-prone, tedious, and adds no value -- only lines of code. As it turns out the proper definitions can be generated with relative easy with a declarative macro, making the code much more concise. Hence, with this change we use a newly introduced macro for generating those enums.
Diffstat (limited to 'nitrocli/src/arg_util.rs')
-rw-r--r--nitrocli/src/arg_util.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs
new file mode 100644
index 0000000..6dd14ed
--- /dev/null
+++ b/nitrocli/src/arg_util.rs
@@ -0,0 +1,79 @@
+// arg_util.rs
+
+// *************************************************************************
+// * Copyright (C) 2019 Daniel Mueller (deso@posteo.net) *
+// * *
+// * This program is free software: you can redistribute it and/or modify *
+// * it under the terms of the GNU General Public License as published by *
+// * the Free Software Foundation, either version 3 of the License, or *
+// * (at your option) any later version. *
+// * *
+// * This program is distributed in the hope that it will be useful, *
+// * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+// * GNU General Public License for more details. *
+// * *
+// * You should have received a copy of the GNU General Public License *
+// * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+// *************************************************************************
+
+/// A macro for generating an enum with a set of simple (i.e., no
+/// parameters) variants and their textual representations.
+// TODO: Right now we hard code the derives we create. We may want to
+// make this set configurable.
+macro_rules! Enum {
+ ( $name:ident, [ $( $var:ident => $str:expr ), *] ) => {
+ #[derive(Clone, Copy, Debug, PartialEq)]
+ #[allow(unused)]
+ pub enum $name {
+ $(
+ $var,
+ )*
+ }
+
+ impl 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 = ();
+
+ fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
+ match s {
+ $(
+ $str => Ok($name::$var),
+ )*
+ _ => Err(()),
+ }
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ Enum! {Command, [
+ Var1 => "var1",
+ Var2 => "2",
+ Var3 => "crazy"
+ ]}
+
+ #[test]
+ fn text_representations() {
+ assert_eq!(Command::Var1.as_ref(), "var1");
+ assert_eq!(Command::Var2.as_ref(), "2");
+ assert_eq!(Command::Var3.as_ref(), "crazy");
+ }
+}