Skip to content

Small refactoring tweaks to improve further changes #3872

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 4 commits into from
Jun 13, 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
2 changes: 2 additions & 0 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ pub(crate) async fn install(
do_write_env_files()?;

if !opts.no_modify_path {
#[cfg(windows)]
do_add_to_programs()?;
do_add_to_path()?;
}
Expand Down Expand Up @@ -1022,6 +1023,7 @@ pub(crate) fn uninstall(no_prompt: bool) -> Result<utils::ExitCode> {

// Remove CARGO_HOME/bin from PATH
do_remove_from_path()?;
#[cfg(windows)]
do_remove_from_programs()?;

// Delete everything in CARGO_HOME *except* the rustup bin
Expand Down
8 changes: 0 additions & 8 deletions src/cli/self_update/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ pub(crate) fn do_write_env_files() -> Result<()> {
Ok(())
}

pub(crate) fn do_add_to_programs() -> Result<()> {
Ok(())
}

pub(crate) fn do_remove_from_programs() -> Result<()> {
Ok(())
}

/// Tell the upgrader to replace the rustup bins, then delete
/// itself.
pub(crate) fn run_update(setup_path: &Path) -> Result<utils::ExitCode> {
Expand Down
10 changes: 6 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ impl OverrideCfg {
path.display()
)
}
(None, None) => cfg
.get_default()?
.ok_or(RustupError::ToolchainNotSelected)?,
(None, None) => cfg.get_default()?.ok_or_else(no_toolchain_error)?,
};
Ok(match toolchain_name {
ToolchainName::Official(desc) => {
Expand Down Expand Up @@ -692,7 +690,7 @@ impl Cfg {
) -> Result<(Toolchain<'_>, ActiveReason)> {
self.maybe_find_or_install_active_toolchain(&self.current_dir)
.await?
.ok_or(RustupError::ToolchainNotSelected.into())
.ok_or_else(no_toolchain_error)
}

#[cfg_attr(feature = "otel", tracing::instrument(skip_all))]
Expand Down Expand Up @@ -957,6 +955,10 @@ fn get_default_host_triple(s: &Settings) -> dist::TargetTriple {
.unwrap_or_else(dist::TargetTriple::from_host_or_build)
}

fn no_toolchain_error() -> anyhow::Error {
RustupError::ToolchainNotSelected(process().name().unwrap_or_else(|| "Rust".into())).into()
}

/// Specifies how a `rust-toolchain`/`rust-toolchain.toml` configuration file should be parsed.
enum ParseMode {
/// Only permit TOML format in a configuration file.
Expand Down
5 changes: 0 additions & 5 deletions src/currentprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ impl From<TestProcess> for Process {

/// Obtain the current instance of CurrentProcess
pub fn process() -> Process {
home_process()
}

/// Obtain the current instance of HomeProcess
pub(crate) fn home_process() -> Process {
match PROCESS.with(|p| p.borrow().clone()) {
None => panic!("No process instance"),
Some(p) => p,
Expand Down
2 changes: 1 addition & 1 deletion src/dist/component/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl Component {
let temp = tx.temp().new_file()?;
utils::filter_file("components", &abs_path, &temp, |l| l != self.name)?;
tx.modify_file(path)?;
utils::rename_file("components", &temp, &abs_path, tx.notify_handler())?;
utils::rename("components", &temp, &abs_path, tx.notify_handler())?;

// TODO: If this is the last component remove the components file
// and the version file.
Expand Down
12 changes: 6 additions & 6 deletions src/dist/component/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ impl<'a> ChangedItem<'a> {
AddedFile(path) => utils::remove_file("component", &prefix.abs_path(path))?,
AddedDir(path) => utils::remove_dir("component", &prefix.abs_path(path), notify)?,
RemovedFile(path, tmp) | ModifiedFile(path, Some(tmp)) => {
utils::rename_file("component", tmp, &prefix.abs_path(path), notify)?
utils::rename("component", tmp, &prefix.abs_path(path), notify)?
}
RemovedDir(path, tmp) => {
utils::rename_dir("component", &tmp.join("bk"), &prefix.abs_path(path), notify)?
utils::rename("component", &tmp.join("bk"), &prefix.abs_path(path), notify)?
}
ModifiedFile(path, None) => {
let abs_path = prefix.abs_path(path);
Expand Down Expand Up @@ -292,7 +292,7 @@ impl<'a> ChangedItem<'a> {
}
.into())
} else {
utils::rename_file("component", &abs_path, &backup, notify)?;
utils::rename("component", &abs_path, &backup, notify)?;
Ok(ChangedItem::RemovedFile(relpath, backup))
}
}
Expand All @@ -312,7 +312,7 @@ impl<'a> ChangedItem<'a> {
}
.into())
} else {
utils::rename_dir("component", &abs_path, &backup.join("bk"), notify)?;
utils::rename("component", &abs_path, &backup.join("bk"), notify)?;
Ok(ChangedItem::RemovedDir(relpath, backup))
}
}
Expand Down Expand Up @@ -342,7 +342,7 @@ impl<'a> ChangedItem<'a> {
notify: &'a dyn Fn(Notification<'_>),
) -> Result<Self> {
let abs_path = ChangedItem::dest_abs_path(prefix, component, &relpath)?;
utils::rename_file("component", src, &abs_path, notify)?;
utils::rename("component", src, &abs_path, notify)?;
Ok(ChangedItem::AddedFile(relpath))
}
fn move_dir(
Expand All @@ -353,7 +353,7 @@ impl<'a> ChangedItem<'a> {
notify: &'a dyn Fn(Notification<'_>),
) -> Result<Self> {
let abs_path = ChangedItem::dest_abs_path(prefix, component, &relpath)?;
utils::rename_dir("component", src, &abs_path, notify)?;
utils::rename("component", src, &abs_path, notify)?;
Ok(ChangedItem::AddedDir(relpath))
}
}
2 changes: 1 addition & 1 deletion src/dist/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<'a> DownloadCfg<'a> {
} else {
(self.notify_handler)(Notification::ChecksumValid(url.as_ref()));

utils::rename_file(
utils::rename(
"downloaded",
&partial_file_path,
&target_file,
Expand Down
10 changes: 3 additions & 7 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use std::path::PathBuf;
use thiserror::Error as ThisError;
use url::Url;

use crate::{
currentprocess::process,
dist::dist::{TargetTriple, ToolchainDesc},
};
use crate::dist::dist::{TargetTriple, ToolchainDesc};
use crate::{
dist::manifest::{Component, Manifest},
toolchain::names::{PathBasedToolchainName, ToolchainName},
Expand Down Expand Up @@ -92,11 +89,10 @@ pub enum RustupError {
#[error("path '{0}' not found")]
PathToolchainNotInstalled(PathBasedToolchainName),
#[error(
"rustup could not choose a version of {} to run, because one wasn't specified explicitly, and no default is configured.\n{}",
process().name().unwrap_or_else(|| "Rust".into()),
"rustup could not choose a version of {0} to run, because one wasn't specified explicitly, and no default is configured.\n{}",
"help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain."
)]
ToolchainNotSelected,
ToolchainNotSelected(String),
#[error("toolchain '{}' does not contain component {}{}{}", .desc, .component, suggest_message(.suggestion), if .component.contains("rust-std") {
format!("\nnote: not all platforms have the standard library pre-compiled: https://doc.rust-lang.org/nightly/rustc/platform-support.html{}",
if desc.channel == "nightly" { "\nhelp: consider using `cargo build -Z build-std` instead" } else { "" }
Expand Down
34 changes: 5 additions & 29 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use retry::{retry, OperationResult};
use sha2::Sha256;
use url::Url;

use crate::currentprocess::{home_process, process};
use crate::currentprocess::process;
use crate::errors::*;
use crate::utils::notifications::Notification;
use crate::utils::raw;
Expand Down Expand Up @@ -90,30 +90,6 @@ pub(crate) fn write_str(name: &'static str, file: &mut File, path: &Path, s: &st
})
}

pub fn rename_file<'a, N>(
name: &'static str,
src: &'a Path,
dest: &'a Path,
notify: &'a dyn Fn(N),
) -> Result<()>
where
N: From<Notification<'a>>,
{
rename(name, src, dest, notify)
}

pub(crate) fn rename_dir<'a, N>(
name: &'static str,
src: &'a Path,
dest: &'a Path,
notify: &'a dyn Fn(N),
) -> Result<()>
where
N: From<Notification<'a>>,
{
rename(name, src, dest, notify)
}

pub(crate) fn filter_file<F: FnMut(&str) -> bool>(
name: &'static str,
src: &Path,
Expand Down Expand Up @@ -496,15 +472,15 @@ pub fn current_exe() -> Result<PathBuf> {
}

pub(crate) fn home_dir() -> Option<PathBuf> {
home::home_dir_with_env(&home_process())
home::home_dir_with_env(&process())
}

pub(crate) fn cargo_home() -> Result<PathBuf> {
home::cargo_home_with_env(&home_process()).context("failed to determine cargo home")
home::cargo_home_with_env(&process()).context("failed to determine cargo home")
}

pub(crate) fn rustup_home() -> Result<PathBuf> {
home::rustup_home_with_env(&home_process()).context("failed to determine rustup home dir")
home::rustup_home_with_env(&process()).context("failed to determine rustup home dir")
}

pub(crate) fn format_path_for_display(path: &str) -> String {
Expand Down Expand Up @@ -541,7 +517,7 @@ where
}
}

fn rename<'a, N>(
pub fn rename<'a, N>(
name: &'static str,
src: &'a Path,
dest: &'a Path,
Expand Down
Loading