From 26beca06fccd1854952a9e9af740025196235170 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Fri, 24 Jul 2020 23:26:25 +0200 Subject: [PATCH 1/3] Print builtin aliases with cargo --list command As stated in #8486 it would help to the discovery of the builtin aliases the facto of printing them with the `cargo --list` command. - Extracted the builtin aliases currently implemented to a sepparated `const`. - Make all of the functions that interact with these aliases point to that function. - Refactored the `list_commands` fn in order to include with the builtin and external commands, the builtin aliases that come with cargo by defaut. --- src/bin/cargo/main.rs | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index f9299367668..6248ea68fd6 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -47,6 +47,25 @@ fn main() { } } +const BUILTIN_ALIASES: [(&str, &str); 4] = [ + ("b", "alias: build"), + ("c", "alias: check"), + ("r", "alias: run"), + ("t", "alias: test"), +]; + +/// Function which contains the list of all of the builtin aliases and it's +/// corresponding execs represented as &str. +fn builtin_aliases_execs(cmd: &str) -> Option<&str> { + match cmd { + "b" => Some("build"), + "c" => Some("check"), + "r" => Some("run"), + "t" => Some("test"), + _ => None, + } +} + fn aliased_command(config: &Config, command: &str) -> CargoResult>> { let alias_name = format!("alias.{}", command); let user_alias = match config.get_string(&alias_name) { @@ -60,12 +79,10 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult None, Err(_) => config.get::>>(&alias_name)?, }; - let result = user_alias.or_else(|| match command { - "b" => Some(vec!["build".to_string()]), - "c" => Some(vec!["check".to_string()]), - "r" => Some(vec!["run".to_string()]), - "t" => Some(vec!["test".to_string()]), - _ => None, + + let result = user_alias.or_else(|| match builtin_aliases_execs(command) { + Some(command_str) => Some(vec![command_str.to_string()]), + None => None, }); Ok(result) } @@ -105,6 +122,12 @@ fn list_commands(config: &Config) -> BTreeSet { about: cmd.p.meta.about.map(|s| s.to_string()), }); } + for command in &BUILTIN_ALIASES { + commands.insert(CommandInfo::BuiltIn { + name: command.0.to_string(), + about: Some(command.1.to_string()), + }); + } commands } From 7b16c7c1305de3551d1d5cdc4dc2bf13861d9685 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Fri, 24 Jul 2020 23:40:49 +0200 Subject: [PATCH 2/3] Add test for listing builtin aliases Added a test that checks that the aliases that currently are builtin with cargo are indeed being printed with the rest of the commands when `cargo --list` is called. Closes #8486 --- tests/testsuite/cargo_command.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/testsuite/cargo_command.rs b/tests/testsuite/cargo_command.rs index fa4e05a74d2..667c2933886 100644 --- a/tests/testsuite/cargo_command.rs +++ b/tests/testsuite/cargo_command.rs @@ -78,6 +78,17 @@ fn list_commands_with_descriptions() { .run(); } +#[cargo_test] +fn list_aliases_with_descriptions() { + let p = project().build(); + p.cargo("--list") + .with_stdout_contains(" b alias: build") + .with_stdout_contains(" c alias: check") + .with_stdout_contains(" r alias: run") + .with_stdout_contains(" t alias: test") + .run(); +} + #[cargo_test] fn list_command_looks_at_path() { let proj = project().build(); From 2ae8df65d84d3dccd38977cda308e5464deeccbe Mon Sep 17 00:00:00 2001 From: CPerezz Date: Tue, 28 Jul 2020 20:53:59 +0200 Subject: [PATCH 3/3] Remomve builtin_aliases duplicate declaration As @ehuss correctly suggested, we could just declare in one `const` structure for every builtin alias the following: `(alias, aliased_command, description)`. Therefore, the suggestion has been applied and the `BUILTIN_ALIASES` const has been refactored. Also, the `builtin_aliases_execs` now parses the `BUILTIN_ALIASES` const searching for a "possible alias command" returning an option with the previous info structure or `None`. --- src/bin/cargo/main.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 6248ea68fd6..64a465108a1 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -47,23 +47,19 @@ fn main() { } } -const BUILTIN_ALIASES: [(&str, &str); 4] = [ - ("b", "alias: build"), - ("c", "alias: check"), - ("r", "alias: run"), - ("t", "alias: test"), +/// Table for defining the aliases which come builtin in `Cargo`. +/// The contents are structured as: `(alias, aliased_command, description)`. +const BUILTIN_ALIASES: [(&str, &str, &str); 4] = [ + ("b", "build", "alias: build"), + ("c", "check", "alias: check"), + ("r", "run", "alias: run"), + ("t", "test", "alias: test"), ]; /// Function which contains the list of all of the builtin aliases and it's /// corresponding execs represented as &str. -fn builtin_aliases_execs(cmd: &str) -> Option<&str> { - match cmd { - "b" => Some("build"), - "c" => Some("check"), - "r" => Some("run"), - "t" => Some("test"), - _ => None, - } +fn builtin_aliases_execs(cmd: &str) -> Option<&(&str, &str, &str)> { + BUILTIN_ALIASES.iter().find(|alias| alias.0 == cmd) } fn aliased_command(config: &Config, command: &str) -> CargoResult>> { @@ -81,7 +77,7 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult Some(vec![command_str.to_string()]), + Some(command_str) => Some(vec![command_str.1.to_string()]), None => None, }); Ok(result) @@ -122,10 +118,13 @@ fn list_commands(config: &Config) -> BTreeSet { about: cmd.p.meta.about.map(|s| s.to_string()), }); } + + // Add the builtin_aliases and them descriptions to the + // `commands` `BTreeSet`. for command in &BUILTIN_ALIASES { commands.insert(CommandInfo::BuiltIn { name: command.0.to_string(), - about: Some(command.1.to_string()), + about: Some(command.2.to_string()), }); }