From 5e62012ce3cb9c27627dec3192682a0657a12c93 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 23 Jul 2024 00:43:32 +1000 Subject: [PATCH] Add compile-time length checking for `Strategy` Signed-off-by: Jiahao XU --- crates/bin/src/args.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/bin/src/args.rs b/crates/bin/src/args.rs index 4f7cf65a..061fbdab 100644 --- a/crates/bin/src/args.rs +++ b/crates/bin/src/args.rs @@ -434,13 +434,17 @@ impl Default for RateLimit { #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] pub(crate) struct StrategyWrapped(pub(crate) Strategy); +impl StrategyWrapped { + const VARIANTS: &'static [Self; 3] = &[ + Self(Strategy::CrateMetaData), + Self(Strategy::QuickInstall), + Self(Strategy::Compile), + ]; +} + impl ValueEnum for StrategyWrapped { fn value_variants<'a>() -> &'a [Self] { - &[ - Self(Strategy::CrateMetaData), - Self(Strategy::QuickInstall), - Self(Strategy::Compile), - ] + Self::VARIANTS } fn to_possible_value(&self) -> Option { Some(PossibleValue::new(self.0.to_str())) @@ -598,10 +602,14 @@ You cannot use --{option} and specify multiple packages at the same time. Do one #[cfg(test)] mod test { + use strum::VariantArray; + use super::*; #[test] fn verify_cli() { Args::command().debug_assert() } + + const _: () = assert!(Strategy::VARIANTS.len() == StrategyWrapped::VARIANTS.len()); }