Trait FromEnv

Source
pub trait FromEnv:
    Debug
    + Sized
    + 'static {
    type Error: Error;

    // Required methods
    fn inventory() -> Vec<&'static EnvItemInfo>;
    fn from_env() -> Result<Self, FromEnvErr<Self::Error>>;

    // Provided method
    fn check_inventory() -> Result<(), Vec<&'static EnvItemInfo>> { ... }
}
Expand description

Trait for loading from the environment.

This trait is for structs or other complex objects, that need to be loaded from the environment. It expects that

  • The struct is Sized and 'static.
  • The struct elements can be parsed from strings.
  • Struct elements are at fixed env vars, known by the type at compile time.

As such, unless the env is modified, these are essentially static runtime values.

Required Associated Types§

Source

type Error: Error

Error type produced when loading from the environment.

Required Methods§

Source

fn inventory() -> Vec<&'static EnvItemInfo>

Get the required environment variable names for this type.

§Note

This MUST include the environment variable names for all fields in the struct, including optional vars.

Source

fn from_env() -> Result<Self, FromEnvErr<Self::Error>>

Load from the environment.

Provided Methods§

Source

fn check_inventory() -> Result<(), Vec<&'static EnvItemInfo>>

Get a list of missing environment variables.

This will check all environment variables in the inventory, and return a list of those that are non-optional and missing. This is useful for reporting missing environment variables.

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§