From 447870f5e8d70c5be3bba2d7e866bf26420b1260 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 7 Jan 2020 23:44:17 +0000 Subject: 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!. --- nitrocli/src/arg_util.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs index 15ab730..cb75c52 100644 --- a/nitrocli/src/arg_util.rs +++ b/nitrocli/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 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(", "), ) ) } -- cgit v1.2.1