Trait FromEnvVar

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

    // Required method
    fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>;

    // Provided methods
    fn from_env_var_or(
        env_var: &str,
        default: Self,
    ) -> Result<Self, FromEnvErr<Self::Error>> { ... }
    fn from_env_var_or_else(
        env_var: &str,
        default: impl FnOnce() -> Self,
    ) -> Result<Self, FromEnvErr<Self::Error>> { ... }
    fn from_env_var_or_default(
        env_var: &str,
    ) -> Result<Self, FromEnvErr<Self::Error>>
       where Self: Default { ... }
}
Expand description

Trait for loading primitives from the environment. These are simple types that should correspond to a single environment variable. It has been implemented for common integer types, String, url::Url, tracing::Level, and std::time::Duration.

It aims to make FromEnv implementations easier to write, by providing a default implementation for common types.

§Note on error types

FromEnv and FromEnvVar are often deeply nested. This means that error types are often nested as well. To avoid this, we use a single error type FromEnvVar that wraps an inner error type. This allows us to ensure that env-related errors (e.g. missing env vars) are not lost in the recursive structure of parsing errors. Environment errors are always at the top level, and should never be nested. Do not use FromEnvErr<T> as the Error associated type in FromEnv.

// Do not do this
impl FromEnv for MyType {
    type Error = FromEnvErr<MyTypeErr>;
}

// Instead do this:
impl FromEnv for MyType {
   type Error = MyTypeErr;
}

§Implementing FromEnv

FromEnvVar is a trait for loading simple types from the environment. It represents a type that can be loaded from a single environment variable. It is similar to FromStr and will usually be using an existing FromStr impl.


// We can re-use the `FromStr` implementation for our `FromEnvVar` impl.
impl FromEnvVar for MyCoolType {
    type Error = <MyCoolType as FromStr>::Err;

    fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
    {
        String::from_env_var(env_var).unwrap().parse().map_err(Into::into)
    }
}

Required Associated Types§

Source

type Error: Error

Error type produced when parsing the primitive.

Required Methods§

Source

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Load the primitive from the environment at the given variable.

Provided Methods§

Source

fn from_env_var_or( env_var: &str, default: Self, ) -> Result<Self, FromEnvErr<Self::Error>>

Load the primitive from the environment at the given variable. If the variable is unset or empty, return the default value.

This function will return an error if the environment variable is set but cannot be parsed.

Source

fn from_env_var_or_else( env_var: &str, default: impl FnOnce() -> Self, ) -> Result<Self, FromEnvErr<Self::Error>>

Load the primitive from the environment at the given variable. If the variable is unset or empty, call the provided function to get the default value.

This function will return an error if the environment variable is set but cannot be parsed.

Source

fn from_env_var_or_default( env_var: &str, ) -> Result<Self, FromEnvErr<Self::Error>>
where Self: Default,

Load the primitive from the environment at the given variable. If the variable is unset or empty, return the value generated by Default::default.

This function will return an error if the environment variable is set but cannot be parsed.

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.

Implementations on Foreign Types§

Source§

impl FromEnvVar for bool

Source§

impl FromEnvVar for i8

Source§

type Error = <i8 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for i16

Source§

type Error = <i16 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for i32

Source§

type Error = <i32 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for i64

Source§

type Error = <i64 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for i128

Source§

type Error = <i128 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for isize

Source§

type Error = <isize as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for u8

Source§

type Error = <u8 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for u16

Source§

type Error = <u16 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for u32

Source§

type Error = <u32 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for u64

Source§

type Error = <u64 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for u128

Source§

type Error = <u128 as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for usize

Source§

type Error = <usize as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for String

Source§

impl FromEnvVar for Duration

Source§

impl FromEnvVar for Address

Source§

type Error = <Address as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for Bytes

Source§

type Error = <Bytes as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for SignetEnvironmentConstants

Source§

impl FromEnvVar for HostConstants

Source§

impl FromEnvVar for RollupConstants

Source§

impl FromEnvVar for SignetConstants

Source§

impl FromEnvVar for SignetSystemConstants

Source§

impl FromEnvVar for Level

Source§

type Error = <Level as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for Url

Source§

type Error = <Url as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl FromEnvVar for U256

Source§

type Error = <Uint<256, 4> as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl<T> FromEnvVar for Option<T>
where T: FromEnvVar,

Source§

type Error = <T as FromEnvVar>::Error

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl<T> FromEnvVar for Box<T>
where T: FromEnvVar,

Source§

type Error = <T as FromEnvVar>::Error

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl<T> FromEnvVar for Arc<T>
where T: FromEnvVar,

Source§

type Error = <T as FromEnvVar>::Error

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl<T> FromEnvVar for Vec<T>
where T: From<String> + Debug + 'static,

Source§

impl<T, U> FromEnvVar for Cow<'static, U>
where T: FromEnvVar, U: ToOwned<Owned = T> + Debug + ?Sized,

Source§

type Error = <T as FromEnvVar>::Error

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Source§

impl<const N: usize> FromEnvVar for FixedBytes<N>

Available on crate feature alloy only.
Source§

type Error = <FixedBytes<N> as FromStr>::Err

Source§

fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>

Implementors§