Skip to content

Commit 2b0b516

Browse files
committed
Replace non_empty_env_var() with Process::var_opt()
1 parent 63b4e21 commit 2b0b516

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

src/cli/self_update.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use crate::{
5656
errors::*,
5757
markdown::md,
5858
},
59-
config::{Cfg, non_empty_env_var},
59+
config::Cfg,
6060
dist::{self, PartialToolchainDesc, Profile, TargetTriple, ToolchainDesc},
6161
errors::RustupError,
6262
install::UpdateStatus,
@@ -1174,11 +1174,12 @@ pub(crate) async fn prepare_update(process: &Process) -> Result<Option<PathBuf>>
11741174

11751175
// Get available version
11761176
info!("checking for self-update (current version: {current_version})");
1177-
let available_version = if let Some(ver) = non_empty_env_var("RUSTUP_VERSION", process)? {
1178-
info!("`RUSTUP_VERSION` has been set to `{ver}`");
1179-
ver
1180-
} else {
1181-
get_available_rustup_version(process).await?
1177+
let available_version = match process.var_opt("RUSTUP_VERSION")? {
1178+
Some(ver) => {
1179+
info!("`RUSTUP_VERSION` has been set to `{ver}`");
1180+
ver
1181+
}
1182+
None => get_available_rustup_version(process).await?,
11821183
};
11831184

11841185
// If up-to-date

src/config.rs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::fmt::{self, Debug, Display};
2+
use std::io;
23
use std::path::{Path, PathBuf};
34
use std::str::FromStr;
45
use std::sync::Arc;
5-
use std::{env, io};
66

77
use anyhow::{Context, Result, anyhow, bail};
88
use serde::Deserialize;
@@ -282,11 +282,12 @@ impl<'a> Cfg<'a> {
282282
let default_host_triple =
283283
settings_file.with(|s| Ok(get_default_host_triple(s, process)))?;
284284
// Environment override
285-
let env_override = non_empty_env_var("RUSTUP_TOOLCHAIN", process)?
286-
.map(ResolvableLocalToolchainName::try_from)
287-
.transpose()?
288-
.map(|t| t.resolve(&default_host_triple))
289-
.transpose()?;
285+
let env_override = match process.var_opt("RUSTUP_TOOLCHAIN")? {
286+
Some(tc) => {
287+
Some(ResolvableLocalToolchainName::try_from(&tc)?.resolve(&default_host_triple)?)
288+
}
289+
None => None,
290+
};
290291

291292
let dist_root_server = dist_root_server(process)?;
292293

@@ -982,22 +983,21 @@ impl<'a> Cfg<'a> {
982983
/// The root path of the release server, without the `/dist` suffix.
983984
/// By default, it points to [`dist::DEFAULT_DIST_SERVER`].
984985
pub(crate) fn dist_root_server(process: &Process) -> Result<String> {
985-
Ok(
986-
if let Some(s) = non_empty_env_var("RUSTUP_DIST_SERVER", process)? {
987-
trace!("`RUSTUP_DIST_SERVER` has been set to `{s}`");
988-
s
989-
}
990-
// For backwards compatibility
991-
else if let Some(mut root) = non_empty_env_var("RUSTUP_DIST_ROOT", process)? {
992-
trace!("`RUSTUP_DIST_ROOT` has been set to `{root}`");
993-
if let Some(stripped) = root.strip_suffix("/dist") {
994-
root.truncate(stripped.len());
995-
}
996-
root
997-
} else {
998-
dist::DEFAULT_DIST_SERVER.to_owned()
999-
},
1000-
)
986+
if let Some(s) = process.var_opt("RUSTUP_DIST_SERVER")? {
987+
trace!("`RUSTUP_DIST_SERVER` has been set to `{s}`");
988+
return Ok(s);
989+
}
990+
991+
// For backwards compatibility
992+
let Some(mut root) = process.var_opt("RUSTUP_DIST_ROOT")? else {
993+
return Ok(dist::DEFAULT_DIST_SERVER.to_owned());
994+
};
995+
996+
trace!("`RUSTUP_DIST_ROOT` has been set to `{root}`");
997+
if let Some(stripped) = root.strip_suffix("/dist") {
998+
root.truncate(stripped.len());
999+
}
1000+
Ok(root)
10011001
}
10021002

10031003
impl Debug for Cfg<'_> {
@@ -1043,15 +1043,6 @@ fn get_default_host_triple(s: &Settings, process: &Process) -> TargetTriple {
10431043
.unwrap_or_else(|| TargetTriple::from_host_or_build(process))
10441044
}
10451045

1046-
pub(crate) fn non_empty_env_var(name: &str, process: &Process) -> anyhow::Result<Option<String>> {
1047-
match process.var(name) {
1048-
Ok(s) if !s.is_empty() => Ok(Some(s)),
1049-
Ok(_) => Ok(None),
1050-
Err(env::VarError::NotPresent) => Ok(None),
1051-
Err(err) => Err(err.into()),
1052-
}
1053-
}
1054-
10551046
fn no_toolchain_error(process: &Process) -> anyhow::Error {
10561047
RustupError::ToolchainNotSelected(process.name().unwrap_or_else(|| "Rust".into())).into()
10571048
}

src/process.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ impl Process {
8787
})
8888
}
8989

90+
pub fn var_opt(&self, key: &str) -> Result<Option<String>, env::VarError> {
91+
match self.var(key) {
92+
Ok(val) => Ok(Some(val)),
93+
Err(env::VarError::NotPresent) => Ok(None),
94+
Err(err) => Err(err),
95+
}
96+
}
97+
9098
pub fn var(&self, key: &str) -> Result<String, env::VarError> {
9199
let value = match self {
92100
Process::OsProcess(_) => env::var(key)?,

0 commit comments

Comments
 (0)