Skip to content

Commit f439f48

Browse files
committed
refactor(cfg): extract Cfg::active_toolchain()
1 parent 671b874 commit f439f48

File tree

1 file changed

+18
-46
lines changed

1 file changed

+18
-46
lines changed

src/config.rs

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -526,61 +526,33 @@ impl<'a> Cfg<'a> {
526526
&self,
527527
force_install_active: Option<bool>,
528528
) -> Result<Option<(LocalToolchainName, ActiveReason)>> {
529-
let (components, targets, profile, toolchain, reason) = match self.find_override_config()? {
530-
Some((
531-
OverrideCfg::Official {
532-
components,
533-
targets,
534-
profile,
535-
toolchain,
536-
},
537-
reason,
538-
)) => (components, targets, profile, toolchain, reason),
539-
Some((override_config, reason)) => {
540-
return Ok(Some((override_config.into_local_toolchain_name(), reason)));
541-
}
542-
None => {
543-
return Ok(self
544-
.get_default()?
545-
.map(|x| (x.into(), ActiveReason::Default)));
546-
}
547-
};
548-
549529
let should_install_active = if let Some(force) = force_install_active {
550530
force
551531
} else {
552532
self.should_auto_install()?
553533
};
554-
555534
if !should_install_active {
556-
return Ok(Some(((&toolchain).into(), reason)));
535+
return self.active_toolchain();
557536
}
558537

559-
let components = components.iter().map(AsRef::as_ref).collect::<Vec<_>>();
560-
let targets = targets.iter().map(AsRef::as_ref).collect::<Vec<_>>();
561-
match DistributableToolchain::new(self, toolchain.clone()) {
562-
Err(RustupError::ToolchainNotInstalled { .. }) => {
563-
DistributableToolchain::install(
564-
self,
565-
&toolchain,
566-
&components,
567-
&targets,
568-
profile.unwrap_or_default(),
569-
false,
570-
)
571-
.await?;
572-
}
573-
Ok(mut distributable) => {
574-
if !distributable.components_exist(&components, &targets)? {
575-
distributable
576-
.update(&components, &targets, profile.unwrap_or_default())
577-
.await?;
578-
}
579-
}
580-
Err(e) => return Err(e.into()),
581-
};
538+
match self.find_or_install_active_toolchain(true, false).await {
539+
Ok(r) => Ok(Some(r)),
540+
Err(e) => match e.downcast_ref::<RustupError>() {
541+
Some(RustupError::ToolchainNotSelected(_)) => Ok(None),
542+
_ => Err(e),
543+
},
544+
}
545+
}
582546

583-
Ok(Some(((&toolchain).into(), reason)))
547+
pub(crate) fn active_toolchain(&self) -> Result<Option<(LocalToolchainName, ActiveReason)>> {
548+
Ok(
549+
if let Some((override_config, reason)) = self.find_override_config()? {
550+
Some((override_config.into_local_toolchain_name(), reason))
551+
} else {
552+
self.get_default()?
553+
.map(|x| (x.into(), ActiveReason::Default))
554+
},
555+
)
584556
}
585557

586558
fn find_override_config(&self) -> Result<Option<(OverrideCfg, ActiveReason)>> {

0 commit comments

Comments
 (0)