Skip to content

Clean up usage of if_not_empty() #3925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
}
};
Expand Down Expand Up @@ -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<Option<String>> {
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()
}
Expand Down
3 changes: 0 additions & 3 deletions src/dist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://rust-lang.github.io/rustup-components-history>.\n\nAfter determining \
Expand Down
8 changes: 0 additions & 8 deletions src/utils/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ pub(crate) fn random_string(length: usize) -> String {
.collect()
}

pub(crate) fn if_not_empty<S: PartialEq<str>>(s: S) -> Option<S> {
if s == *"" {
None
} else {
Some(s)
}
}

pub fn write_file(path: &Path, contents: &str) -> io::Result<()> {
let mut file = fs::OpenOptions::new()
.write(true)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
Loading