This is a helper library which is dedicated to make extracting value from environment variables easy.
add below line to cargo.toml which is in your project
```toml nb-from-env = "*"
```
```rust use nbfromenv::{FromEnv, FromEnvDerive}
struct MyServerConfig {
loglevel: Option
fn main() { dotenv::dotenv().ok(); let config = MyServerConfig::from_env(); ... }
```
almost all of primitive types in rust are supported, include:
you can also implement FromEnv
for your type
If not specified, all environment variables name is the upper-case of field name:
```rust
struct MyConfig { myvar: String // this will search for MYVAR in environment varialbes } ```
but you can use #[env_var]
tag to specify which environment variable is related to the field:
```rust
struct MyConfig { #[envvar(THEREALNAME)] // this will search for THEREALNAME in environment variables myvar: String }
```
You can specify default value for non-existing environment variable by #[env_default]
tag:
```rust struct MyConfig { #[envdefault("my default value")] // if MYVAR not exists, "my default value" will assign to myvar myvar: String }
```
note that, the type of default value must be string, value will be convert to correspond type in from_env
function
When environment variable whic you expected does not exist, from_env
will panic if correspond field is not optional(i.e Option<T>
). Optional field will be None
if environment variable is not exists