Skip to content

Commit 26beca0

Browse files
committed
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.
1 parent d83a2d6 commit 26beca0

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/bin/cargo/main.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ fn main() {
4747
}
4848
}
4949

50+
const BUILTIN_ALIASES: [(&str, &str); 4] = [
51+
("b", "alias: build"),
52+
("c", "alias: check"),
53+
("r", "alias: run"),
54+
("t", "alias: test"),
55+
];
56+
57+
/// Function which contains the list of all of the builtin aliases and it's
58+
/// corresponding execs represented as &str.
59+
fn builtin_aliases_execs(cmd: &str) -> Option<&str> {
60+
match cmd {
61+
"b" => Some("build"),
62+
"c" => Some("check"),
63+
"r" => Some("run"),
64+
"t" => Some("test"),
65+
_ => None,
66+
}
67+
}
68+
5069
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
5170
let alias_name = format!("alias.{}", command);
5271
let user_alias = match config.get_string(&alias_name) {
@@ -60,12 +79,10 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<Str
6079
Ok(None) => None,
6180
Err(_) => config.get::<Option<Vec<String>>>(&alias_name)?,
6281
};
63-
let result = user_alias.or_else(|| match command {
64-
"b" => Some(vec!["build".to_string()]),
65-
"c" => Some(vec!["check".to_string()]),
66-
"r" => Some(vec!["run".to_string()]),
67-
"t" => Some(vec!["test".to_string()]),
68-
_ => None,
82+
83+
let result = user_alias.or_else(|| match builtin_aliases_execs(command) {
84+
Some(command_str) => Some(vec![command_str.to_string()]),
85+
None => None,
6986
});
7087
Ok(result)
7188
}
@@ -105,6 +122,12 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
105122
about: cmd.p.meta.about.map(|s| s.to_string()),
106123
});
107124
}
125+
for command in &BUILTIN_ALIASES {
126+
commands.insert(CommandInfo::BuiltIn {
127+
name: command.0.to_string(),
128+
about: Some(command.1.to_string()),
129+
});
130+
}
108131

109132
commands
110133
}

0 commit comments

Comments
 (0)