Skip to content

Commit 44b01cc

Browse files
committed
return a PathBuf instead of String
1 parent 1b76b42 commit 44b01cc

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

crates/ra_env/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
55
use anyhow::{Error, Result};
66
use std::env;
7-
use std::path::Path;
7+
use std::path::{Path, PathBuf};
88
use std::process::Command;
99

10-
/// Return a `String` to use for the given executable.
10+
/// Return a `PathBuf` to use for the given executable.
1111
///
1212
/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that
1313
/// gives a valid Cargo executable; or it may return a full path to a valid
1414
/// Cargo.
15-
pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<String> {
15+
pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<PathBuf> {
1616
// The current implementation checks three places for an executable to use:
1717
// 1) Appropriate environment variable (erroring if this is set but not a usable executable)
1818
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
@@ -25,7 +25,7 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
2525
let env_var = executable_name.to_ascii_uppercase();
2626
if let Ok(path) = env::var(&env_var) {
2727
if is_valid_executable(&path) {
28-
Ok(path)
28+
Ok(path.into())
2929
} else {
3030
Err(Error::msg(format!(
3131
"`{}` environment variable points to something that's not a valid executable",
@@ -34,14 +34,14 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
3434
}
3535
} else {
3636
if is_valid_executable(executable_name) {
37-
return Ok(executable_name.to_owned());
37+
return Ok(executable_name.into());
3838
}
3939
if let Some(mut path) = dirs::home_dir() {
4040
path.push(".cargo");
4141
path.push("bin");
4242
path.push(executable_name);
4343
if is_valid_executable(&path) {
44-
return Ok(path.into_os_string().into_string().expect("Invalid Unicode in path"));
44+
return Ok(path);
4545
}
4646
}
4747
// This error message may also be caused by $PATH or $CARGO/$RUSTC/etc not being set correctly

crates/ra_project_model/src/sysroot.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ fn create_command_text(program: &str, args: &[&str]) -> String {
8989
format!("{} {}", program, args.join(" "))
9090
}
9191

92-
fn run_command_in_cargo_dir(cargo_toml: &Path, program: &str, args: &[&str]) -> Result<Output> {
92+
fn run_command_in_cargo_dir(cargo_toml: impl AsRef<Path>, program: impl AsRef<Path>, args: &[&str]) -> Result<Output> {
93+
let program = program.as_ref().as_os_str().to_str().expect("Invalid Unicode in path");
9394
let output = Command::new(program)
94-
.current_dir(cargo_toml.parent().unwrap())
95+
.current_dir(cargo_toml.as_ref().parent().unwrap())
9596
.args(args)
9697
.output()
9798
.context(format!("{} failed", create_command_text(program, args)))?;

0 commit comments

Comments
 (0)