Skip to content

Commit b4f54d4

Browse files
committed
Simplify process access to current_dir
1 parent 1cc78e0 commit b4f54d4

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
/// An abstraction for the current process.
2925
///
3026
/// This acts as a clonable proxy to the global state provided by some key OS
@@ -60,11 +56,11 @@ use cwdsource::*;
6056
/// methods are in performance critical loops (except perhaps progress bars -
6157
/// and even there we should be doing debouncing and managing update rates).
6258
#[enum_dispatch]
63-
pub trait CurrentProcess: CurrentDirSource + Debug {}
59+
pub trait CurrentProcess: Debug {}
6460

6561
/// Allows concrete types for the currentprocess abstraction.
6662
#[derive(Clone, Debug)]
67-
#[enum_dispatch(CurrentProcess, CurrentDirSource)]
63+
#[enum_dispatch(CurrentProcess)]
6864
pub enum Process {
6965
OSProcess(OSProcess),
7066
#[cfg(feature = "test")]
@@ -144,6 +140,14 @@ impl Process {
144140
}
145141
}
146142

143+
pub(crate) fn current_dir(&self) -> io::Result<PathBuf> {
144+
match self {
145+
Process::OSProcess(_) => env::current_dir(),
146+
#[cfg(feature = "test")]
147+
Process::TestProcess(p) => Ok(p.cwd.clone()),
148+
}
149+
}
150+
147151
#[cfg(test)]
148152
fn id(&self) -> u64 {
149153
match self {
@@ -154,6 +158,32 @@ impl Process {
154158
}
155159
}
156160

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