This is a helper library which is dedicated to make extracting value from environment variables easy.

How To Import

add below line to cargo.toml which is in your project

```toml nb-from-env = "*"

```

Usage

```rust use nbfromenv::{FromEnv, FromEnvDerive}

[derive(FromEnvDerive)]

struct MyServerConfig { loglevel: Option, listenaddress: String, maxconns: i32, isdebug: bool, }

fn main() { dotenv::dotenv().ok(); let config = MyServerConfig::from_env(); ... }

```

Support Data Types

almost all of primitive types in rust are supported, include:

you can also implement FromEnv for your type

Environment Variable Name

If not specified, all environment variables name is the upper-case of field name:

```rust

[derive(FromEnvDerive)]

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

[derive(FromEnvDerive)]

struct MyConfig { #[envvar(THEREALNAME)] // this will search for THEREALNAME in environment variables myvar: String }

```

Default Value

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

Optional Type Field

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