https://github.com/bnjjj/tmp_env
tmp_env is a crate which lets you create temporary environment and be automatically cleaned when not needed.
https://github.com/bnjjj/tmp_env
directory environment rust tmp
Last synced: 6 months ago
JSON representation
tmp_env is a crate which lets you create temporary environment and be automatically cleaned when not needed.
- Host: GitHub
- URL: https://github.com/bnjjj/tmp_env
- Owner: bnjjj
- License: mit
- Created: 2021-06-09T08:40:30.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-16T09:37:45.000Z (almost 5 years ago)
- Last Synced: 2025-03-01T11:22:33.608Z (about 1 year ago)
- Topics: directory, environment, rust, tmp
- Language: Rust
- Homepage:
- Size: 5.86 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tmp_env
[](https://crates.io/crates/tmp_env)
[](https://docs.rs/tmp_env)
tmp_env is a crate which lets you create temporary environment and be automatically cleaned when not needed.
For example sometimes you need to change the current directory or set environment variables to launch a process but you don't need this temporary environment for the rest of your program.
Then you will use `tmp_env` to create environment variable using `tmp_env::set_var` instead of `std::env::set_var` to get from `tmp_env::set_var` a datastructure which will automatically restore the
corresponding environment variable when dropped.
## Install
Put this dependency in your `Cargo.toml`
```toml
tmp_env = "0.1"
```
## Usage
- To temporary change the current directory:
```rust
{
let _tmp_current_dir = tmp_env::set_current_dir("src").expect("should set the new current_dir");
let current_dir = std::env::current_dir().expect("cannot get current dir from std env");
assert!(current_dir.ends_with("src"));
}
let current_dir = std::env::current_dir().expect("cannot get current dir from std env");
assert!(!current_dir.ends_with("src"));
```
- To temporary set an environment variable:
```rust
{
let _tmp_env = tmp_env::set_var("TEST_TMP_ENV", "myvalue");
assert_eq!(std::env::var("TEST_TMP_ENV"), Ok(String::from("myvalue")));
}
// Because guard `_tmp_env` is dropped then the environment variable is also automatically unset (not restored because no previous value was set)
assert!(std::env::var("TEST_TMP_ENV").is_err());
```
- To temporary create a directory
```rust
{
let tmp_dir = tmp_env::create_temp_dir().expect("cannot create temp dir"); // When tmp_dir is dropped this temporary dir will be removed
assert!(std::fs::metadata(&*tmp_dir).is_ok());
}
// The temporary directory is now removed
```