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§
Required Methods§
Sourcefn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Load the primitive from the environment at the given variable.
Provided Methods§
Sourcefn from_env_var_or(
env_var: &str,
default: Self,
) -> Result<Self, FromEnvErr<Self::Error>>
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.
Sourcefn from_env_var_or_else(
env_var: &str,
default: impl FnOnce() -> Self,
) -> Result<Self, FromEnvErr<Self::Error>>
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.
Sourcefn from_env_var_or_default(
env_var: &str,
) -> Result<Self, FromEnvErr<Self::Error>>where
Self: Default,
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
impl FromEnvVar for bool
type Error = ParseBoolError
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl FromEnvVar for i8
impl FromEnvVar for i8
Source§impl FromEnvVar for i16
impl FromEnvVar for i16
Source§impl FromEnvVar for i32
impl FromEnvVar for i32
Source§impl FromEnvVar for i64
impl FromEnvVar for i64
Source§impl FromEnvVar for i128
impl FromEnvVar for i128
Source§impl FromEnvVar for isize
impl FromEnvVar for isize
Source§impl FromEnvVar for u8
impl FromEnvVar for u8
Source§impl FromEnvVar for u16
impl FromEnvVar for u16
Source§impl FromEnvVar for u32
impl FromEnvVar for u32
Source§impl FromEnvVar for u64
impl FromEnvVar for u64
Source§impl FromEnvVar for u128
impl FromEnvVar for u128
Source§impl FromEnvVar for usize
impl FromEnvVar for usize
Source§impl FromEnvVar for String
impl FromEnvVar for String
type Error = Infallible
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl FromEnvVar for Duration
impl FromEnvVar for Duration
type Error = ParseIntError
fn from_env_var(s: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl FromEnvVar for Address
impl FromEnvVar for Address
Source§impl FromEnvVar for Bytes
impl FromEnvVar for Bytes
Source§impl FromEnvVar for SignetEnvironmentConstants
impl FromEnvVar for SignetEnvironmentConstants
type Error = <SignetEnvironmentConstants as FromStr>::Err
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl FromEnvVar for HostConstants
impl FromEnvVar for HostConstants
type Error = <HostConstants as FromStr>::Err
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl FromEnvVar for RollupConstants
impl FromEnvVar for RollupConstants
type Error = <RollupConstants as FromStr>::Err
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl FromEnvVar for SignetConstants
impl FromEnvVar for SignetConstants
type Error = <SignetConstants as FromStr>::Err
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl FromEnvVar for SignetSystemConstants
impl FromEnvVar for SignetSystemConstants
type Error = <SignetSystemConstants as FromStr>::Err
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl FromEnvVar for Level
impl FromEnvVar for Level
Source§impl FromEnvVar for Url
impl FromEnvVar for Url
Source§impl FromEnvVar for U256
impl FromEnvVar for U256
Source§impl<T> FromEnvVar for Option<T>where
T: FromEnvVar,
impl<T> FromEnvVar for Option<T>where
T: FromEnvVar,
type Error = <T as FromEnvVar>::Error
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl<T> FromEnvVar for Box<T>where
T: FromEnvVar,
impl<T> FromEnvVar for Box<T>where
T: FromEnvVar,
type Error = <T as FromEnvVar>::Error
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl<T> FromEnvVar for Arc<T>where
T: FromEnvVar,
impl<T> FromEnvVar for Arc<T>where
T: FromEnvVar,
type Error = <T as FromEnvVar>::Error
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl<T> FromEnvVar for Vec<T>
impl<T> FromEnvVar for Vec<T>
type Error = Infallible
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>>
Source§impl<T, U> FromEnvVar for Cow<'static, U>
impl<T, U> FromEnvVar for Cow<'static, U>
type Error = <T as FromEnvVar>::Error
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.
impl<const N: usize> FromEnvVar for FixedBytes<N>
alloy only.