switchy_env 0.3.0

Switchy Environment Variables package
Documentation
  • Coverage
  • 100%
    42 out of 42 items documented4 out of 38 items with examples
  • Size
  • Source code size: 54.79 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 878.11 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 7s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • MoosicBox/MoosicBox
    37 2 109
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • BSteffaniak

switchy_env

Deterministic environment variable access for testing and simulation.

Features

  • Standard backend: switchy_env::standard::* reads real environment variables via std::env
  • Simulator backend: switchy_env::* uses a configurable environment with deterministic defaults when simulator is enabled (default)
  • Type Safety: Parse environment variables to specific types
  • Testing: Set/remove variables for testing scenarios

Usage

With default features, switchy_env::* resolves to the simulator backend.

use switchy_env::{var, var_or, var_parse, var_parse_or, var_parse_opt, var_exists, vars};

// Get environment variable
let database_url = var("DATABASE_URL")?;

// Get with default
let port = var_or("PORT", "8080");

// Parse to specific type
let timeout: u64 = var_parse("TIMEOUT")?;

// Parse with default
let max_connections: usize = var_parse_or("MAX_CONNECTIONS", 100);

// Parse optional variable (None if not set, Some(T) if parseable, Err if unparseable)
let debug_level: Option<u32> = var_parse_opt("DEBUG_LEVEL")?;

// Check if variable exists
if var_exists("FEATURE_FLAG") {
    // ...
}

// Get all environment variables
let all_vars = vars();

To force real environment access when both default features are enabled, use the standard module:

use switchy_env::standard::var;

let home = var("HOME")?;

Simulator Features

In simulator mode, you can control environment variables:

use switchy_env::simulator::{set_var, remove_var, clear, reset};

// Set variable for testing
set_var("TEST_VAR", "test_value");

// Remove variable
remove_var("TEST_VAR");

// Clear all variables
clear();

// Reset to defaults
reset();

Cargo Features

  • std (default): Enable real environment variable access
  • simulator (default): Enable deterministic simulation mode
  • fail-on-warnings: Treat warnings as errors

Core Types

  • EnvProvider: Trait for custom environment providers with methods for fetching, parsing, and enumerating variables
  • EnvError: Error enum returned by fallible operations (NotFound, InvalidValue, ParseError)
  • Result<T>: Convenience alias for std::result::Result<T, EnvError>