pub trait EnvAccess {
// Required method
fn env_provider(&self) -> &dyn EnvProvider;
// Provided methods
fn env(&self, key: &str) -> Option<String> { ... }
fn env_or(&self, key: &str, default: &str) -> String { ... }
fn env_require(&self, key: &str) -> Result<String> { ... }
fn env_parse<T: FromStr>(&self, key: &str) -> Result<T>
where T::Err: Display { ... }
fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
where T::Err: Display { ... }
fn env_contains(&self, key: &str) -> bool { ... }
}Expand description
Extension methods for environment variable access on contexts.
This is implemented as a separate trait to avoid code duplication across different context types.
Required Methods§
Sourcefn env_provider(&self) -> &dyn EnvProvider
fn env_provider(&self) -> &dyn EnvProvider
Get the environment provider.
Provided Methods§
Sourcefn env(&self, key: &str) -> Option<String>
fn env(&self, key: &str) -> Option<String>
Get an environment variable.
Returns None if the variable is not set.
Sourcefn env_or(&self, key: &str, default: &str) -> String
fn env_or(&self, key: &str, default: &str) -> String
Get an environment variable with a default value.
Returns the default if the variable is not set.
Sourcefn env_require(&self, key: &str) -> Result<String>
fn env_require(&self, key: &str) -> Result<String>
Get a required environment variable.
Returns an error if the variable is not set.
Sourcefn env_parse<T: FromStr>(&self, key: &str) -> Result<T>
fn env_parse<T: FromStr>(&self, key: &str) -> Result<T>
Get an environment variable and parse it to the specified type.
Returns an error if:
- The variable is not set
- The value cannot be parsed to the target type
Sourcefn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
Get an environment variable and parse it, with a default.
Returns the default if the variable is not set. Returns an error only if the variable IS set but cannot be parsed.
Sourcefn env_contains(&self, key: &str) -> bool
fn env_contains(&self, key: &str) -> bool
Check if an environment variable is set.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.