Skip to content

Commit 0fbb111

Browse files
committed
Simplify process access to environment variables
1 parent e46516f commit 0fbb111

File tree

21 files changed

+39
-98
lines changed

21 files changed

+39
-98
lines changed

src/bin/rustup-init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustup::cli::rustup_mode;
2626
#[cfg(windows)]
2727
use rustup::cli::self_update;
2828
use rustup::cli::setup_mode;
29-
use rustup::currentprocess::{process, varsource::VarSource, with, OSProcess};
29+
use rustup::currentprocess::{process, with, OSProcess};
3030
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
3131
use rustup::is_proxyable_tools;
3232
use rustup::utils::utils;

src/cli/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use once_cell::sync::Lazy;
1414

1515
use super::self_update;
1616
use crate::cli::download_tracker::DownloadTracker;
17-
use crate::currentprocess::{terminalsource, varsource::VarSource};
17+
use crate::currentprocess::terminalsource;
1818
use crate::dist::dist::{TargetTriple, ToolchainDesc};
1919
use crate::install::UpdateStatus;
2020
use crate::utils::notifications as util_notifications;

src/cli/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22
use std::io::Write;
33

4-
use crate::currentprocess::{process, terminalsource, varsource::VarSource};
4+
use crate::currentprocess::{process, terminalsource};
55

66
macro_rules! warn {
77
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::warn_fmt ( format_args ! ( $ ( $ arg ) * ) ) )

src/cli/rustup_mode.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,8 +1622,6 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
16221622

16231623
#[cfg(not(windows))]
16241624
fn man(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1625-
use crate::currentprocess::varsource::VarSource;
1626-
16271625
let command = m.get_one::<String>("command").unwrap();
16281626

16291627
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;

src/cli/self_update.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ use crate::{
6565
errors::*,
6666
markdown::md,
6767
},
68-
currentprocess::varsource::VarSource,
6968
dist::dist::{self, PartialToolchainDesc, Profile, TargetTriple, ToolchainDesc},
7069
install::UpdateStatus,
7170
process,

src/cli/self_update/shell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::path::PathBuf;
2929
use anyhow::{bail, Result};
3030

3131
use super::utils;
32-
use crate::{currentprocess::varsource::VarSource, process};
32+
use crate::process;
3333

3434
pub(crate) type Shell = Box<dyn UnixShell>;
3535

src/cli/self_update/unix.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use anyhow::{bail, Context, Result};
55

66
use super::install_bins;
77
use super::shell;
8-
use crate::currentprocess::varsource::VarSource;
98
use crate::process;
109
use crate::utils::utils;
1110
use crate::utils::Notification;

src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use thiserror::Error as ThisError;
1212

1313
use crate::{
1414
cli::self_update::SelfUpdateMode,
15-
currentprocess::varsource::VarSource,
1615
dist::{
1716
dist::{self, PartialToolchainDesc, Profile, ToolchainDesc},
1817
download::DownloadCfg,

src/currentprocess.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ pub mod cwdsource;
2222
pub mod filesource;
2323
mod homethunk;
2424
pub mod terminalsource;
25-
pub mod varsource;
2625

2726
use cwdsource::*;
28-
use varsource::*;
2927

3028
use crate::utils::tty::{stderr_isatty, stdout_isatty};
3129

@@ -64,11 +62,11 @@ use crate::utils::tty::{stderr_isatty, stdout_isatty};
6462
/// methods are in performance critical loops (except perhaps progress bars -
6563
/// and even there we should be doing debouncing and managing update rates).
6664
#[enum_dispatch]
67-
pub trait CurrentProcess: CurrentDirSource + VarSource + Debug {}
65+
pub trait CurrentProcess: CurrentDirSource + Debug {}
6866

6967
/// Allows concrete types for the currentprocess abstraction.
7068
#[derive(Clone, Debug)]
71-
#[enum_dispatch(CurrentProcess, CurrentDirSource, VarSource)]
69+
#[enum_dispatch(CurrentProcess, CurrentDirSource)]
7270
pub enum Process {
7371
OSProcess(OSProcess),
7472
#[cfg(feature = "test")]
@@ -89,6 +87,25 @@ impl Process {
8987
.map(String::from)
9088
}
9189

90+
pub fn var(&self, key: &str) -> Result<String, env::VarError> {
91+
match self {
92+
Process::OSProcess(_) => env::var(key),
93+
#[cfg(feature = "test")]
94+
Process::TestProcess(p) => match p.vars.get(key) {
95+
Some(val) => Ok(val.to_owned()),
96+
None => Err(env::VarError::NotPresent),
97+
},
98+
}
99+
}
100+
101+
pub(crate) fn var_os(&self, key: &str) -> Option<OsString> {
102+
match self {
103+
Process::OSProcess(_) => env::var_os(key),
104+
#[cfg(feature = "test")]
105+
Process::TestProcess(p) => p.vars.get(key).map(OsString::from),
106+
}
107+
}
108+
92109
pub(crate) fn args(&self) -> Box<dyn Iterator<Item = String> + '_> {
93110
match self {
94111
Process::OSProcess(_) => Box::new(env::args()),

src/currentprocess/homethunk.rs

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,30 @@ use std::path::PathBuf;
55

66
use home::env as home;
77

8-
use super::OSProcess;
9-
use super::Process;
10-
#[cfg(feature = "test")]
11-
use super::{CurrentDirSource, TestProcess, VarSource};
8+
use super::{CurrentDirSource, Process};
129

1310
impl home::Env for Process {
1411
fn home_dir(&self) -> Option<PathBuf> {
1512
match self {
16-
Process::OSProcess(p) => p.home_dir(),
13+
Process::OSProcess(_) => self.var("HOME").ok().map(|v| v.into()),
1714
#[cfg(feature = "test")]
18-
Process::TestProcess(p) => p.home_dir(),
15+
Process::TestProcess(_) => home::OS_ENV.home_dir(),
1916
}
2017
}
18+
2119
fn current_dir(&self) -> Result<PathBuf, io::Error> {
2220
match self {
23-
Process::OSProcess(p) => home::Env::current_dir(p),
21+
Process::OSProcess(_) => CurrentDirSource::current_dir(self),
2422
#[cfg(feature = "test")]
25-
Process::TestProcess(p) => home::Env::current_dir(p),
23+
Process::TestProcess(_) => home::OS_ENV.current_dir(),
2624
}
2725
}
26+
2827
fn var_os(&self, key: &str) -> Option<OsString> {
2928
match self {
30-
Process::OSProcess(p) => home::Env::var_os(p, key),
29+
Process::OSProcess(_) => self.var_os(key),
3130
#[cfg(feature = "test")]
32-
Process::TestProcess(p) => home::Env::var_os(p, key),
31+
Process::TestProcess(_) => self.var_os(key),
3332
}
3433
}
3534
}
36-
37-
#[cfg(feature = "test")]
38-
impl home::Env for TestProcess {
39-
fn home_dir(&self) -> Option<PathBuf> {
40-
self.var("HOME").ok().map(|v| v.into())
41-
}
42-
fn current_dir(&self) -> Result<PathBuf, io::Error> {
43-
CurrentDirSource::current_dir(self)
44-
}
45-
fn var_os(&self, key: &str) -> Option<OsString> {
46-
VarSource::var_os(self, key)
47-
}
48-
}
49-
50-
impl home::Env for OSProcess {
51-
fn home_dir(&self) -> Option<PathBuf> {
52-
home::OS_ENV.home_dir()
53-
}
54-
fn current_dir(&self) -> Result<PathBuf, io::Error> {
55-
home::OS_ENV.current_dir()
56-
}
57-
fn var_os(&self, key: &str) -> Option<OsString> {
58-
home::OS_ENV.var_os(key)
59-
}
60-
}

0 commit comments

Comments
 (0)