From 02d63fcf27c18b58c89e43ae81176833857a6a43 Mon Sep 17 00:00:00 2001 From: Gabriel Smith Date: Sat, 16 Mar 2019 16:03:40 -0400 Subject: [PATCH] Hold all programs with completions in an array This removes the duplication between the `variants` and `from_str` implementations. --- src/cli/rustup_mode.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 5d5f158dc9..552f5035e3 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1117,9 +1117,14 @@ enum CompletionCommand { Cargo, } +static COMPLETIONS: &[(&'static str, CompletionCommand)] = &[ + ("rustup", CompletionCommand::Rustup), + ("cargo", CompletionCommand::Cargo), +]; + impl CompletionCommand { - fn variants() -> [&'static str; 2] { - ["rustup", "cargo"] + fn variants() -> Vec<&'static str> { + COMPLETIONS.iter().map(|&(s, _)| s).collect::>() } } @@ -1127,10 +1132,13 @@ impl FromStr for CompletionCommand { type Err = String; fn from_str(s: &str) -> std::result::Result { - match s { - _ if s.eq_ignore_ascii_case("rustup") => Ok(CompletionCommand::Rustup), - _ if s.eq_ignore_ascii_case("cargo") => Ok(CompletionCommand::Cargo), - _ => Err(String::from("[valid values: rustup, cargo]")), + match COMPLETIONS + .iter() + .filter(|&(val, _)| val.eq_ignore_ascii_case(s)) + .next() + { + Some(&(_, cmd)) => Ok(cmd), + None => Err(String::from("[valid values: rustup, cargo]")), } } }