switchy_env
Deterministic environment variable access for testing and simulation.
Features
- Standard backend:
switchy_env::standard::*reads real environment variables viastd::env - Simulator backend:
switchy_env::*uses a configurable environment with deterministic defaults whensimulatoris 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 ;
// Get environment variable
let database_url = var?;
// Get with default
let port = var_or;
// Parse to specific type
let timeout: u64 = var_parse?;
// Parse with default
let max_connections: usize = var_parse_or;
// Parse optional variable (None if not set, Some(T) if parseable, Err if unparseable)
let debug_level: = var_parse_opt?;
// Check if variable exists
if var_exists
// Get all environment variables
let all_vars = vars;
To force real environment access when both default features are enabled, use the standard module:
use var;
let home = var?;
Simulator Features
In simulator mode, you can control environment variables:
use ;
// Set variable for testing
set_var;
// Remove variable
remove_var;
// Clear all variables
clear;
// Reset to defaults
reset;
Cargo Features
std(default): Enable real environment variable accesssimulator(default): Enable deterministic simulation modefail-on-warnings: Treat warnings as errors
Core Types
EnvProvider: Trait for custom environment providers with methods for fetching, parsing, and enumerating variablesEnvError: Error enum returned by fallible operations (NotFound,InvalidValue,ParseError)Result<T>: Convenience alias forstd::result::Result<T, EnvError>