Skip to content

Commit

Permalink
Abstract over home::
Browse files Browse the repository at this point in the history
The home crate also accesses global process state, so we need to
abstract over it as well.
  • Loading branch information
rbtcollins committed Jun 13, 2020
1 parent 2880f30 commit 0997247
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,6 @@ codegen-units = 1
# Reduce build time by setting proc-macro crates non optimized.
[profile.release.build-override]
opt-level = 0

[patch.crates-io]
home = { path = "../rustup-home" }
1 change: 1 addition & 0 deletions src/currentprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rand::{thread_rng, Rng};
pub mod argsource;
pub mod cwdsource;
pub mod filesource;
mod homethunk;
pub mod varsource;

use argsource::*;
Expand Down
21 changes: 21 additions & 0 deletions src/currentprocess/homethunk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// Adapts currentprocess to the trait home::Env
use std::ffi::OsString;
use std::io;
use std::ops::Deref;
use std::path::PathBuf;

use super::CurrentDirSource;
use super::CurrentProcess;
use super::VarSource;

impl home::Env for Box<dyn CurrentProcess + 'static> {
fn home_dir(&self) -> Option<PathBuf> {
self.var("HOME").ok().map(|v| v.into())
}
fn current_dir(&self) -> Result<PathBuf, io::Error> {
CurrentDirSource::current_dir(self.deref())
}
fn var_os(&self, key: &str) -> Option<OsString> {
VarSource::var_os(self.deref(), key)
}
}
6 changes: 3 additions & 3 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,11 @@ pub fn to_absolute<P: AsRef<Path>>(path: P) -> Result<PathBuf> {
}

pub fn home_dir() -> Option<PathBuf> {
home::home_dir()
home::home_dir_from(&process())
}

pub fn cargo_home() -> Result<PathBuf> {
home::cargo_home().map_err(|e| Error::from_kind(ErrorKind::Io(e)))
home::cargo_home_from(&process()).map_err(|e| Error::from_kind(ErrorKind::Io(e)))
}

// Creates a ~/.rustup folder
Expand All @@ -496,7 +496,7 @@ pub fn rustup_home_in_user_dir() -> Result<PathBuf> {
}

pub fn rustup_home() -> Result<PathBuf> {
home::rustup_home().map_err(|e| Error::from_kind(ErrorKind::Io(e)))
home::rustup_home_from(&process()).map_err(|e| Error::from_kind(ErrorKind::Io(e)))
}

pub fn format_path_for_display(path: &str) -> String {
Expand Down

0 comments on commit 0997247

Please sign in to comment.