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.
This commit is contained in:
CPerezz 2020-07-24 23:26:25 +02:00
parent d83a2d609d
commit 26beca06fc

View File

@ -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<Option<Vec<String>>> {
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<Option<Vec<Str
Ok(None) => None,
Err(_) => config.get::<Option<Vec<String>>>(&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<CommandInfo> {
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
}