Skip to content

Commit

Permalink
refactor(names): replace maybe_official_toolchainame_parser with `i…
Browse files Browse the repository at this point in the history
…mpl FromStr`
  • Loading branch information
rami3l committed Jan 19, 2024
1 parent 81f984e commit 0d85d0f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/cli/setup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -39,10 +39,14 @@ struct RustupInit {
default_host: Option<String>,

/// 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<MaybeOfficialToolchainName>,

#[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
Expand Down
17 changes: 8 additions & 9 deletions src/toolchain/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err> {
$to::validate(value)
}
}
};
($from:ty, $to:ident) => {
impl TryFrom<$from> for $to {
Expand Down Expand Up @@ -264,15 +272,6 @@ impl Display for MaybeOfficialToolchainName {
}
}

/// 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_official_toolchainame_parser(
value: &str,
) -> Result<MaybeOfficialToolchainName, InvalidName> {
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.
Expand Down

0 comments on commit 0d85d0f

Please sign in to comment.