mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
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`.
This commit is contained in:
parent
7b16c7c130
commit
2ae8df65d8
@ -47,23 +47,19 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const BUILTIN_ALIASES: [(&str, &str); 4] = [
|
/// Table for defining the aliases which come builtin in `Cargo`.
|
||||||
("b", "alias: build"),
|
/// The contents are structured as: `(alias, aliased_command, description)`.
|
||||||
("c", "alias: check"),
|
const BUILTIN_ALIASES: [(&str, &str, &str); 4] = [
|
||||||
("r", "alias: run"),
|
("b", "build", "alias: build"),
|
||||||
("t", "alias: test"),
|
("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
|
/// Function which contains the list of all of the builtin aliases and it's
|
||||||
/// corresponding execs represented as &str.
|
/// corresponding execs represented as &str.
|
||||||
fn builtin_aliases_execs(cmd: &str) -> Option<&str> {
|
fn builtin_aliases_execs(cmd: &str) -> Option<&(&str, &str, &str)> {
|
||||||
match cmd {
|
BUILTIN_ALIASES.iter().find(|alias| alias.0 == 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>>> {
|
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
|
||||||
@ -81,7 +77,7 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<Str
|
|||||||
};
|
};
|
||||||
|
|
||||||
let result = user_alias.or_else(|| match builtin_aliases_execs(command) {
|
let result = user_alias.or_else(|| match builtin_aliases_execs(command) {
|
||||||
Some(command_str) => Some(vec![command_str.to_string()]),
|
Some(command_str) => Some(vec![command_str.1.to_string()]),
|
||||||
None => None,
|
None => None,
|
||||||
});
|
});
|
||||||
Ok(result)
|
Ok(result)
|
||||||
@ -122,10 +118,13 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
|
|||||||
about: cmd.p.meta.about.map(|s| s.to_string()),
|
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 {
|
for command in &BUILTIN_ALIASES {
|
||||||
commands.insert(CommandInfo::BuiltIn {
|
commands.insert(CommandInfo::BuiltIn {
|
||||||
name: command.0.to_string(),
|
name: command.0.to_string(),
|
||||||
about: Some(command.1.to_string()),
|
about: Some(command.2.to_string()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user