EnvAccess

Trait EnvAccess 

Source
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§

Source

fn env_provider(&self) -> &dyn EnvProvider

Get the environment provider.

Provided Methods§

Source

fn env(&self, key: &str) -> Option<String>

Get an environment variable.

Returns None if the variable is not set.

Source

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.

Source

fn env_require(&self, key: &str) -> Result<String>

Get a required environment variable.

Returns an error if the variable is not set.

Source

fn env_parse<T: FromStr>(&self, key: &str) -> Result<T>
where T::Err: Display,

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
Source

fn env_parse_or<T: FromStr>(&self, key: &str, default: T) -> Result<T>
where T::Err: Display,

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.

Source

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.

Implementors§