#env-var #environment #variables #testing #utility

env_swapper

A lightweight Rust library to temporarily set environment variables and automatically restore them when out of scope

3 releases

0.1.2 Nov 21, 2024
0.1.1 Oct 30, 2024
0.1.0 Oct 29, 2024

#1760 in Development tools

Download history 197/week @ 2024-10-25 61/week @ 2024-11-01 6/week @ 2024-11-08 111/week @ 2024-11-15 39/week @ 2024-11-22 2/week @ 2024-11-29 7/week @ 2024-12-06

159 downloads per month

MIT license

5KB

EnvSwapper

env_swapper is a lightweight Rust library to temporarily set environment variables, automatically restoring the previous state when out of scope.

Installation

Add env_swapper to your Cargo.toml:

[dependencies]
env_swapper = "0.1.0"

Usage

use env_swapper::EnvSwapper;
use std::env;

fn main() {
    let key = "EXAMPLE_VAR";
    
    {
        let _swapper = EnvSwapper::new(&[(key, "temporary_value")]);
        println!("Inside scope: {:?}", env::var(key)); // Outputs "temporary_value"
    }

    println!("Outside scope: {:?}", env::var(key)); // Restored to original
}

Features

Temporarily sets and restores environment variables automatically on scope exit. Ideal for testing, sandboxed configurations, and controlled environment changes.

License

This project is licensed under the MIT License.

Author

bensatlantik

No runtime deps