Skip to content

Commit 99536cf

Browse files
committed
ActiveReason -> ActiveSource
1 parent 85e2052 commit 99536cf

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
@@ -332,7 +332,7 @@ pub(crate) async fn list_toolchains(
332332
} else {
333333
let default_toolchain_name = cfg.get_default()?;
334334
let active_toolchain_name: Option<ToolchainName> =
335-
if let Ok(Some((LocalToolchainName::Named(toolchain), _reason))) =
335+
if let Ok(Some((LocalToolchainName::Named(toolchain), _source))) =
336336
cfg.maybe_ensure_active_toolchain(None).await
337337
{
338338
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
@@ -31,7 +31,7 @@ use crate::{
3131
topical_doc,
3232
},
3333
command, component_for_bin,
34-
config::{ActiveReason, Cfg},
34+
config::{ActiveSource, Cfg},
3535
dist::{
3636
AutoInstallMode, PartialToolchainDesc, Profile, TargetTriple,
3737
manifest::{Component, ComponentStatus},
@@ -779,10 +779,13 @@ async fn default_(
779779
}
780780
};
781781

782-
if let Some((toolchain, reason)) = cfg.active_toolchain()?
783-
&& !matches!(reason, ActiveReason::Default)
782+
if let Some((toolchain, source)) = cfg.active_toolchain()?
783+
&& !matches!(source, ActiveSource::Default)
784784
{
785-
info!("note that the toolchain '{toolchain}' is currently in use ({reason})");
785+
info!(
786+
"note that the toolchain '{toolchain}' is currently in use ({})",
787+
source.to_reason()
788+
);
786789
}
787790
} else {
788791
let default_toolchain = cfg
@@ -997,9 +1000,9 @@ async fn update(
9971000
exit_code &= self_update::self_update(cfg.process).await?;
9981001
}
9991002
} else if ensure_active_toolchain {
1000-
let (toolchain, reason) = cfg.ensure_active_toolchain(force_non_host, true).await?;
1003+
let (toolchain, source) = cfg.ensure_active_toolchain(force_non_host, true).await?;
10011004
info!("the active toolchain `{toolchain}` has been installed");
1002-
info!("it's active because: {reason}");
1005+
info!("it's active because: {}", source.to_reason());
10031006
} else {
10041007
exit_code &= common::update_all_channels(cfg, opts.force).await?;
10051008
if self_update {
@@ -1088,16 +1091,16 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
10881091
}
10891092

10901093
let installed_toolchains = cfg.list_toolchains()?;
1091-
let active_toolchain_and_reason: Option<(ToolchainName, ActiveReason)> =
1092-
if let Ok(Some((LocalToolchainName::Named(toolchain_name), reason))) =
1094+
let active_toolchain_and_source: Option<(ToolchainName, ActiveSource)> =
1095+
if let Ok(Some((LocalToolchainName::Named(toolchain_name), source))) =
10931096
cfg.maybe_ensure_active_toolchain(None).await
10941097
{
1095-
Some((toolchain_name, reason))
1098+
Some((toolchain_name, source))
10961099
} else {
10971100
None
10981101
};
10991102

1100-
let (active_toolchain_name, _active_reason) = active_toolchain_and_reason
1103+
let (active_toolchain_name, _active_source) = active_toolchain_and_source
11011104
.as_ref()
11021105
.map(|atar| (&atar.0, &atar.1))
11031106
.unzip();
@@ -1161,15 +1164,15 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
11611164

11621165
print_header(&mut t, "active toolchain")?;
11631166

1164-
match active_toolchain_and_reason {
1165-
Some((active_toolchain_name, active_reason)) => {
1166-
let active_toolchain = Toolchain::with_reason(
1167+
match active_toolchain_and_source {
1168+
Some((active_toolchain_name, active_source)) => {
1169+
let active_toolchain = Toolchain::with_source(
11671170
cfg,
11681171
active_toolchain_name.clone().into(),
1169-
&active_reason,
1172+
&active_source,
11701173
)?;
11711174
writeln!(t.lock(), "name: {}", active_toolchain.name())?;
1172-
writeln!(t.lock(), "active because: {active_reason}")?;
1175+
writeln!(t.lock(), "active because: {}", active_source.to_reason())?;
11731176
if verbose {
11741177
writeln!(t.lock(), "compiler: {}", active_toolchain.rustc_version())?;
11751178
writeln!(t.lock(), "path: {}", active_toolchain.path().display())?;
@@ -1205,14 +1208,14 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
12051208
#[tracing::instrument(level = "trace", skip_all)]
12061209
async fn show_active_toolchain(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::ExitCode> {
12071210
match cfg.maybe_ensure_active_toolchain(None).await? {
1208-
Some((toolchain_name, reason)) => {
1209-
let toolchain = Toolchain::with_reason(cfg, toolchain_name.clone(), &reason)?;
1211+
Some((toolchain_name, source)) => {
1212+
let toolchain = Toolchain::with_source(cfg, toolchain_name.clone(), &source)?;
12101213
if verbose {
12111214
writeln!(
12121215
cfg.process.stdout().lock(),
12131216
"{}\nactive because: {}\ncompiler: {}\npath: {}",
12141217
toolchain.name(),
1215-
reason,
1218+
source.to_reason(),
12161219
toolchain.rustc_version(),
12171220
toolchain.path().display()
12181221
)?;
@@ -1221,9 +1224,9 @@ async fn show_active_toolchain(cfg: &Cfg<'_>, verbose: bool) -> Result<utils::Ex
12211224
cfg.process.stdout().lock(),
12221225
"{} ({})",
12231226
toolchain.name(),
1224-
match reason {
1225-
ActiveReason::Default => &"default" as &dyn fmt::Display,
1226-
_ => &reason,
1227+
match source {
1228+
ActiveSource::Default => &"default" as &dyn fmt::Display,
1229+
_ => &source.to_reason(),
12271230
}
12281231
)?;
12291232
}

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

@@ -518,7 +519,7 @@ impl<'a> Cfg<'a> {
518519
pub(crate) async fn maybe_ensure_active_toolchain(
519520
&self,
520521
force_ensure: Option<bool>,
521-
) -> Result<Option<(LocalToolchainName, ActiveReason)>> {
522+
) -> Result<Option<(LocalToolchainName, ActiveSource)>> {
522523
let should_ensure = if let Some(force) = force_ensure {
523524
force
524525
} else {
@@ -537,39 +538,39 @@ impl<'a> Cfg<'a> {
537538
}
538539
}
539540

540-
pub(crate) fn active_toolchain(&self) -> Result<Option<(LocalToolchainName, ActiveReason)>> {
541+
pub(crate) fn active_toolchain(&self) -> Result<Option<(LocalToolchainName, ActiveSource)>> {
541542
Ok(
542-
if let Some((override_config, reason)) = self.find_override_config()? {
543-
Some((override_config.into_local_toolchain_name(), reason))
543+
if let Some((override_config, source)) = self.find_override_config()? {
544+
Some((override_config.into_local_toolchain_name(), source))
544545
} else {
545546
self.get_default()?
546-
.map(|x| (x.into(), ActiveReason::Default))
547+
.map(|x| (x.into(), ActiveSource::Default))
547548
},
548549
)
549550
}
550551

551-
fn find_override_config(&self) -> Result<Option<(OverrideCfg, ActiveReason)>> {
552-
let override_config: Option<(OverrideCfg, ActiveReason)> =
552+
fn find_override_config(&self) -> Result<Option<(OverrideCfg, ActiveSource)>> {
553+
let override_config: Option<(OverrideCfg, ActiveSource)> =
553554
// First check +toolchain override from the command line
554555
if let Some(name) = &self.toolchain_override {
555556
let override_config = name.resolve(&self.get_default_host_triple()?)?.into();
556-
Some((override_config, ActiveReason::CommandLine))
557+
Some((override_config, ActiveSource::CommandLine))
557558
}
558559
// Then check the RUSTUP_TOOLCHAIN environment variable
559560
else if let Some(name) = &self.env_override {
560561
// Because path based toolchain files exist, this has to support
561562
// custom, distributable, and absolute path toolchains otherwise
562563
// rustup's export of a RUSTUP_TOOLCHAIN when running a process will
563564
// error when a nested rustup invocation occurs
564-
Some((name.clone().into(), ActiveReason::Environment))
565+
Some((name.clone().into(), ActiveSource::Environment))
565566
}
566567
// Then walk up the directory tree from 'path' looking for either the
567568
// directory in the override database, or a `rust-toolchain{.toml}` file,
568569
// in that order.
569-
else if let Some((override_cfg, active_reason)) = self.settings_file.with(|s| {
570+
else if let Some((override_cfg, active_source)) = self.settings_file.with(|s| {
570571
self.find_override_from_dir_walk(&self.current_dir, s)
571572
})? {
572-
Some((override_cfg, active_reason))
573+
Some((override_cfg, active_source))
573574
}
574575
// Otherwise, there is no override.
575576
else {
@@ -583,13 +584,13 @@ impl<'a> Cfg<'a> {
583584
&self,
584585
dir: &Path,
585586
settings: &Settings,
586-
) -> Result<Option<(OverrideCfg, ActiveReason)>> {
587+
) -> Result<Option<(OverrideCfg, ActiveSource)>> {
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'
@@ -683,9 +684,9 @@ impl<'a> Cfg<'a> {
683684
}
684685
}
685686

686-
let reason = ActiveReason::ToolchainFile(toolchain_file);
687+
let source = ActiveSource::ToolchainFile(toolchain_file);
687688
let override_cfg = OverrideCfg::from_file(self, override_file)?;
688-
return Ok(Some((override_cfg, reason)));
689+
return Ok(Some((override_cfg, source)));
689690
}
690691

691692
dir = d.parent();
@@ -779,8 +780,8 @@ impl<'a> Cfg<'a> {
779780
&self,
780781
force_non_host: bool,
781782
verbose: bool,
782-
) -> Result<(LocalToolchainName, ActiveReason)> {
783-
if let Some((override_config, reason)) = self.find_override_config()? {
783+
) -> Result<(LocalToolchainName, ActiveSource)> {
784+
if let Some((override_config, source)) = self.find_override_config()? {
784785
let toolchain = override_config.clone().into_local_toolchain_name();
785786
if let OverrideCfg::Official {
786787
toolchain,
@@ -799,18 +800,18 @@ impl<'a> Cfg<'a> {
799800
)
800801
.await?;
801802
} else {
802-
Toolchain::with_reason(self, toolchain.clone(), &reason)?;
803+
Toolchain::with_source(self, toolchain.clone(), &source)?;
803804
}
804-
Ok((toolchain, reason))
805+
Ok((toolchain, source))
805806
} else if let Some(toolchain) = self.get_default()? {
806-
let reason = ActiveReason::Default;
807+
let source = ActiveSource::Default;
807808
if let ToolchainName::Official(desc) = &toolchain {
808809
self.ensure_installed(desc, vec![], vec![], None, force_non_host, verbose)
809810
.await?;
810811
} else {
811-
Toolchain::with_reason(self, toolchain.clone().into(), &reason)?;
812+
Toolchain::with_source(self, toolchain.clone().into(), &source)?;
812813
}
813-
Ok((toolchain.into(), reason))
814+
Ok((toolchain.into(), source))
814815
} else {
815816
Err(no_toolchain_error(self.process))
816817
}

0 commit comments

Comments
 (0)