Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): rewrite rustup with clap_derive #3596

Merged
merged 13 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,632 changes: 772 additions & 860 deletions src/cli/rustup_mode.rs

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub(crate) enum Notification<'a> {
ReadMetadataVersion(&'a str),
NonFatalError(&'a anyhow::Error),
UpgradeRemovesToolchains,
PlainVerboseMessage(&'a str),
/// Both `rust-toolchain` and `rust-toolchain.toml` exist within a directory
DuplicateToolchainFile {
rust_toolchain: &'a Path,
Expand Down Expand Up @@ -68,7 +67,6 @@ impl<'a> Notification<'a> {
| UpdatingToolchain(_)
| ReadMetadataVersion(_)
| InstalledToolchain(_)
| PlainVerboseMessage(_)
| UpdateHashMatches => NotificationLevel::Verbose,
SetDefaultToolchain(_)
| SetOverrideToolchain(_, _)
Expand Down Expand Up @@ -124,7 +122,6 @@ impl<'a> Display for Notification<'a> {
f,
"this upgrade will remove all existing toolchains. you will need to reinstall them"
),
PlainVerboseMessage(r) => write!(f, "{r}"),
DuplicateToolchainFile {
rust_toolchain,
rust_toolchain_toml,
Expand Down
42 changes: 0 additions & 42 deletions src/toolchain/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,6 @@ fn validate(candidate: &str) -> Result<&str, InvalidName> {
}
}

/// Thunk to avoid errors like
/// = 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`...
/// = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
pub(crate) fn partial_toolchain_desc_parser(
value: &str,
) -> Result<PartialToolchainDesc, anyhow::Error> {
value.parse::<PartialToolchainDesc>()
}

/// A toolchain name from user input.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum ResolvableToolchainName {
Expand Down Expand Up @@ -185,15 +176,6 @@ impl Display for ResolvableToolchainName {
}
}

/// Thunk to avoid errors like
/// = 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`...
/// = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
pub(crate) fn resolvable_toolchainame_parser(
value: &str,
) -> Result<ResolvableToolchainName, InvalidName> {
ResolvableToolchainName::try_from(value)
}

/// A toolchain name from user input. MaybeToolchainName accepts 'none' or a
/// custom or resolvable official name. Possibly this should be an Option with a
/// local trait for our needs.
Expand Down Expand Up @@ -229,15 +211,6 @@ impl Display for MaybeResolvableToolchainName {
}
}

/// Thunk to avoid errors like
/// = 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`...
/// = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
pub(crate) fn maybe_resolvable_toolchainame_parser(
value: &str,
) -> Result<MaybeResolvableToolchainName, InvalidName> {
MaybeResolvableToolchainName::try_from(value)
}

/// ResolvableToolchainName + none, for overriding default-has-a-value
/// situations in the CLI with an official toolchain name or none
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -375,12 +348,6 @@ impl Display for ResolvableLocalToolchainName {
}
}

pub(crate) fn resolvable_local_toolchainame_parser(
value: &str,
) -> Result<ResolvableLocalToolchainName, InvalidName> {
ResolvableLocalToolchainName::try_from(value)
}

/// LocalToolchainName can be used in calls to Cfg that alter configuration,
/// like setting overrides, or that depend on configuration, like calculating
/// the toolchain directory. It is not used to model the RUSTUP_TOOLCHAIN
Expand Down Expand Up @@ -458,15 +425,6 @@ impl Display for CustomToolchainName {
}
}

/// Thunk to avoid
/// = 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`...
/// = note: ...but it actually implements `FnOnce<(&'2 str,)>`, for some specific lifetime `'2`
pub(crate) fn custom_toolchain_name_parser(
value: &str,
) -> Result<CustomToolchainName, InvalidName> {
CustomToolchainName::try_from(value)
}

/// An toolchain specified just via its path. Relative paths enable arbitrary
/// code execution in a rust dir, so as a partial mitigation is limited to
/// absolute paths.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
bin.name = "rustup"
args = ["completions","--help"]
args = ["completions", "--help"]
stdout = """
...
Generate tab-completion scripts for your shell

Usage: rustup[EXE] completions [shell] [command]
Usage: rustup[EXE] completions <SHELL> [COMMAND]

Arguments:
[shell] [possible values: bash, elvish, fish, powershell, zsh]
[command] [possible values: rustup, cargo]
<SHELL> [possible values: bash, elvish, fish, powershell, zsh]
[COMMAND] [default: rustup] [possible values: rustup, cargo]

Options:
-h, --help Print help
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
bin.name = "rustup"
args = ["component","add","--help"]
args = ["component", "add", "--help"]
stdout = """
...
Add a component to a Rust toolchain

Usage: rustup[EXE] component add [OPTIONS] <component>...
Usage: rustup[EXE] component add [OPTIONS] <COMPONENT>...

Arguments:
<component>...
<COMPONENT>...

Options:
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
information see `rustup help toolchain`
--target <target>
--target <TARGET>
-h, --help Print help
"""
stderr = ""
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
bin.name = "rustup"
args = ["component","list","--help"]
args = ["component", "list", "--help"]
stdout = """
...
List installed and available components

Usage: rustup[EXE] component list [OPTIONS]

Options:
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
information see `rustup help toolchain`
--installed List only installed components
-h, --help Print help
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
bin.name = "rustup"
args = ["component","remove","--help"]
args = ["component", "remove", "--help"]
stdout = """
...
Remove a component from a Rust toolchain

Usage: rustup[EXE] component remove [OPTIONS] <component>...
Usage: rustup[EXE] component remove [OPTIONS] <COMPONENT>...

Arguments:
<component>...
<COMPONENT>...

Options:
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
information see `rustup help toolchain`
--target <target>
--target <TARGET>
-h, --help Print help
"""
stderr = ""
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
bin.name = "rustup"
args = ["default","--help"]
args = ["default", "--help"]
stdout = """
...
Set the default toolchain

Usage: rustup[EXE] default [toolchain]
Usage: rustup[EXE] default [TOOLCHAIN]

Arguments:
[toolchain] 'none', a toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain
[TOOLCHAIN] 'none', a toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain
name. For more information see `rustup help toolchain`

Options:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
bin.name = "rustup"
args = ["doc","--help"]
args = ["doc", "--help"]
stdout = """
...
Open the documentation for the current toolchain

Usage: rustup[EXE] doc [OPTIONS] [topic]
Usage: rustup[EXE] doc [OPTIONS] [TOPIC]

Arguments:
[topic] Topic such as 'core', 'fn', 'usize', 'eprintln!', 'core::arch', 'alloc::format!',
[TOPIC] Topic such as 'core', 'fn', 'usize', 'eprintln!', 'core::arch', 'alloc::format!',
'std::fs', 'std::fs::read_dir', 'std::io::Bytes', 'std::iter::Sum',
'std::io::error::Result' etc...

Options:
--path Only print the path to the documentation
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
information see `rustup help toolchain`
--alloc The Rust core allocation and collections library
--book The Rust Programming Language book
Expand Down
2 changes: 1 addition & 1 deletion tests/suite/cli-ui/rustup/rustup_help_cmd_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Commands:
help Print this message or the help of the given subcommand(s)

Arguments:
[+toolchain] release channel (e.g. +stable) or custom toolchain to set override
[+toolchain] Release channel (e.g. +stable) or custom toolchain to set override

Options:
-v, --verbose Enable verbose output
Expand Down
2 changes: 1 addition & 1 deletion tests/suite/cli-ui/rustup/rustup_help_flag_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Commands:
help Print this message or the help of the given subcommand(s)

Arguments:
[+toolchain] release channel (e.g. +stable) or custom toolchain to set override
[+toolchain] Release channel (e.g. +stable) or custom toolchain to set override

Options:
-v, --verbose Enable verbose output
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
bin.name = "rustup"
args = ["man","--help"]
args = ["man", "--help"]
stdout = """
...
View the man page for a given command

Usage: rustup[EXE] man [OPTIONS] <command>
Usage: rustup[EXE] man [OPTIONS] <COMMAND>

Arguments:
<command>
<COMMAND>

Options:
--toolchain <toolchain> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
--toolchain <TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more
information see `rustup help toolchain`
-h, --help Print help
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/cli-ui/rustup/rustup_only_options_stdout.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rustup [..]

The Rust toolchain installer

Usage: rustup [OPTIONS] [+toolchain] [COMMAND]
Usage: rustup[EXE] [OPTIONS] [+toolchain] [COMMAND]

Commands:
show Show the active and installed toolchains or profiles
Expand All @@ -28,7 +28,7 @@ Commands:

Arguments:
[+toolchain]
release channel (e.g. +stable) or custom toolchain to set override
Release channel (e.g. +stable) or custom toolchain to set override

Options:
-v, --verbose
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
bin.name = "rustup"
args = ["override","add","--help"]
args = ["override", "add", "--help"]
stdout = """
...
Set the override toolchain for a directory

Usage: rustup[EXE] override set [OPTIONS] <toolchain>
Usage: rustup[EXE] override set [OPTIONS] <TOOLCHAIN>

Arguments:
<toolchain> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name. For
<TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name. For
more information see `rustup help toolchain`

Options:
--path <path> Path to the directory
--path <PATH> Path to the directory
-h, --help Print help
"""
stderr = ""
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
bin.name = "rustup"
args = ["override","unset","--help"]
args = ["override", "remove", "--help"]
stdout = """
...
Remove the override toolchain for a directory

Usage: rustup[EXE] override unset [OPTIONS]

Options:
--path <path> Path to the directory
--path <PATH> Path to the directory
--nonexistent Remove override toolchain for all nonexistent directories
-h, --help Print help

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
bin.name = "rustup"
args = ["override","set","--help"]
args = ["override", "set", "--help"]
stdout = """
...
Set the override toolchain for a directory

Usage: rustup[EXE] override set [OPTIONS] <toolchain>
Usage: rustup[EXE] override set [OPTIONS] <TOOLCHAIN>

Arguments:
<toolchain> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name. For
<TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name. For
more information see `rustup help toolchain`

Options:
--path <path> Path to the directory
--path <PATH> Path to the directory
-h, --help Print help
"""
stderr = ""
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
bin.name = "rustup"
args = ["override","unset","--help"]
args = ["override", "unset", "--help"]
stdout = """
...
Remove the override toolchain for a directory

Usage: rustup[EXE] override unset [OPTIONS]

Options:
--path <path> Path to the directory
--path <PATH> Path to the directory
--nonexistent Remove override toolchain for all nonexistent directories
-h, --help Print help

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ stdout = """
...
Run a command with an environment configured for a given toolchain

Usage: rustup[EXE] run [OPTIONS] <toolchain> <command>...
Usage: rustup[EXE] run [OPTIONS] <TOOLCHAIN> <COMMAND>...

Arguments:
<toolchain> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name, or
<TOOLCHAIN> Toolchain name, such as 'stable', 'nightly', '1.8.0', or a custom toolchain name, or
an absolute path. For more information see `rustup help toolchain`
<command>...
<COMMAND>...

Options:
--install Install the requested toolchain if needed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bin.name = "rustup"
args = ["self","--help"]
args = ["self", "--help"]
stdout = """
...
Modify the rustup installation
Expand All @@ -8,8 +8,8 @@ Usage: rustup[EXE] self <COMMAND>

Commands:
update Download and install updates to rustup
uninstall Uninstall rustup.
upgrade-data Upgrade the internal data format.
uninstall Uninstall rustup
upgrade-data Upgrade the internal data format
help Print this message or the help of the given subcommand(s)

Options:
Expand Down
Loading
Loading