Skip to content

Commit e72d65a

Browse files
authored
Rename from to try_from (#75)
1 parent 75c8f06 commit e72d65a

File tree

20 files changed

+40
-37
lines changed

20 files changed

+40
-37
lines changed

crates/pet-conda/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl Locator for Conda {
152152
fn supported_categories(&self) -> Vec<PythonEnvironmentCategory> {
153153
vec![PythonEnvironmentCategory::Conda]
154154
}
155-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
155+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
156156
// Possible we do not have the prefix, but this exe is in the bin directory and its a conda env or root conda install.
157157
let mut prefix = env.prefix.clone();
158158
if prefix.is_none() {

crates/pet-conda/tests/ci_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn detect_conda_root_from_path() {
8282
let conda = Conda::from(&env);
8383

8484
let python_env = PythonEnv::new(exe, Some(conda_dir.clone()), None);
85-
let env = conda.from(&python_env).unwrap();
85+
let env = conda.try_from(&python_env).unwrap();
8686

8787
assert_eq!(env.manager.is_some(), true);
8888

@@ -182,7 +182,7 @@ fn detect_conda_env_from_path() {
182182
let conda = Conda::from(&env);
183183

184184
let python_env = PythonEnv::new(exe.clone(), Some(prefix.clone()), None);
185-
let env = conda.from(&python_env).unwrap();
185+
let env = conda.try_from(&python_env).unwrap();
186186

187187
assert_eq!(env.manager.is_some(), true);
188188

crates/pet-conda/tests/lib_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn find_conda_env_without_manager() {
1919
let path = resolve_test_path(&["unix", "conda_env_without_manager", "env_python_3"]);
2020

2121
let env = locator
22-
.from(&PythonEnv::new(
22+
.try_from(&PythonEnv::new(
2323
path.join("bin").join("python").into(),
2424
Some(path.clone().into()),
2525
None,
@@ -70,7 +70,7 @@ fn find_conda_env_without_manager_but_detect_manager_from_history() {
7070
fs::write(history_file, history_contents).unwrap();
7171

7272
let env = locator
73-
.from(&PythonEnv::new(
73+
.try_from(&PythonEnv::new(
7474
path.join("bin").join("python").into(),
7575
Some(path.clone().into()),
7676
None,

crates/pet-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub trait Locator: Send + Sync {
4747
///
4848
/// Note: The returned environment could have some missing information.
4949
/// This is because the `from` will do a best effort to get the environment information without spawning Python.
50-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment>;
50+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment>;
5151
/// Finds all environments specific to this locator.
5252
fn find(&self, reporter: &dyn Reporter);
5353
}

crates/pet-homebrew/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl Locator for Homebrew {
104104
fn supported_categories(&self) -> Vec<PythonEnvironmentCategory> {
105105
vec![PythonEnvironmentCategory::Homebrew]
106106
}
107-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
107+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
108108
from(env)
109109
}
110110

crates/pet-linux-global-python/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Locator for LinuxGlobalPython {
6262
vec![PythonEnvironmentCategory::LinuxGlobal]
6363
}
6464

65-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
65+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
6666
if std::env::consts::OS == "macos" || std::env::consts::OS == "windows" {
6767
return None;
6868
}

crates/pet-mac-commandlinetools/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Locator for MacCmdLineTools {
3535
vec![PythonEnvironmentCategory::MacCommandLineTools]
3636
}
3737

38-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
38+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
3939
if std::env::consts::OS != "macos" {
4040
return None;
4141
}
@@ -216,7 +216,7 @@ impl Locator for MacCmdLineTools {
216216
}
217217
}
218218
env.symlinks = Some(symlinks);
219-
if let Some(env) = self.from(&env) {
219+
if let Some(env) = self.try_from(&env) {
220220
_reporter.report_environment(&env);
221221
}
222222
}

crates/pet-mac-python-org/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Locator for MacPythonOrg {
3434
vec![PythonEnvironmentCategory::MacPythonOrg]
3535
}
3636

37-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
37+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
3838
if std::env::consts::OS != "macos" {
3939
return None;
4040
}
@@ -158,7 +158,8 @@ impl Locator for MacPythonOrg {
158158
let executable = prefix.join("bin").join("python3");
159159
let version = version::from_header_files(&prefix);
160160

161-
if let Some(env) = self.from(&PythonEnv::new(executable, Some(prefix), version)) {
161+
if let Some(env) = self.try_from(&PythonEnv::new(executable, Some(prefix), version))
162+
{
162163
reporter.report_environment(&env);
163164
}
164165
}

crates/pet-mac-xcode/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Locator for MacXCode {
3535
vec![PythonEnvironmentCategory::MacCommandLineTools]
3636
}
3737

38-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
38+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
3939
if std::env::consts::OS != "macos" {
4040
return None;
4141
}
@@ -204,7 +204,7 @@ impl Locator for MacXCode {
204204
// }
205205
// }
206206
// env.symlinks = Some(symlinks);
207-
// if let Some(env) = self.from(&env) {
207+
// if let Some(env) = self.try_from(&env) {
208208
// _reporter.report_environment(&env);
209209
// }
210210
// }

crates/pet-pipenv/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Locator for PipEnv {
7878
vec![PythonEnvironmentCategory::Pipenv]
7979
}
8080

81-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
81+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
8282
if !is_pipenv(env, &self.env_vars) {
8383
return None;
8484
}

crates/pet-poetry/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl Locator for Poetry {
157157
vec![PythonEnvironmentCategory::Poetry]
158158
}
159159

160-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
160+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
161161
if !is_virtualenv(env) {
162162
return None;
163163
}

crates/pet-pyenv/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Locator for PyEnv {
5858
]
5959
}
6060

61-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
61+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
6262
if let Some(prefix) = &env.prefix {
6363
if is_conda_env(prefix) {
6464
return None;

crates/pet-pyenv/tests/pyenv_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ fn resolve_pyenv_environment() {
436436
};
437437

438438
// Resolve regular Python installs in Pyenv
439-
let result = locator.from(&PythonEnv::new(
439+
let result = locator.try_from(&PythonEnv::new(
440440
resolve_test_path(&[home.to_str().unwrap(), ".pyenv/versions/3.9.9/bin/python"]),
441441
Some(resolve_test_path(&[
442442
home.to_str().unwrap(),
@@ -448,7 +448,7 @@ fn resolve_pyenv_environment() {
448448
assert_eq!(result.unwrap(), expected_3_9_9);
449449

450450
// Resolve regular virtual-envs in Pyenv
451-
let result = locator.from(&PythonEnv::new(
451+
let result = locator.try_from(&PythonEnv::new(
452452
resolve_test_path(&[
453453
home.to_str().unwrap(),
454454
".pyenv/versions/my-virtual-env/bin/python",
@@ -463,7 +463,7 @@ fn resolve_pyenv_environment() {
463463
assert_eq!(result.unwrap(), expected_virtual_env);
464464

465465
// Should not resolve conda envs in pyenv
466-
let result = locator.from(&PythonEnv::new(
466+
let result = locator.try_from(&PythonEnv::new(
467467
resolve_test_path(&[
468468
home.to_str().unwrap(),
469469
".pyenv/versions/anaconda-4.0.0/bin/python",
@@ -478,7 +478,7 @@ fn resolve_pyenv_environment() {
478478
assert_eq!(result.is_none(), true);
479479

480480
// Should not resolve conda envs using Conda Locator
481-
let result = conda.from(&PythonEnv::new(
481+
let result = conda.try_from(&PythonEnv::new(
482482
resolve_test_path(&[
483483
home.to_str().unwrap(),
484484
".pyenv/versions/anaconda-4.0.0/bin/python",

crates/pet-venv/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Locator for Venv {
4040
vec![PythonEnvironmentCategory::Venv]
4141
}
4242

43-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
43+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
4444
if is_venv(env) {
4545
let mut prefix = env.prefix.clone();
4646
if prefix.is_none() {

crates/pet-virtualenv/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Locator for VirtualEnv {
7878
vec![PythonEnvironmentCategory::VirtualEnv]
7979
}
8080

81-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
81+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
8282
if is_virtualenv(env) {
8383
let version = match env.version {
8484
Some(ref v) => Some(v.clone()),

crates/pet-virtualenvwrapper/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Locator for VirtualEnvWrapper {
3636
vec![PythonEnvironmentCategory::VirtualEnvWrapper]
3737
}
3838

39-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
39+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
4040
if !is_virtualenvwrapper(env, &self.env_vars) {
4141
return None;
4242
}

crates/pet-windows-registry/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Locator for WindowsRegistry {
6969
vec![PythonEnvironmentCategory::WindowsRegistry]
7070
}
7171

72-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
72+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
7373
// Assume we create a virtual env from a python install,
7474
// Then the exe in the virtual env bin will be a symlink to the homebrew python install.
7575
// Hence the first part of the condition will be true, but the second part will be false.

crates/pet-windows-store/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Locator for WindowsStore {
6969
}
7070

7171
#[cfg(windows)]
72-
fn from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
72+
fn try_from(&self, env: &PythonEnv) -> Option<PythonEnvironment> {
7373
use pet_virtualenv::is_virtualenv;
7474

7575
// Assume we create a virtual env from a python install,
@@ -91,7 +91,7 @@ impl Locator for WindowsStore {
9191
}
9292

9393
#[cfg(unix)]
94-
fn from(&self, _env: &PythonEnv) -> Option<PythonEnvironment> {
94+
fn try_from(&self, _env: &PythonEnv) -> Option<PythonEnvironment> {
9595
None
9696
}
9797

crates/pet/src/locators.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ pub fn identify_python_environment_using_locators(
8686
fallback_category: Option<PythonEnvironmentCategory>,
8787
) -> Option<PythonEnvironment> {
8888
let executable = env.executable.clone();
89-
if let Some(env) = locators
90-
.iter()
91-
.fold(None, |e, loc| if e.is_some() { e } else { loc.from(env) })
92-
{
89+
if let Some(env) = locators.iter().fold(
90+
None,
91+
|e, loc| if e.is_some() { e } else { loc.try_from(env) },
92+
) {
9393
return Some(env);
9494
}
9595

@@ -98,9 +98,11 @@ pub fn identify_python_environment_using_locators(
9898
// We try to get the interpreter info, hoping that the real exe returned might be identifiable.
9999
if let Some(resolved_env) = ResolvedPythonEnv::from(&executable) {
100100
let env = resolved_env.to_python_env();
101-
if let Some(env) = locators
102-
.iter()
103-
.fold(None, |e, loc| if e.is_some() { e } else { loc.from(&env) })
101+
if let Some(env) =
102+
locators.iter().fold(
103+
None,
104+
|e, loc| if e.is_some() { e } else { loc.try_from(&env) },
105+
)
104106
{
105107
trace!(
106108
"Unknown Env ({:?}) in Path resolved as {:?}",

crates/pet/tests/ci_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod common;
5151
/// by spawning the Python executable
5252
/// Verification 2:
5353
/// For each enviornment, given the executable verify we can get the exact same information
54-
/// Using the `locators.from` method (without having to find all environments).
54+
/// Using the `locator.try_from` method (without having to find all environments).
5555
/// I.e. we should be able to get the same information using only the executable.
5656
/// Verification 3:
5757
/// Similarly for each environment use one of the known symlinks and verify we can get the same information.
@@ -96,7 +96,7 @@ fn verify_validity_of_discovered_envs() {
9696
}));
9797
// Verification 2:
9898
// For each enviornment, given the executable verify we can get the exact same information
99-
// Using the `locators.from` method (without having to find all environments).
99+
// Using the `locator.try_from` method (without having to find all environments).
100100
// I.e. we should be able to get the same information using only the executable.
101101
//
102102
// Verification 3:
@@ -295,7 +295,7 @@ fn verify_we_can_get_same_env_inf_using_from_with_exe(
295295
) {
296296
let mut environment = environment.clone();
297297

298-
// Assume we were given a path to the exe, then we use the `locator.from` method.
298+
// Assume we were given a path to the exe, then we use the `locator.try_from` method.
299299
// We should be able to get the exct same information back given only the exe.
300300
//
301301
// Note: We will not not use the old locator objects, as we do not want any cached information.

0 commit comments

Comments
 (0)