Skip to content

Commit 14e3e6f

Browse files
committed
Simplify process access to current_dir
1 parent 0fbb111 commit 14e3e6f

File tree

4 files changed

+36
-67
lines changed

4 files changed

+36
-67
lines changed

src/currentprocess.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ use enum_dispatch::enum_dispatch;
1818
#[cfg(feature = "test")]
1919
use rand::{thread_rng, Rng};
2020

21-
pub mod cwdsource;
2221
pub mod filesource;
23-
mod homethunk;
2422
pub mod terminalsource;
2523

26-
use cwdsource::*;
27-
2824
use crate::utils::tty::{stderr_isatty, stdout_isatty};
2925

3026
/// An abstraction for the current process.
@@ -62,11 +58,11 @@ use crate::utils::tty::{stderr_isatty, stdout_isatty};
6258
/// methods are in performance critical loops (except perhaps progress bars -
6359
/// and even there we should be doing debouncing and managing update rates).
6460
#[enum_dispatch]
65-
pub trait CurrentProcess: CurrentDirSource + Debug {}
61+
pub trait CurrentProcess: Debug {}
6662

6763
/// Allows concrete types for the currentprocess abstraction.
6864
#[derive(Clone, Debug)]
69-
#[enum_dispatch(CurrentProcess, CurrentDirSource)]
65+
#[enum_dispatch(CurrentProcess)]
7066
pub enum Process {
7167
OSProcess(OSProcess),
7268
#[cfg(feature = "test")]
@@ -146,6 +142,14 @@ impl Process {
146142
}
147143
}
148144

145+
pub(crate) fn current_dir(&self) -> io::Result<PathBuf> {
146+
match self {
147+
Process::OSProcess(_) => env::current_dir(),
148+
#[cfg(feature = "test")]
149+
Process::TestProcess(p) => Ok(p.cwd.clone()),
150+
}
151+
}
152+
149153
#[cfg(test)]
150154
fn id(&self) -> u64 {
151155
match self {
@@ -156,6 +160,32 @@ impl Process {
156160
}
157161
}
158162

163+
impl home::env::Env for Process {
164+
fn home_dir(&self) -> Option<PathBuf> {
165+
match self {
166+
Process::OSProcess(_) => self.var("HOME").ok().map(|v| v.into()),
167+
#[cfg(feature = "test")]
168+
Process::TestProcess(_) => home::env::OS_ENV.home_dir(),
169+
}
170+
}
171+
172+
fn current_dir(&self) -> Result<PathBuf, io::Error> {
173+
match self {
174+
Process::OSProcess(_) => self.current_dir(),
175+
#[cfg(feature = "test")]
176+
Process::TestProcess(_) => home::env::OS_ENV.current_dir(),
177+
}
178+
}
179+
180+
fn var_os(&self, key: &str) -> Option<OsString> {
181+
match self {
182+
Process::OSProcess(_) => self.var_os(key),
183+
#[cfg(feature = "test")]
184+
Process::TestProcess(_) => self.var_os(key),
185+
}
186+
}
187+
}
188+
159189
/// Obtain the current instance of CurrentProcess
160190
pub fn process() -> Process {
161191
home_process()

src/currentprocess/cwdsource.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/currentprocess/homethunk.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/utils/utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use retry::{retry, OperationResult};
1010
use sha2::Sha256;
1111
use url::Url;
1212

13-
use crate::currentprocess::cwdsource::CurrentDirSource;
1413
use crate::errors::*;
1514
use crate::utils::notifications::Notification;
1615
use crate::utils::raw;

0 commit comments

Comments
 (0)