diff --git a/src/config.rs b/src/config.rs index 8551c894db..c64b9c3695 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,8 @@ -use std::borrow::Cow; use std::fmt::{self, Debug, Display}; -use std::io; use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::Arc; +use std::{env, io}; use anyhow::{anyhow, bail, Context, Result}; use serde::Deserialize; @@ -288,30 +287,24 @@ impl<'a> Cfg<'a> { let default_host_triple = settings_file.with(|s| Ok(get_default_host_triple(s, process)))?; // Environment override - let env_override = process - .var("RUSTUP_TOOLCHAIN") - .ok() - .and_then(utils::if_not_empty) + let env_override = non_empty_env_var("RUSTUP_TOOLCHAIN", process)? .map(ResolvableLocalToolchainName::try_from) .transpose()? .map(|t| t.resolve(&default_host_triple)) .transpose()?; - let dist_root_server = match process.var("RUSTUP_DIST_SERVER") { - Ok(s) if !s.is_empty() => { + let dist_root_server = match non_empty_env_var("RUSTUP_DIST_SERVER", process)? { + Some(s) => { trace!("`RUSTUP_DIST_SERVER` has been set to `{s}`"); s } - _ => { + None => { // For backward compatibility - process - .var("RUSTUP_DIST_ROOT") - .ok() - .and_then(utils::if_not_empty) + non_empty_env_var("RUSTUP_DIST_ROOT", process)? .inspect(|url| trace!("`RUSTUP_DIST_ROOT` has been set to `{url}`")) - .map_or(Cow::Borrowed(dist::DEFAULT_DIST_ROOT), Cow::Owned) .as_ref() - .trim_end_matches("/dist") + .map(|root| root.trim_end_matches("/dist")) + .unwrap_or(dist::DEFAULT_DIST_SERVER) .to_owned() } }; @@ -1000,6 +993,15 @@ fn get_default_host_triple(s: &Settings, process: &Process) -> dist::TargetTripl .unwrap_or_else(|| dist::TargetTriple::from_host_or_build(process)) } +fn non_empty_env_var(name: &str, process: &Process) -> anyhow::Result> { + match process.var(name) { + Ok(s) if !s.is_empty() => Ok(Some(s)), + Ok(_) => Ok(None), + Err(env::VarError::NotPresent) => Ok(None), + Err(err) => Err(err.into()), + } +} + fn no_toolchain_error(process: &Process) -> anyhow::Error { RustupError::ToolchainNotSelected(process.name().unwrap_or_else(|| "Rust".into())).into() } diff --git a/src/dist/mod.rs b/src/dist/mod.rs index 17d5542c16..239d5227a4 100644 --- a/src/dist/mod.rs +++ b/src/dist/mod.rs @@ -41,9 +41,6 @@ pub(crate) use triple::*; pub static DEFAULT_DIST_SERVER: &str = "https://static.rust-lang.org"; -// Deprecated -pub(crate) static DEFAULT_DIST_ROOT: &str = "https://static.rust-lang.org/dist"; - const TOOLSTATE_MSG: &str = "If you require these components, please install and use the latest successful build version,\n\ which you can find at .\n\nAfter determining \ diff --git a/src/utils/raw.rs b/src/utils/raw.rs index c93296784c..aea44d4bea 100644 --- a/src/utils/raw.rs +++ b/src/utils/raw.rs @@ -65,14 +65,6 @@ pub(crate) fn random_string(length: usize) -> String { .collect() } -pub(crate) fn if_not_empty>(s: S) -> Option { - if s == *"" { - None - } else { - Some(s) - } -} - pub fn write_file(path: &Path, contents: &str) -> io::Result<()> { let mut file = fs::OpenOptions::new() .write(true) diff --git a/src/utils/utils.rs b/src/utils/utils.rs index a4b974f575..293e1e1b08 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -17,7 +17,7 @@ use crate::utils::raw; #[cfg(not(windows))] pub(crate) use crate::utils::utils::raw::find_cmd; -pub(crate) use crate::utils::utils::raw::{if_not_empty, is_directory}; +pub(crate) use crate::utils::utils::raw::is_directory; pub use crate::utils::utils::raw::{is_file, path_exists};