summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-07 23:44:17 +0000
committerDaniel Mueller <deso@posteo.net>2020-01-07 23:44:17 +0000
commit5e3ece42ee1ac765433fdcc05b8eb47a548051f1 (patch)
tree3e123498c38d084569ff5aa06b77557c36066dcd
parentfc58fa043c2de6e9566e921ec19fd626b3444f25 (diff)
downloadnitrocli-5e3ece42ee1ac765433fdcc05b8eb47a548051f1.tar.gz
nitrocli-5e3ece42ee1ac765433fdcc05b8eb47a548051f1.tar.bz2
Add all_str function to enums generated by Enum! macro
To make it easier to list all possible values for a command-line option mapped to an enum, we add the all_str function to the Enum! macro that returns an array of the string representations of all variants. We also use this new function to simplify the generation of the error message in the FromStr implementation in Enum!.
-rw-r--r--src/arg_util.rs15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/arg_util.rs b/src/arg_util.rs
index 2f04ccd..8cd8fe5 100644
--- a/src/arg_util.rs
+++ b/src/arg_util.rs
@@ -83,6 +83,15 @@ macro_rules! enum_int {
)*
]
}
+
+ #[allow(unused)]
+ pub fn all_str() -> [&'static str; count!($($var),*)] {
+ [
+ $(
+ $str,
+ )*
+ ]
+ }
}
impl ::std::convert::AsRef<str> for $name {
@@ -112,11 +121,7 @@ macro_rules! enum_int {
_ => Err(
format!(
"expected one of {}",
- $name::all_variants()
- .iter()
- .map(::std::convert::AsRef::as_ref)
- .collect::<::std::vec::Vec<_>>()
- .join(", "),
+ $name::all_str().join(", "),
)
)
}