Skip to content

Commit 6610d10

Browse files
committed
ActiveReason -> ActiveSource
1 parent 7209fd3 commit 6610d10

File tree

5 files changed

+84
-80
lines changed

5 files changed

+84
-80
lines changed

src/cli/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ pub(crate) async fn list_toolchains(
331331
} else {
332332
let default_toolchain_name = cfg.get_default()?;
333333
let active_toolchain_name: Option<ToolchainName> =
334-
if let Ok(Some((LocalToolchainName::Named(toolchain), _reason))) =
334+
if let Ok(Some((LocalToolchainName::Named(toolchain), _source))) =
335335
cfg.maybe_ensure_active_toolchain(None).await
336336
{
337337
Some(toolchain)

src/cli/proxy_mode.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use anyhow::Result;
88
use crate::{
99
cli::{common::set_globals, job, self_update},
1010
command::run_command_for_dir,
11-
config::ActiveReason,
11+
config::ActiveSource,
1212
process::Process,
1313
toolchain::ResolvableLocalToolchainName,
1414
};
@@ -42,9 +42,9 @@ pub async fn main(arg0: &str, current_dir: PathBuf, process: &Process) -> Result
4242
set_env_source(
4343
&mut cmd,
4444
if toolchain_specified {
45-
Some(ActiveReason::CommandLine)
46-
} else if let Ok(Some((_, reason))) = cfg.active_toolchain() {
47-
Some(reason)
45+
Some(ActiveSource::CommandLine)
46+
} else if let Ok(Some((_, source))) = cfg.active_toolchain() {
47+
Some(source)
4848
} else {
4949
None
5050
},
@@ -54,8 +54,8 @@ pub async fn main(arg0: &str, current_dir: PathBuf, process: &Process) -> Result
5454

5555
/// Set the `RUSTUP_TOOLCHAIN_SOURCE` environment variable to indicate how the toolchain was
5656
/// determined.
57-
fn set_env_source(cmd: &mut Command, reason: Option<ActiveReason>) {
58-
if let Some(reason) = reason {
59-
cmd.env("RUSTUP_TOOLCHAIN_SOURCE", reason.to_source());
57+
fn set_env_source(cmd: &mut Command, source: Option<ActiveSource>) {
58+
if let Some(source) = source {
59+
cmd.env("RUSTUP_TOOLCHAIN_SOURCE", source.to_string());
6060
}
6161
}

src/cli/rustup_mode.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
topical_doc,
3131
},
3232
command, component_for_bin,
33-
config::{ActiveReason, Cfg},
33+
config::{ActiveSource, Cfg},
3434
dist::{
3535
AutoInstallMode, PartialToolchainDesc, Profile, TargetTriple,
3636
manifest::{Component, ComponentStatus},
@@ -781,10 +781,13 @@ async fn default_(
781781
}
782782
};
783783

784-
if let Some((toolchain, reason)) = cfg.active_toolchain()?
785-
&& !matches!(reason, ActiveReason::Default)
784+
if let Some((toolchain, source)) = cfg.active_toolchain()?
785+
&& !matches!(source, ActiveSource::Default)
786786
{
787-
info!("note that the toolchain '{toolchain}' is currently in use ({reason})");
787+
info!(
788+
"note that the toolchain '{toolchain}' is currently in use ({})",
789+
source.to_reason()
790+
);
788791
}
789792
} else {
790793
let default_toolchain = cfg
@@ -999,9 +1002,9 @@ async fn update(
9991002
exit_code &= self_update::self_update(cfg.process).await?;
10001003
}
10011004
} else if ensure_active_toolchain {
1002-
let (toolchain, reason) = cfg.ensure_active_toolchain(force_non_host, true).await?;
1005+
let (toolchain, source) = cfg.ensure_active_toolchain(force_non_host, true).await?;
10031006
info!("the active toolchain `{toolchain}` has been installed");
1004-
info!("it's active because: {reason}");
1007+
info!("it's active because: {}", source.to_reason());
10051008
} else {
10061009
exit_code &= common::update_all_channels(cfg, opts.force).await?;
10071010
if self_update {
@@ -1090,16 +1093,16 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
10901093
}
10911094

10921095
let installed_toolchains = cfg.list_toolchains()?;
1093-
let active_toolchain_and_reason: Option<(ToolchainName, ActiveReason)> =
1094-
if let Ok(Some((LocalToolchainName::Named(toolchain_name), reason))) =
1096+
let active_toolchain_and_source: Option<(ToolchainName, ActiveSource)> =
1097+
if let Ok(Some((LocalToolchainName::Named(toolchain_name), source))) =
10951098
cfg.maybe_ensure_active_toolchain(None).await
10961099
{
1097-
Some((toolchain_name, reason))
1100+
Some((toolchain_name, source))
10981101
} else {
10991102
None
11001103
};
11011104

1102-
let (active_toolchain_name, _active_reason) = active_toolchain_and_reason
1105+
let (active_toolchain_name, _active_source) = active_toolchain_and_source
11031106
.as_ref()
11041107
.map(|atar| (&atar.0, &atar.1))
11051108
.unzip();
@@ -1163,15 +1166,15 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
11631166

11641167
print_header::<Error>(&mut t, "active toolchain")?;
11651168

1166-
match active_toolchain_and_reason {
1167-
Some((active_toolchain_name, active_reason)) => {
1168-
let active_toolchain = Toolchain::with_reason(
1169+
match active_toolchain_and_source {
1170+
Some((active_toolchain_name, active_source)) => {
1171+
let active_toolchain = Toolchain::with_source(
11691172
cfg,
11701173
active_toolchain_name.clone().into(),
1171-
&active_reason,
1174+
&active_source,
11721175
)?;
11731176
writeln!(t.lock(), "name: {}", active_toolchain.name())?;
1174-
writeln!(t.lock(), "active because: {active_reason}")?;
1177+
writeln!(t.lock(), "active because: {}", active_source.to_reason())?;
11751178
if verbose {
11761179
writeln!(t.lock(), "compiler: {}", active_toolchain.rustc_version())?;
11771180
writeln!(t.lock(), "path: {}", active_toolchain.path().display())?;
@@ -1210,14 +1213,14 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
12101213
#[tracing::instrument(level = "trace", skip_all)]
12111214
async fn show_active_toolchain(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
12121215
match cfg.maybe_ensure_active_toolchain(None).await? {
1213-
Some((toolchain_name, reason)) => {
1214-
let toolchain = Toolchain::with_reason(cfg, toolchain_name.clone(), &reason)?;
1216+
Some((toolchain_name, source)) => {
1217+
let toolchain = Toolchain::with_source(cfg, toolchain_name.clone(), &source)?;
12151218
if verbose {
12161219
writeln!(
12171220
cfg.process.stdout().lock(),
12181221
"{}\nactive because: {}\ncompiler: {}\npath: {}",
12191222
toolchain.name(),
1220-
reason,
1223+
source.to_reason(),
12211224
toolchain.rustc_version(),
12221225
toolchain.path().display()
12231226
)?;
@@ -1226,9 +1229,9 @@ async fn show_active_toolchain(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::Ex
12261229
cfg.process.stdout().lock(),
12271230
"{} ({})",
12281231
toolchain.name(),
1229-
match reason {
1230-
ActiveReason::Default => &"default" as &dyn fmt::Display,
1231-
_ => &reason,
1232+
match source {
1233+
ActiveSource::Default => &"default" as &dyn fmt::Display,
1234+
_ => &source.to_reason(),
12321235
}
12331236
)?;
12341237
}

src/config.rs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)