fabro/lib/crates/fabro-util/src/env.rs
Bryan Helmkamp 80de5ca616 refactor(static): centralize env var names
Add fabro-static::EnvVars as the shared registry for fixed environment variable names and migrate env reads, clap env bindings, and subprocess/test allowlists to use it.

Add clippy bans for raw std::env lookup APIs so future dynamic env facades must be documented explicitly.
2026-04-24 12:29:51 -04:00

38 lines
1.2 KiB
Rust

/// Abstraction over environment variable lookup.
///
/// Production code uses [`SystemEnv`] which delegates to [`std::env::var`].
/// Tests inject a [`TestEnv`] backed by a `HashMap` so they never mutate
/// process-global state.
pub trait Env: Send + Sync {
fn var(&self, key: &str) -> Result<String, std::env::VarError>;
}
/// Reads real process environment variables.
#[derive(Clone, Debug)]
pub struct SystemEnv;
impl Env for SystemEnv {
#[expect(
clippy::disallowed_methods,
reason = "SystemEnv is the intentional process-env facade behind the Env trait."
)]
fn var(&self, key: &str) -> Result<String, std::env::VarError> {
std::env::var(key)
}
}
/// In-memory environment double — no process-global mutation.
///
/// Intended for use in tests across the workspace. Unconditionally compiled
/// because it is trivial and has no external dependencies.
#[derive(Clone, Debug)]
pub struct TestEnv(pub std::collections::HashMap<String, String>);
impl Env for TestEnv {
fn var(&self, key: &str) -> Result<String, std::env::VarError> {
self.0
.get(key)
.cloned()
.ok_or(std::env::VarError::NotPresent)
}
}