Crate strict_env[−][src]
Use this crate to parse environment variables into any type that
implements FromStr
.
Basic usage
std::env::set_var("PORT", "9001"); let port: u16 = strict_env::parse("PORT")?; assert_eq!(port, 9001);
Usage with remote types
If you need to parse a type that originates from an external crate
and does not implement FromStr
, you can wrap
the value in a newtype that implements the trait.
// std::time::Duration does not implement FromStr! struct ConfigDuration(std::time::Duration); // Custom implementation using the awesome humantime crate impl std::str::FromStr for ConfigDuration { type Err = humantime::DurationError; fn from_str(s: &str) -> Result<Self, Self::Err> { let inner = humantime::parse_duration(s)?; Ok(Self(inner)) } } // Now we can use strict_env! (But we might have to use the turbofish.) std::env::set_var("CACHE_DURATION", "2 minutes"); let cache_duration = strict_env::parse::<ConfigDuration>("CACHE_DURATION")?.0; assert_eq!(cache_duration.as_secs(), 120);
Enums
Error | Error type for this library. |
Functions
parse | Parse an environment variable into a value that implements
|
parse_optional | Like |
parse_or_default | Like |