Skip to content

Commit 2ae8df6

Browse files
committed
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`.
1 parent 7b16c7c commit 2ae8df6

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/bin/cargo/main.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,19 @@ 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"),
50+
/// Table for defining the aliases which come builtin in `Cargo`.
51+
/// The contents are structured as: `(alias, aliased_command, description)`.
52+
const BUILTIN_ALIASES: [(&str, &str, &str); 4] = [
53+
("b", "build", "alias: build"),
54+
("c", "check", "alias: check"),
55+
("r", "run", "alias: run"),
56+
("t", "test", "alias: test"),
5557
];
5658

5759
/// Function which contains the list of all of the builtin aliases and it's
5860
/// 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-
}
61+
fn builtin_aliases_execs(cmd: &str) -> Option<&(&str, &str, &str)> {
62+
BUILTIN_ALIASES.iter().find(|alias| alias.0 == cmd)
6763
}
6864

6965
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
8177
};
8278

8379
let result = user_alias.or_else(|| match builtin_aliases_execs(command) {
84-
Some(command_str) => Some(vec![command_str.to_string()]),
80+
Some(command_str) => Some(vec![command_str.1.to_string()]),
8581
None => None,
8682
});
8783
Ok(result)
@@ -122,10 +118,13 @@ fn list_commands(config: &Config) -> BTreeSet<CommandInfo> {
122118
about: cmd.p.meta.about.map(|s| s.to_string()),
123119
});
124120
}
121+
122+
// Add the builtin_aliases and them descriptions to the
123+
// `commands` `BTreeSet`.
125124
for command in &BUILTIN_ALIASES {
126125
commands.insert(CommandInfo::BuiltIn {
127126
name: command.0.to_string(),
128-
about: Some(command.1.to_string()),
127+
about: Some(command.2.to_string()),
129128
});
130129
}
131130

0 commit comments

Comments
 (0)