Skip to content

Commit 48c798c

Browse files
committed
refactor(cli): rewrite rustup (check|default) with clap-derive
1 parent 8983028 commit 48c798c

File tree

6 files changed

+31
-41
lines changed

6 files changed

+31
-41
lines changed

src/cli/rustup_mode.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ use crate::{
3737
toolchain::{
3838
distributable::DistributableToolchain,
3939
names::{
40-
maybe_resolvable_toolchainame_parser, partial_toolchain_desc_parser,
41-
resolvable_local_toolchainame_parser, resolvable_toolchainame_parser,
42-
CustomToolchainName, LocalToolchainName, MaybeResolvableToolchainName,
43-
ResolvableLocalToolchainName, ResolvableToolchainName, ToolchainName,
40+
partial_toolchain_desc_parser, resolvable_local_toolchainame_parser,
41+
resolvable_toolchainame_parser, CustomToolchainName, LocalToolchainName,
42+
MaybeResolvableToolchainName, ResolvableLocalToolchainName, ResolvableToolchainName,
43+
ToolchainName,
4444
},
4545
toolchain::Toolchain,
4646
},
@@ -129,6 +129,16 @@ enum RustupSubcmd {
129129
force_non_host: bool,
130130
},
131131

132+
/// Check for updates to Rust toolchains and rustup
133+
Check,
134+
135+
/// Set the default toolchain
136+
#[command(after_help = DEFAULT_HELP)]
137+
Default {
138+
#[arg(help = MAYBE_RESOLVABLE_TOOLCHAIN_ARG_HELP)]
139+
toolchain: Option<MaybeResolvableToolchainName>,
140+
},
141+
132142
/// Modify or query the installed toolchains
133143
Toolchain {
134144
#[command(subcommand)]
@@ -273,6 +283,8 @@ impl Rustup {
273283
ToolchainSubcmd::Link { toolchain, path } => toolchain_link(cfg, &toolchain, &path),
274284
ToolchainSubcmd::Uninstall { opts } => toolchain_remove(cfg, opts),
275285
},
286+
RustupSubcmd::Check => check_updates(cfg),
287+
RustupSubcmd::Default { toolchain } => default_(cfg, toolchain),
276288
}
277289
}
278290
}
@@ -349,11 +361,10 @@ pub fn main() -> Result<utils::ExitCode> {
349361
Ok(match matches.subcommand() {
350362
Some(s) => match s {
351363
("dump-testament", _) => common::dump_testament()?,
352-
("show" | "update" | "install" | "uninstall" | "toolchain", _) => {
353-
Rustup::from_arg_matches(&matches)?.dispatch(cfg)?
354-
}
355-
("check", _) => check_updates(cfg)?,
356-
("default", m) => default_(cfg, m)?,
364+
(
365+
"show" | "update" | "install" | "uninstall" | "toolchain" | "check" | "default",
366+
_,
367+
) => Rustup::from_arg_matches(&matches)?.dispatch(cfg)?,
357368
("target", c) => match c.subcommand() {
358369
Some(s) => match s {
359370
("list", m) => handle_epipe(target_list(cfg, m))?,
@@ -459,18 +470,6 @@ pub(crate) fn cli() -> Command {
459470
.about("Dump information about the build")
460471
.hide(true), // Not for users, only CI
461472
)
462-
.subcommand(Command::new("check").about("Check for updates to Rust toolchains and rustup"))
463-
.subcommand(
464-
Command::new("default")
465-
.about("Set the default toolchain")
466-
.after_help(DEFAULT_HELP)
467-
.arg(
468-
Arg::new("toolchain")
469-
.help(MAYBE_RESOLVABLE_TOOLCHAIN_ARG_HELP)
470-
.required(false)
471-
.value_parser(maybe_resolvable_toolchainame_parser),
472-
),
473-
)
474473
.subcommand(
475474
Command::new("target")
476475
.about("Modify a toolchain's supported targets")
@@ -797,10 +796,10 @@ fn maybe_upgrade_data(cfg: &Cfg, m: &ArgMatches) -> Result<bool> {
797796
}
798797
}
799798

800-
fn default_(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
799+
fn default_(cfg: &Cfg, toolchain: Option<MaybeResolvableToolchainName>) -> Result<utils::ExitCode> {
801800
common::warn_if_host_is_emulated();
802801

803-
if let Some(toolchain) = m.get_one::<MaybeResolvableToolchainName>("toolchain") {
802+
if let Some(toolchain) = toolchain {
804803
match toolchain.to_owned() {
805804
MaybeResolvableToolchainName::None => {
806805
cfg.set_default(None)?;

src/toolchain/names.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,6 @@ impl Display for MaybeResolvableToolchainName {
229229
}
230230
}
231231

232-
/// Thunk to avoid errors like
233-
/// = note: `fn(&'2 str) -> Result<CustomToolchainName, <CustomToolchainName as TryFrom<&'2 str>>::Error> {<CustomToolchainName as TryFrom<&'2 str>>::try_from}` must implement `FnOnce<(&'1 str,)>`, for any lifetime `'1`...
234-
/// = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
235-
pub(crate) fn maybe_resolvable_toolchainame_parser(
236-
value: &str,
237-
) -> Result<MaybeResolvableToolchainName, InvalidName> {
238-
MaybeResolvableToolchainName::try_from(value)
239-
}
240-
241232
/// ResolvableToolchainName + none, for overriding default-has-a-value
242233
/// situations in the CLI with an official toolchain name or none
243234
#[derive(Debug, Clone)]

tests/suite/cli-ui/rustup/rustup_default_cmd_help_flag_stdout.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
bin.name = "rustup"
2-
args = ["default","--help"]
2+
args = ["default", "--help"]
33
stdout = """
44
...
55
Set the default toolchain
66
7-
Usage: rustup[EXE] default [toolchain]
7+
Usage: rustup[EXE] default [TOOLCHAIN]
88
99
Arguments:
10-
[toolchain] 'none', a toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain
10+
[TOOLCHAIN] 'none', a toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain
1111
name. For more information see `rustup help toolchain`
1212
1313
Options:

tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
check Check for updates to Rust toolchains and rustup
13-
default Set the default toolchain
1412
target Modify a toolchain's supported targets
1513
component Modify a toolchain's installed components
1614
override Modify toolchain overrides for directories
@@ -23,6 +21,8 @@ Commands:
2321
completions Generate tab-completion scripts for your shell
2422
show Show the active and installed toolchains or profiles
2523
update Update Rust toolchains and rustup
24+
check Check for updates to Rust toolchains and rustup
25+
default Set the default toolchain
2626
toolchain Modify or query the installed toolchains
2727
help Print this message or the help of the given subcommand(s)
2828

tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
check Check for updates to Rust toolchains and rustup
13-
default Set the default toolchain
1412
target Modify a toolchain's supported targets
1513
component Modify a toolchain's installed components
1614
override Modify toolchain overrides for directories
@@ -23,6 +21,8 @@ Commands:
2321
completions Generate tab-completion scripts for your shell
2422
show Show the active and installed toolchains or profiles
2523
update Update Rust toolchains and rustup
24+
check Check for updates to Rust toolchains and rustup
25+
default Set the default toolchain
2626
toolchain Modify or query the installed toolchains
2727
help Print this message or the help of the given subcommand(s)
2828

tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ The Rust toolchain installer
99
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]
1010
1111
Commands:
12-
check Check for updates to Rust toolchains and rustup
13-
default Set the default toolchain
1412
target Modify a toolchain's supported targets
1513
component Modify a toolchain's installed components
1614
override Modify toolchain overrides for directories
@@ -23,6 +21,8 @@ Commands:
2321
completions Generate tab-completion scripts for your shell
2422
show Show the active and installed toolchains or profiles
2523
update Update Rust toolchains and rustup
24+
check Check for updates to Rust toolchains and rustup
25+
default Set the default toolchain
2626
toolchain Modify or query the installed toolchains
2727
help Print this message or the help of the given subcommand(s)
2828

0 commit comments

Comments
 (0)