Skip to content

Commit 17dadb0

Browse files
authored
Use PathBufs to avoid regular conversions (#23393)
1 parent 9e3475c commit 17dadb0

File tree

14 files changed

+134
-151
lines changed

14 files changed

+134
-151
lines changed

native_locator/src/common_python.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ use crate::messaging;
66
use crate::utils;
77
use std::env;
88
use std::path::Path;
9+
use std::path::PathBuf;
910

10-
fn get_env_path(path: &str) -> Option<String> {
11+
fn get_env_path(path: &str) -> Option<PathBuf> {
1112
let path = Path::new(path);
1213
match path.parent() {
1314
Some(parent) => {
1415
if parent.file_name()? == "Scripts" {
15-
return Some(parent.parent()?.to_string_lossy().to_string());
16+
return Some(parent.parent()?.to_path_buf());
1617
} else {
17-
return Some(parent.to_string_lossy().to_string());
18+
return Some(parent.to_path_buf());
1819
}
1920
}
2021
None => None,
@@ -26,7 +27,7 @@ fn report_path_python(dispatcher: &mut impl messaging::MessageDispatcher, path:
2627
let env_path = get_env_path(path);
2728
dispatcher.report_environment(messaging::PythonEnvironment::new(
2829
None,
29-
Some(path.to_string()),
30+
Some(PathBuf::from(path)),
3031
messaging::PythonEnvironmentCategory::System,
3132
version,
3233
env_path.clone(),

native_locator/src/conda.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn get_conda_envs_from_environment_txt(environment: &impl known::Environment) ->
254254
}
255255

256256
fn get_known_env_locations(
257-
conda_bin: PathBuf,
257+
conda_bin: &PathBuf,
258258
environment: &impl known::Environment,
259259
) -> Vec<String> {
260260
let mut paths = vec![];
@@ -289,7 +289,7 @@ fn get_known_env_locations(
289289
}
290290

291291
fn get_conda_envs_from_known_env_locations(
292-
conda_bin: PathBuf,
292+
conda_bin: &PathBuf,
293293
environment: &impl known::Environment,
294294
) -> Vec<String> {
295295
let mut envs = vec![];
@@ -332,12 +332,11 @@ struct CondaEnv {
332332
}
333333

334334
fn get_distinct_conda_envs(
335-
conda_bin: PathBuf,
335+
conda_bin: &PathBuf,
336336
environment: &impl known::Environment,
337337
) -> Vec<CondaEnv> {
338338
let mut envs = get_conda_envs_from_environment_txt(environment);
339-
let mut known_envs =
340-
get_conda_envs_from_known_env_locations(conda_bin.to_path_buf(), environment);
339+
let mut known_envs = get_conda_envs_from_known_env_locations(conda_bin, environment);
341340
envs.append(&mut known_envs);
342341
envs.sort();
343342
envs.dedup();
@@ -386,26 +385,22 @@ pub fn find_and_report(
386385
match conda_binary {
387386
Some(conda_binary) => {
388387
let params = messaging::EnvManager::new(
389-
conda_binary.to_string_lossy().to_string(),
388+
conda_binary.clone(),
390389
get_conda_version(&conda_binary),
391390
EnvManagerType::Conda,
392391
);
393392
dispatcher.report_environment_manager(params);
394393

395-
let envs = get_distinct_conda_envs(conda_binary.to_path_buf(), environment);
394+
let envs = get_distinct_conda_envs(&conda_binary, environment);
396395
for env in envs {
397396
let executable = find_python_binary_path(Path::new(&env.path));
398-
let env_path = env.path.to_string_lossy().to_string();
399397
let params = messaging::PythonEnvironment::new(
400398
Some(env.name.to_string()),
401-
match executable {
402-
Some(executable) => Some(executable.to_string_lossy().to_string()),
403-
None => None,
404-
},
399+
executable,
405400
messaging::PythonEnvironmentCategory::Conda,
406401
get_conda_python_version(&env.path),
407-
Some(env_path.clone()),
408-
Some(env_path),
402+
Some(env.path.clone()),
403+
Some(env.path.clone()),
409404
None,
410405
if env.named {
411406
Some(vec![

native_locator/src/homebrew.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ pub fn find_and_report(
4545
continue;
4646
}
4747
reported.insert(exe.to_string_lossy().to_string());
48-
let executable = exe.to_string_lossy().to_string();
4948
let env = crate::messaging::PythonEnvironment::new(
5049
None,
51-
Some(executable.clone()),
50+
Some(exe.clone()),
5251
crate::messaging::PythonEnvironmentCategory::Homebrew,
5352
version,
5453
None,
5554
None,
5655
None,
57-
Some(vec![executable]),
56+
Some(vec![exe.to_string_lossy().to_string()]),
5857
);
5958
dispatcher.report_environment(env);
6059
}

native_locator/src/known.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{env, path::PathBuf};
44

55
pub trait Environment {
6-
fn get_user_home(&self) -> Option<String>;
6+
fn get_user_home(&self) -> Option<PathBuf>;
77
fn get_env_var(&self, key: String) -> Option<String>;
88
fn get_know_global_search_locations(&self) -> Vec<PathBuf>;
99
}
@@ -12,7 +12,7 @@ pub struct EnvironmentApi {}
1212

1313
#[cfg(windows)]
1414
impl Environment for EnvironmentApi {
15-
fn get_user_home(&self) -> Option<String> {
15+
fn get_user_home(&self) -> Option<PathBuf> {
1616
get_user_home()
1717
}
1818
fn get_env_var(&self, key: String) -> Option<String> {
@@ -25,7 +25,7 @@ impl Environment for EnvironmentApi {
2525

2626
#[cfg(unix)]
2727
impl Environment for EnvironmentApi {
28-
fn get_user_home(&self) -> Option<String> {
28+
fn get_user_home(&self) -> Option<PathBuf> {
2929
get_user_home()
3030
}
3131
fn get_env_var(&self, key: String) -> Option<String> {
@@ -49,10 +49,10 @@ impl Environment for EnvironmentApi {
4949
}
5050
}
5151

52-
fn get_user_home() -> Option<String> {
52+
fn get_user_home() -> Option<PathBuf> {
5353
let home = env::var("HOME").or_else(|_| env::var("USERPROFILE"));
5454
match home {
55-
Ok(home) => Some(home),
55+
Ok(home) => Some(PathBuf::from(home)),
5656
Err(_) => None,
5757
}
5858
}

native_locator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ fn main() {
5555
dispatcher.log_error(&format!("Error getting elapsed time: {:?}", e));
5656
}
5757
}
58-
58+
5959
dispatcher.exit();
6060
}

native_locator/src/messaging.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
use std::path::PathBuf;
5+
46
use crate::logging::{LogLevel, LogMessage};
57
use serde::{Deserialize, Serialize};
68

@@ -24,13 +26,13 @@ pub enum EnvManagerType {
2426
#[derive(Serialize, Deserialize)]
2527
#[serde(rename_all = "camelCase")]
2628
pub struct EnvManager {
27-
pub executable_path: String,
29+
pub executable_path: PathBuf,
2830
pub version: Option<String>,
2931
pub tool: EnvManagerType,
3032
}
3133

3234
impl EnvManager {
33-
pub fn new(executable_path: String, version: Option<String>, tool: EnvManagerType) -> Self {
35+
pub fn new(executable_path: PathBuf, version: Option<String>, tool: EnvManagerType) -> Self {
3436
Self {
3537
executable_path,
3638
version,
@@ -84,27 +86,27 @@ pub enum PythonEnvironmentCategory {
8486
#[serde(rename_all = "camelCase")]
8587
pub struct PythonEnvironment {
8688
pub name: Option<String>,
87-
pub python_executable_path: Option<String>,
89+
pub python_executable_path: Option<PathBuf>,
8890
pub category: PythonEnvironmentCategory,
8991
pub version: Option<String>,
90-
pub env_path: Option<String>,
91-
pub sys_prefix_path: Option<String>,
92+
pub env_path: Option<PathBuf>,
93+
pub sys_prefix_path: Option<PathBuf>,
9294
pub env_manager: Option<EnvManager>,
9395
pub python_run_command: Option<Vec<String>>,
9496
/**
9597
* The project path for the Pipenv environment.
9698
*/
97-
pub project_path: Option<String>,
99+
pub project_path: Option<PathBuf>,
98100
}
99101

100102
impl PythonEnvironment {
101103
pub fn new(
102104
name: Option<String>,
103-
python_executable_path: Option<String>,
105+
python_executable_path: Option<PathBuf>,
104106
category: PythonEnvironmentCategory,
105107
version: Option<String>,
106-
env_path: Option<String>,
107-
sys_prefix_path: Option<String>,
108+
env_path: Option<PathBuf>,
109+
sys_prefix_path: Option<PathBuf>,
108110
env_manager: Option<EnvManager>,
109111
python_run_command: Option<Vec<String>>,
110112
) -> Self {
@@ -121,12 +123,12 @@ impl PythonEnvironment {
121123
}
122124
}
123125
pub fn new_pipenv(
124-
python_executable_path: Option<String>,
126+
python_executable_path: Option<PathBuf>,
125127
version: Option<String>,
126-
env_path: Option<String>,
127-
sys_prefix_path: Option<String>,
128+
env_path: Option<PathBuf>,
129+
sys_prefix_path: Option<PathBuf>,
128130
env_manager: Option<EnvManager>,
129-
project_path: String,
131+
project_path: PathBuf,
130132
) -> Self {
131133
Self {
132134
name: None,
@@ -137,7 +139,7 @@ impl PythonEnvironment {
137139
sys_prefix_path,
138140
env_manager,
139141
python_run_command: match python_executable_path {
140-
Some(exe) => Some(vec![exe]),
142+
Some(exe) => Some(vec![exe.to_string_lossy().to_string()]),
141143
None => None,
142144
},
143145
project_path: Some(project_path),

native_locator/src/pipenv.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use crate::utils::PythonEnv;
66
use std::fs;
77
use std::path::PathBuf;
88

9-
fn get_pipenv_project(env: &PythonEnv) -> Option<String> {
9+
fn get_pipenv_project(env: &PythonEnv) -> Option<PathBuf> {
1010
let project_file = env.path.join(".project");
1111
if project_file.exists() {
1212
if let Ok(contents) = fs::read_to_string(project_file) {
1313
let project_folder = PathBuf::from(contents.trim().to_string());
1414
if project_folder.exists() {
15-
return Some(project_folder.to_string_lossy().to_string());
15+
return Some(project_folder);
1616
}
1717
}
1818
}
@@ -22,23 +22,11 @@ fn get_pipenv_project(env: &PythonEnv) -> Option<String> {
2222

2323
pub fn find_and_report(env: &PythonEnv, dispatcher: &mut impl MessageDispatcher) -> Option<()> {
2424
if let Some(project_path) = get_pipenv_project(env) {
25-
let env_path = env
26-
.path
27-
.clone()
28-
.into_os_string()
29-
.to_string_lossy()
30-
.to_string();
31-
let executable = env
32-
.executable
33-
.clone()
34-
.into_os_string()
35-
.to_string_lossy()
36-
.to_string();
3725
let env = PythonEnvironment::new_pipenv(
38-
Some(executable),
26+
Some(env.executable.clone()),
3927
env.version.clone(),
40-
Some(env_path.clone()),
41-
Some(env_path),
28+
Some(env.path.clone()),
29+
Some(env.path.clone()),
4230
None,
4331
project_path,
4432
);

0 commit comments

Comments
 (0)