Skip to content

Commit

Permalink
Change type to &str
Browse files Browse the repository at this point in the history
  • Loading branch information
InfyniteHeap committed Jun 21, 2024
1 parent 811c141 commit 5b6be5c
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/cli/self_update/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,11 @@ pub(crate) fn wait_for_parent() -> Result<()> {

pub(crate) fn do_add_to_path(process: &Process) -> Result<()> {
let new_path = _with_path_cargo_home_bin(_add_to_path, process)?;
_apply_new_path(new_path)?;
_apply_new_path(new_path.as_deref())?;
do_add_to_programs(process)
}

fn _apply_new_path(new_path: Option<String>) -> Result<()> {
fn _apply_new_path(new_path: Option<&str>) -> Result<()> {
use std::ptr;
use windows_sys::Win32::Foundation::*;
use windows_sys::Win32::UI::WindowsAndMessaging::{
Expand All @@ -478,7 +478,7 @@ fn _apply_new_path(new_path: Option<String>) -> Result<()> {
if new_path.is_empty() {
environment.remove_value("PATH")?;
} else {
environment.set_string("PATH", &new_path)?;
environment.set_string("PATH", new_path)?;
}

// Tell other processes to update their environment
Expand Down Expand Up @@ -515,22 +515,22 @@ fn get_windows_path_var() -> Result<Option<String>> {

// Returns None if the existing old_path does not need changing, otherwise
// prepends the path_str to old_path, handling empty old_path appropriately.
fn _add_to_path(old_path: String, path_str: String) -> Option<String> {
fn _add_to_path(old_path: &str, path_str: &str) -> Option<String> {
if old_path.is_empty() {
Some(path_str)
} else if old_path.contains(&path_str) {
Some(path_str.to_owned())
} else if old_path.contains(path_str) {
None
} else {
let mut new_path = path_str;
let mut new_path = path_str.to_owned();
new_path.push(';');
new_path += &old_path;
new_path += old_path;
Some(new_path)
}
}

// Returns None if the existing old_path does not need changing
fn _remove_from_path(old_path: String, path_str: String) -> Option<String> {
let idx = old_path.find(&path_str)?;
fn _remove_from_path(old_path: &str, path_str: &str) -> Option<String> {
let idx = old_path.find(path_str)?;
// If there's a trailing semicolon (likely, since we probably added one
// during install), include that in the substring to remove. We don't search
// for that to find the string, because if it's the last string in the path,
Expand All @@ -552,17 +552,18 @@ fn _remove_from_path(old_path: String, path_str: String) -> Option<String> {

fn _with_path_cargo_home_bin<F>(f: F, process: &Process) -> Result<Option<String>>
where
F: FnOnce(String, String) -> Option<String>,
F: FnOnce(&str, &str) -> Option<String>,
{
let windows_path = get_windows_path_var()?;
let path = get_windows_path_var()?;
let windows_path = path.as_deref();
let mut path_str = process.cargo_home()?;
path_str.push("bin");
Ok(windows_path.and_then(|old_path| f(old_path, path_str.to_string_lossy().to_string())))
Ok(windows_path.and_then(|old_path| f(old_path, &path_str.to_string_lossy())))
}

pub(crate) fn do_remove_from_path(process: &Process) -> Result<()> {
let new_path = _with_path_cargo_home_bin(_remove_from_path, process)?;
_apply_new_path(new_path)?;
_apply_new_path(new_path.as_deref())?;
do_remove_from_programs()
}

Expand Down Expand Up @@ -760,8 +761,8 @@ mod tests {
assert_eq!(
None,
super::_add_to_path(
r"c:\users\example\.cargo\bin;foo".to_string(),
r"c:\users\example\.cargo\bin".to_string()
r"c:\users\example\.cargo\bin;foo",
r"c:\users\example\.cargo\bin"
)
);
}
Expand Down Expand Up @@ -796,7 +797,7 @@ mod tests {
{
// Can't compare the Results as Eq isn't derived; thanks error-chain.
#![allow(clippy::unit_cmp)]
assert_eq!((), super::_apply_new_path(Some("foo".to_string())).unwrap());
assert_eq!((), super::_apply_new_path(Some("foo")).unwrap());
}
let environment = CURRENT_USER.create("Environment").unwrap();
let path = environment.get_string("PATH").unwrap();
Expand All @@ -815,7 +816,7 @@ mod tests {
{
// Can't compare the Results as Eq isn't derived; thanks error-chain.
#![allow(clippy::unit_cmp)]
assert_eq!((), super::_apply_new_path(Some(String::new())).unwrap());
assert_eq!((), super::_apply_new_path(Some("")).unwrap());
}
let reg_value = environment.get_string("PATH");
match reg_value {
Expand Down Expand Up @@ -868,8 +869,8 @@ mod tests {
assert_eq!(
"foo",
super::_remove_from_path(
r"c:\users\example\.cargo\bin;foo".to_string(),
r"c:\users\example\.cargo\bin".to_string(),
r"c:\users\example\.cargo\bin;foo",
r"c:\users\example\.cargo\bin",
)
.unwrap()
)
Expand All @@ -880,8 +881,8 @@ mod tests {
assert_eq!(
"foo",
super::_remove_from_path(
r"foo;c:\users\example\.cargo\bin".to_string(),
r"c:\users\example\.cargo\bin".to_string(),
r"foo;c:\users\example\.cargo\bin",
r"c:\users\example\.cargo\bin",
)
.unwrap()
)
Expand Down

0 comments on commit 5b6be5c

Please sign in to comment.