Skip to content

Commit 6b2508e

Browse files
committed
Simplify process access to current_dir
1 parent 204c8a9 commit 6b2508e

File tree

4 files changed

+37
-67
lines changed

4 files changed

+37
-67
lines changed

src/currentprocess.rs

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

22-
pub mod cwdsource;
2322
pub mod filesource;
24-
mod homethunk;
2523
pub mod terminalsource;
2624

27-
use cwdsource::*;
28-
2925
/// An abstraction for the current process.
3026
///
3127
/// This acts as a clonable proxy to the global state provided by some key OS
@@ -61,11 +57,11 @@ use cwdsource::*;
6157
/// methods are in performance critical loops (except perhaps progress bars -
6258
/// and even there we should be doing debouncing and managing update rates).
6359
#[enum_dispatch]
64-
pub trait CurrentProcess: CurrentDirSource + Debug {}
60+
pub trait CurrentProcess: Debug {}
6561

6662
/// Allows concrete types for the currentprocess abstraction.
6763
#[derive(Clone, Debug)]
68-
#[enum_dispatch(CurrentProcess, CurrentDirSource)]
64+
#[enum_dispatch(CurrentProcess)]
6965
pub enum Process {
7066
OSProcess(OSProcess),
7167
#[cfg(feature = "test")]
@@ -145,6 +141,14 @@ impl Process {
145141
}
146142
}
147143

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

162+
impl home::env::Env for Process {
163+
fn home_dir(&self) -> Option<PathBuf> {
164+
match self {
165+
Process::OSProcess(_) => self.var("HOME").ok().map(|v| v.into()),
166+
#[cfg(feature = "test")]
167+
Process::TestProcess(_) => home::env::OS_ENV.home_dir(),
168+
}
169+
}
170+
171+
fn current_dir(&self) -> Result<PathBuf, io::Error> {
172+
match self {
173+
Process::OSProcess(_) => self.current_dir(),
174+
#[cfg(feature = "test")]
175+
Process::TestProcess(_) => home::env::OS_ENV.current_dir(),
176+
}
177+
}
178+
179+
fn var_os(&self, key: &str) -> Option<OsString> {
180+
match self {
181+
Process::OSProcess(_) => self.var_os(key),
182+
#[cfg(feature = "test")]
183+
Process::TestProcess(_) => self.var_os(key),
184+
}
185+
}
186+
}
187+
158188
/// Obtain the current instance of CurrentProcess
159189
pub fn process() -> Process {
160190
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use retry::{retry, OperationResult};
1111
use sha2::Sha256;
1212
use url::Url;
1313

14-
use crate::currentprocess::{cwdsource::CurrentDirSource, home_process, process};
14+
use crate::currentprocess::{home_process, process};
1515
use crate::errors::*;
1616
use crate::utils::notifications::Notification;
1717
use crate::utils::raw;

0 commit comments

Comments
 (0)