From 55a1861a33e9409439454d24ec6a397aa111248e Mon Sep 17 00:00:00 2001 From: rami3l Date: Thu, 21 Dec 2023 12:42:46 +0800 Subject: [PATCH] refactor(names): replace `maybe_official_toolchainame_parser` with `impl FromStr` --- src/cli/setup_mode.rs | 10 +++++++--- src/toolchain/names.rs | 17 ++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/cli/setup_mode.rs b/src/cli/setup_mode.rs index 56cff5d87e..0b580a37b3 100644 --- a/src/cli/setup_mode.rs +++ b/src/cli/setup_mode.rs @@ -9,7 +9,7 @@ use crate::{ currentprocess::filesource::StdoutSource, dist::dist::Profile, process, - toolchain::names::{maybe_official_toolchainame_parser, MaybeOfficialToolchainName}, + toolchain::names::MaybeOfficialToolchainName, utils::utils, }; @@ -39,10 +39,14 @@ struct RustupInit { default_host: Option, /// Choose a default toolchain to install. Use 'none' to not install any toolchains at all - #[arg(long, value_parser = maybe_official_toolchainame_parser)] + #[arg(long)] default_toolchain: Option, - #[arg(long, value_parser = PossibleValuesParser::new(Profile::names()), default_value = Profile::default_name())] + #[arg( + long, + value_parser = PossibleValuesParser::new(Profile::names()), + default_value = Profile::default_name(), + )] profile: String, /// Component name to also install diff --git a/src/toolchain/names.rs b/src/toolchain/names.rs index b8d9bf013b..f472ac1db8 100644 --- a/src/toolchain/names.rs +++ b/src/toolchain/names.rs @@ -95,6 +95,14 @@ macro_rules! try_from_str { $to::validate(&value) } } + + impl FromStr for $to { + type Err = InvalidName; + + fn from_str(value: &str) -> std::result::Result { + $to::validate(value) + } + } }; ($from:ty, $to:ident) => { impl TryFrom<$from> for $to { @@ -264,15 +272,6 @@ impl Display for MaybeOfficialToolchainName { } } -/// Thunk to avoid errors like -/// = note: `fn(&'2 str) -> Result>::Error> {>::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_official_toolchainame_parser( - value: &str, -) -> Result { - MaybeOfficialToolchainName::try_from(value) -} - /// ToolchainName can be used in calls to Cfg that alter configuration, /// like setting overrides, or that depend on configuration, like calculating /// the toolchain directory.