luff 0.2.1

Print files with formatting
Documentation
//! Environment variable abstraction for testability
//!
//! This module provides a trait for accessing environment variables,
//! allowing tests to mock the environment without using unsafe code
//! or serial execution constraints.

#[cfg(test)]
use std::collections::HashMap;

/// Provider for environment variables
pub trait EnvProvider: Send + Sync {
    /// Get an environment variable by key
    fn var(&self, key: &str) -> Option<String>;
}

/// Production implementation that reads from the process environment
pub struct RealEnv;

impl EnvProvider for RealEnv {
    fn var(&self, key: &str) -> Option<String> {
        std::env::var(key).ok()
    }
}

/// Mock implementation for testing
#[cfg(test)]
#[derive(Default, Clone)]
pub struct MockEnv {
    vars: HashMap<String, String>,
}

#[cfg(test)]
impl MockEnv {
    /// Create a new mock environment
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set a variable in the mock environment
    #[must_use]
    pub fn with_var(mut self, key: &str, value: &str) -> Self {
        let _ = self.vars.insert(key.to_string(), value.to_string());
        self
    }

    /// Remove an environment variable
    ///
    /// Useful for ensuring a variable is explicitly unset in the mock.
    #[must_use]
    pub fn without_var(mut self, key: &str) -> Self {
        let _ = self.vars.remove(key);
        self
    }
}

#[cfg(test)]
impl EnvProvider for MockEnv {
    fn var(&self, key: &str) -> Option<String> {
        self.vars.get(key).cloned()
    }
}