pub struct EnvStore { /* private fields */ }
Expand description
A handle to either the real environment or a fake environment.
§Example
§Using the real environment
let env = EnvStore::real();
std::env::set_var("THE_ANSWER", "42");
assert_eq!(env.var("THE_ANSWER").unwrap(), "42");
§Making a fake environment
let env = EnvStore::fake();
env.set_var("THE_ANSWER", "42");
assert_eq!(env.var("THE_ANSWER").unwrap(), "42");
§Cloning
The Clone
implementation does the shallow copy, whereas the
EnvStore::to_fake
method does the deep copy.
let env = EnvStore::fake();
env.set_var("X", "42");
let env2 = env.to_fake();
env2.set_var("X", "53");
// The original env doesn't change
assert_eq!(env.var("X").unwrap(), "42");
let env3 = env.clone();
env3.set_var("X", "84");
// The original env changes as well
assert_eq!(env.var("X").unwrap(), "84");
§Default
The Default
implementation is equivalent to EnvStore::real
.
Implementations§
Source§impl EnvStore
impl EnvStore
Sourcepub fn set_audio_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_audio_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_audio_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_audio_dir(&self)
dirs
and fake
only.Sourcepub fn set_cache_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_cache_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_cache_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_cache_dir(&self)
dirs
and fake
only.Sourcepub fn config_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn config_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_config_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_config_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_config_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_config_dir(&self)
dirs
and fake
only.Sourcepub fn set_data_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_data_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_data_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_data_dir(&self)
dirs
and fake
only.Sourcepub fn data_local_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn data_local_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_data_local_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_data_local_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_data_local_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_data_local_dir(&self)
dirs
and fake
only.Sourcepub fn desktop_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn desktop_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_desktop_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_desktop_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_desktop_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_desktop_dir(&self)
dirs
and fake
only.Sourcepub fn document_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn document_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_document_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_document_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_document_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_document_dir(&self)
dirs
and fake
only.Sourcepub fn download_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn download_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_download_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_download_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_download_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_download_dir(&self)
dirs
and fake
only.Sourcepub fn executable_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn executable_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_executable_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_executable_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_executable_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_executable_dir(&self)
dirs
and fake
only.Sourcepub fn set_font_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_font_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_font_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_font_dir(&self)
dirs
and fake
only.Sourcepub fn set_home_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_home_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_home_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_home_dir(&self)
dirs
and fake
only.Sourcepub fn picture_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn picture_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_picture_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_picture_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_picture_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_picture_dir(&self)
dirs
and fake
only.Sourcepub fn public_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn public_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_public_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_public_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_public_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_public_dir(&self)
dirs
and fake
only.Sourcepub fn runtime_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn runtime_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_runtime_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_runtime_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_runtime_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_runtime_dir(&self)
dirs
and fake
only.Sourcepub fn template_dir(&self) -> Option<PathBuf>
Available on crate feature dirs
only.
pub fn template_dir(&self) -> Option<PathBuf>
dirs
only.Sourcepub fn set_template_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_template_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_template_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_template_dir(&self)
dirs
and fake
only.Sourcepub fn set_video_dir<P: AsRef<Path>>(&self, path: P)
Available on crate features dirs
and fake
only.
pub fn set_video_dir<P: AsRef<Path>>(&self, path: P)
dirs
and fake
only.Sourcepub fn unset_video_dir(&self)
Available on crate features dirs
and fake
only.
pub fn unset_video_dir(&self)
dirs
and fake
only.Source§impl EnvStore
impl EnvStore
Sourcepub const fn real() -> Self
pub const fn real() -> Self
Returns the handle to the real environment.
§Examples
let env = EnvStore::real();
std::env::set_var("THE_ANSWER", "42");
assert_eq!(env.var("THE_ANSWER").unwrap(), "42");
Sourcepub fn fake() -> Self
Available on crate feature fake
only.
pub fn fake() -> Self
fake
only.Creates a new fake environment from the real environment.
§Examples
let env = EnvStore::fake();
env.set_var("THE_ANSWER", "42");
assert_eq!(env.var("THE_ANSWER").unwrap(), "42");
Sourcepub fn to_fake(&self) -> Self
Available on crate feature fake
only.
pub fn to_fake(&self) -> Self
fake
only.Creates a new fake environment from the existing environment (real or fake).
§Examples
let env = EnvStore::fake();
env.set_var("THE_ANSWER", "42");
let env2 = env.to_fake();
env2.set_var("THE_ANSWER", "84");
assert_eq!(env2.var("THE_ANSWER").unwrap(), "84");
assert_eq!(env.var("THE_ANSWER").unwrap(), "42");
Sourcepub fn ptr_eq(&self, other: &Self) -> bool
pub fn ptr_eq(&self, other: &Self) -> bool
Returns whether the two handle points to the same environment.
Sourcepub fn var<K: AsRef<OsStr>>(&self, key: K) -> Result<String, VarError>
pub fn var<K: AsRef<OsStr>>(&self, key: K) -> Result<String, VarError>
Fetches the environment variable key
from the current process.
Corresponds with std::env::var
.
§Errors
- Environment variable is not present
- Environment variable is not valid unicode
§Panics
This function may panic if key
is empty, contains an ASCII equals
sign '='
or the NUL character '\0'
, or when the value contains the
NUL character.
§Examples
let env = EnvStore::real();
let key = "HOME";
match env.var(key) {
Ok(val) => println!("{}: {:?}", key, val),
Err(e) => println!("couldn't interpret {}: {}", key, e),
}
Sourcepub fn var_os<K: AsRef<OsStr>>(&self, key: K) -> Option<OsString>
pub fn var_os<K: AsRef<OsStr>>(&self, key: K) -> Option<OsString>
Fetches the environment variable key
from the current process,
returning None
if the variable isn’t set.
Corresponds with std::env::var_os
.
§Panics
This function may panic if key
is empty, contains an ASCII equals
sign '='
or the NUL character '\0'
, or when the value contains the
NUL character.
§Examples
let env = EnvStore::real();
let key = "HOME";
match env.var_os(key) {
Some(val) => println!("{}: {:?}", key, val),
None => println!("{} is not defined in the environment.", key)
}
Sourcepub fn vars(&self) -> Vars ⓘ
pub fn vars(&self) -> Vars ⓘ
Returns an iterator of (variable, value) pairs of strings, for all the
environment variables of the current process.
Corresponds with std::env::vars
.
The returned iterator contains a snapshot of the process’s environment variables at the time of this invocation. Modifications to environment variables afterwards will not be reflected in the returned iterator.
§Panics
While iterating, the returned iterator will panic if any key or value
in the environment is not valid unicode. If this is not desired,
consider using the EnvStore::vars_os
method.
§Examples
let env = EnvStore::real();
// We will iterate through the references to the element returned by
// env::vars();
for (key, value) in env.vars() {
println!("{}: {}", key, value);
}
Sourcepub fn vars_os(&self) -> VarsOs ⓘ
pub fn vars_os(&self) -> VarsOs ⓘ
Returns an iterator of (variable, value) pairs of OS strings, for all
the environment variables of the current process.
Corresponds with std::env::vars_os
.
The returned iterator contains a snapshot of the process’s environment variables at the time of this invocation. Modifications to environment variables afterwards will not be reflected in the returned iterator.
§Examples
let env = EnvStore::real();
// We will iterate through the references to the element returned by
// env.vars_os();
for (key, value) in env.vars_os() {
println!("{:?}: {:?}", key, value);
}
Sourcepub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(&self, k: K, v: V)
pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(&self, k: K, v: V)
Sets the environment variable k
to the value v
for the currently
running process.
Corresponds with std::env::set_var
.
Note that while concurrent access to environment variables is safe in Rust, some platforms only expose inherently unsafe non-threadsafe APIs for inspecting the environment. As a result, extra care needs to be taken when auditing calls to unsafe external FFI functions to ensure that any external environment accesses are properly synchronized with accesses in Rust.
Discussion of this unsafety on Unix may be found in:
§Panics
This function may panic if key
is empty, contains an ASCII equals
sign '='
or the NUL character '\0'
, or when the value contains the
NUL character.
§Examples
let env = EnvStore::real();
let key = "KEY";
env.set_var(key, "VALUE");
assert_eq!(env.var(key), Ok("VALUE".to_string()));
Sourcepub fn remove_var<K: AsRef<OsStr>>(&self, k: K)
pub fn remove_var<K: AsRef<OsStr>>(&self, k: K)
Removes an environment variable from the environment of the currently
running process.
Corresponds with std::env::remove_var
.
Note that while concurrent access to environment variables is safe in Rust, some platforms only expose inherently unsafe non-threadsafe APIs for inspecting the environment. As a result extra care needs to be taken when auditing calls to unsafe external FFI functions to ensure that any external environment accesses are properly synchronized with accesses in Rust.
Discussion of this unsafety on Unix may be found in:
§Panics
This function may panic if key
is empty, contains an ASCII equals
sign '='
or the NUL character '\0'
, or when the value contains the
NUL character.
§Examples
let env = EnvStore::real();
let key = "KEY";
env.set_var(key, "VALUE");
assert_eq!(env.var(key), Ok("VALUE".to_string()));
env.remove_var(key);
assert!(env.var(key).is_err());
Sourcepub fn current_dir(&self) -> Result<PathBuf>
pub fn current_dir(&self) -> Result<PathBuf>
Returns the current working directory as a PathBuf
.
Corresponds with std::env::current_dir
.
§Errors
Returns an Err
if the current working directory value is invalid.
Possible cases:
- Current directory does not exist.
- There are insufficient permissions to access the current directory.
§Examples
use fakeenv::EnvStore;
fn main() -> std::io::Result<()> {
let env = EnvStore::real();
let path = env.current_dir()?;
println!("The current directory is {}", path.display());
Ok(())
}
Sourcepub fn set_current_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn set_current_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
Changes the current working directory to the specified path.
Corresponds with std::env::set_current_dir
.
Returns an Err
if the operation fails.
§Examples
use fakeenv::EnvStore;
use std::path::Path;
let env = EnvStore::real();
let root = Path::new("/");
assert!(env.set_current_dir(&root).is_ok());
println!("Successfully changed working directory to {}!", root.display());
Trait Implementations§
impl RefUnwindSafe for EnvStore
impl UnwindSafe for EnvStore
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)