Skip to content
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
13 changes: 6 additions & 7 deletions src/cli/setup_mode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use clap::{builder::PossibleValuesParser, Arg, ArgAction, Command};
use clap::{builder::PossibleValuesParser, value_parser, Arg, ArgAction, Command};

use crate::{
cli::{
Expand All @@ -9,12 +9,14 @@ use crate::{
currentprocess::{argsource::ArgSource, filesource::StdoutSource},
dist::dist::Profile,
process,
toolchain::names::{maybe_official_toolchainame_parser, MaybeOfficialToolchainName},
toolchain::names::MaybeOfficialToolchainName,
utils::utils,
};

#[cfg_attr(feature = "otel", tracing::instrument)]
pub fn main() -> Result<utils::ExitCode> {
use clap::error::ErrorKind;

let args: Vec<_> = process().args().collect();
let arg1 = args.get(1).map(|a| &**a);

Expand Down Expand Up @@ -66,7 +68,7 @@ pub fn main() -> Result<utils::ExitCode> {
.long("default-toolchain")
.num_args(1)
.help("Choose a default toolchain to install. Use 'none' to not install any toolchains at all")
.value_parser(maybe_official_toolchainame_parser)
.value_parser(value_parser!(MaybeOfficialToolchainName))
)
.arg(
Arg::new("profile")
Expand Down Expand Up @@ -107,10 +109,7 @@ pub fn main() -> Result<utils::ExitCode> {

let matches = match cli.try_get_matches_from(process().args_os()) {
Ok(matches) => matches,
Err(e)
if e.kind() == clap::error::ErrorKind::DisplayHelp
|| e.kind() == clap::error::ErrorKind::DisplayVersion =>
{
Err(e) if [ErrorKind::DisplayHelp, ErrorKind::DisplayVersion].contains(&e.kind()) => {
write!(process().stdout().lock(), "{e}")?;
return Ok(utils::ExitCode(0));
}
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