@@ -92,38 +92,39 @@ impl<T: Into<String>> From<T> for OverrideFile {
9292 }
9393}
9494
95- // Represents the reason why the active toolchain is active .
95+ // Represents the source that determined the current active toolchain.
9696#[derive(Debug)]
97- pub(crate) enum ActiveReason {
97+ pub(crate) enum ActiveSource {
9898 Default,
9999 Environment,
100100 CommandLine,
101101 OverrideDB(PathBuf),
102102 ToolchainFile(PathBuf),
103103}
104104
105- impl ActiveReason {
106- /// Format `ActiveReason` for setting the `RUSTUP_TOOLCHAIN_SOURCE` environment variable.
107- pub fn to_source(&self) -> &str {
105+ impl ActiveSource {
106+ pub fn to_reason(&self) -> String {
108107 match self {
108+ Self::Default => String::from("it's the default toolchain"),
109+ Self::Environment => {
110+ String::from("overridden by environment variable RUSTUP_TOOLCHAIN")
111+ }
112+ Self::CommandLine => String::from("overridden by +toolchain on the command line"),
113+ Self::OverrideDB(path) => format!("directory override for '{}'", path.display()),
114+ Self::ToolchainFile(path) => format!("overridden by '{}'", path.display()),
115+ }
116+ }
117+ }
118+
119+ impl Display for ActiveSource {
120+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121+ f.write_str(match self {
109122 Self::Default => "default",
110123 Self::Environment => "env",
111124 Self::CommandLine => "cli",
112125 Self::OverrideDB(_) => "path-override",
113126 Self::ToolchainFile(_) => "toolchain-file",
114- }
115- }
116- }
117-
118- impl Display for ActiveReason {
119- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
120- match self {
121- Self::Default => write!(f, "it's the default toolchain"),
122- Self::Environment => write!(f, "overridden by environment variable RUSTUP_TOOLCHAIN"),
123- Self::CommandLine => write!(f, "overridden by +toolchain on the command line"),
124- Self::OverrideDB(path) => write!(f, "directory override for '{}'", path.display()),
125- Self::ToolchainFile(path) => write!(f, "overridden by '{}'", path.display()),
126- }
127+ })
127128 }
128129}
129130
@@ -517,7 +518,7 @@ impl<'a> Cfg<'a> {
517518 pub(crate) async fn maybe_ensure_active_toolchain(
518519 &self,
519520 force_ensure: Option<bool>,
520- ) -> Result<Option<(LocalToolchainName, ActiveReason )>> {
521+ ) -> Result<Option<(LocalToolchainName, ActiveSource )>> {
521522 let should_ensure = if let Some(force) = force_ensure {
522523 force
523524 } else {
@@ -536,39 +537,39 @@ impl<'a> Cfg<'a> {
536537 }
537538 }
538539
539- pub(crate) fn active_toolchain(&self) -> Result<Option<(LocalToolchainName, ActiveReason )>> {
540+ pub(crate) fn active_toolchain(&self) -> Result<Option<(LocalToolchainName, ActiveSource )>> {
540541 Ok(
541- if let Some((override_config, reason )) = self.find_override_config()? {
542- Some((override_config.into_local_toolchain_name(), reason ))
542+ if let Some((override_config, source )) = self.find_override_config()? {
543+ Some((override_config.into_local_toolchain_name(), source ))
543544 } else {
544545 self.get_default()?
545- .map(|x| (x.into(), ActiveReason ::Default))
546+ .map(|x| (x.into(), ActiveSource ::Default))
546547 },
547548 )
548549 }
549550
550- fn find_override_config(&self) -> Result<Option<(OverrideCfg, ActiveReason )>> {
551- let override_config: Option<(OverrideCfg, ActiveReason )> =
551+ fn find_override_config(&self) -> Result<Option<(OverrideCfg, ActiveSource )>> {
552+ let override_config: Option<(OverrideCfg, ActiveSource )> =
552553 // First check +toolchain override from the command line
553554 if let Some(name) = &self.toolchain_override {
554555 let override_config = name.resolve(&self.get_default_host_triple()?)?.into();
555- Some((override_config, ActiveReason ::CommandLine))
556+ Some((override_config, ActiveSource ::CommandLine))
556557 }
557558 // Then check the RUSTUP_TOOLCHAIN environment variable
558559 else if let Some(name) = &self.env_override {
559560 // Because path based toolchain files exist, this has to support
560561 // custom, distributable, and absolute path toolchains otherwise
561562 // rustup's export of a RUSTUP_TOOLCHAIN when running a process will
562563 // error when a nested rustup invocation occurs
563- Some((name.clone().into(), ActiveReason ::Environment))
564+ Some((name.clone().into(), ActiveSource ::Environment))
564565 }
565566 // Then walk up the directory tree from 'path' looking for either the
566567 // directory in the override database, or a `rust-toolchain{.toml}` file,
567568 // in that order.
568- else if let Some((override_cfg, active_reason )) = self.settings_file.with(|s| {
569+ else if let Some((override_cfg, active_source )) = self.settings_file.with(|s| {
569570 self.find_override_from_dir_walk(&self.current_dir, s)
570571 })? {
571- Some((override_cfg, active_reason ))
572+ Some((override_cfg, active_source ))
572573 }
573574 // Otherwise, there is no override.
574575 else {
@@ -582,14 +583,14 @@ impl<'a> Cfg<'a> {
582583 &self,
583584 dir: &Path,
584585 settings: &Settings,
585- ) -> Result<Option<(OverrideCfg, ActiveReason )>> {
586+ ) -> Result<Option<(OverrideCfg, ActiveSource )>> {
586587 let notify = self.notify_handler.as_ref();
587588 let mut dir = Some(dir);
588589
589590 while let Some(d) = dir {
590591 // First check the override database
591592 if let Some(name) = settings.dir_override(d) {
592- let reason = ActiveReason ::OverrideDB(d.to_owned());
593+ let source = ActiveSource ::OverrideDB(d.to_owned());
593594 // Note that `rustup override set` fully resolves it's input
594595 // before writing to settings.toml, so resolving here may not
595596 // be strictly necessary (could instead model as ToolchainName).
@@ -599,7 +600,7 @@ impl<'a> Cfg<'a> {
599600 let toolchain_name = ResolvableToolchainName::try_from(name)?
600601 .resolve(&get_default_host_triple(settings, self.process))?;
601602 let override_cfg = toolchain_name.into();
602- return Ok(Some((override_cfg, reason )));
603+ return Ok(Some((override_cfg, source )));
603604 }
604605
605606 // Then look for 'rust-toolchain' or 'rust-toolchain.toml'
@@ -676,9 +677,9 @@ impl<'a> Cfg<'a> {
676677 }
677678 }
678679
679- let reason = ActiveReason ::ToolchainFile(toolchain_file);
680+ let source = ActiveSource ::ToolchainFile(toolchain_file);
680681 let override_cfg = OverrideCfg::from_file(self, override_file)?;
681- return Ok(Some((override_cfg, reason )));
682+ return Ok(Some((override_cfg, source )));
682683 }
683684
684685 dir = d.parent();
@@ -772,8 +773,8 @@ impl<'a> Cfg<'a> {
772773 &self,
773774 force_non_host: bool,
774775 verbose: bool,
775- ) -> Result<(LocalToolchainName, ActiveReason )> {
776- if let Some((override_config, reason )) = self.find_override_config()? {
776+ ) -> Result<(LocalToolchainName, ActiveSource )> {
777+ if let Some((override_config, source )) = self.find_override_config()? {
777778 let toolchain = override_config.clone().into_local_toolchain_name();
778779 if let OverrideCfg::Official {
779780 toolchain,
@@ -792,18 +793,18 @@ impl<'a> Cfg<'a> {
792793 )
793794 .await?;
794795 } else {
795- Toolchain::with_reason (self, toolchain.clone(), &reason )?;
796+ Toolchain::with_source (self, toolchain.clone(), &source )?;
796797 }
797- Ok((toolchain, reason ))
798+ Ok((toolchain, source ))
798799 } else if let Some(toolchain) = self.get_default()? {
799- let reason = ActiveReason ::Default;
800+ let source = ActiveSource ::Default;
800801 if let ToolchainName::Official(desc) = &toolchain {
801802 self.ensure_installed(desc, vec![], vec![], None, force_non_host, verbose)
802803 .await?;
803804 } else {
804- Toolchain::with_reason (self, toolchain.clone().into(), &reason )?;
805+ Toolchain::with_source (self, toolchain.clone().into(), &source )?;
805806 }
806- Ok((toolchain.into(), reason ))
807+ Ok((toolchain.into(), source ))
807808 } else {
808809 Err(no_toolchain_error(self.process))
809810 }
0 commit comments