Skip to content

Commit

Permalink
Hold all programs with completions in an array
Browse files Browse the repository at this point in the history
This removes the duplication between the `variants` and `from_str`
implementations.
  • Loading branch information
yodaldevoid committed Mar 20, 2019
1 parent 4a4b260 commit 02d63fc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,20 +1117,28 @@ 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::<Vec<_>>()
}
}

impl FromStr for CompletionCommand {
type Err = String;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
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]")),
}
}
}
Expand Down

0 comments on commit 02d63fc

Please sign in to comment.