Giter Club home page Giter Club logo

tmp_env's Introduction

tmp_env

Version Documentation

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

tmp_env = "0.1"

Usage

  • To temporary change the current directory:
{
    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:
{
    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
{
    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

tmp_env's People

Contributors

bnjjj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.