Skip to main content

Crate switchy_env

Crate switchy_env 

Source
Expand description

Switchy environment variable access with pluggable backends.

This crate provides a unified interface for accessing environment variables with support for both standard system environment variables and a simulator mode for testing.

§Features

  • standard: Uses std::env for real environment variable access
  • simulator: Provides a configurable environment for testing with deterministic defaults

§Usage

With the standard feature (default), access environment variables:

use switchy_env::{var, var_parse};

// Get a variable as a string
let port_str = var("PORT").unwrap();

// Parse a variable as a specific type
let port: u16 = var_parse("PORT").unwrap();

With the simulator feature, configure variables for testing:

use switchy_env::{set_var, var, reset};

// Set a test variable
set_var("DATABASE_URL", "sqlite::memory:");

// Access it like normal
let db_url = var("DATABASE_URL").unwrap();

// Reset to defaults
reset();

§Custom Providers

Implement the EnvProvider trait to create custom environment variable sources:

use switchy_env::{EnvProvider, EnvError, Result};
use std::collections::BTreeMap;

struct CustomEnv;

impl EnvProvider for CustomEnv {
    fn var(&self, name: &str) -> Result<String> {
        // Custom logic here
        Err(EnvError::NotFound(name.to_string()))
    }

    fn vars(&self) -> BTreeMap<String, String> {
        BTreeMap::new()
    }
}

Re-exports§

pub use simulator::var;
pub use simulator::var_exists;
pub use simulator::var_or;
pub use simulator::var_parse;
pub use simulator::var_parse_opt;
pub use simulator::var_parse_or;
pub use simulator::vars;

Modules§

simulator
Simulator environment for testing.
standard
Standard system environment variable access.

Enums§

EnvError
Error types for environment variable operations.

Traits§

EnvProvider
Trait for environment variable access.

Type Aliases§

Result
Result type for environment variable operations.