DesEnv-rs
Environment variables deserializer library.
With this library is possible to deserialize a bunch of environment variables in a more structured data type.
Install
# Cargo.toml
[]
= "0.1.0"
Usage
The library expose a utility function to load the configuration from environment variables
Then Config should be something like this
use Desenv;
In order to have a successful loaded Config it's needed that in the env there are two variables:
FIELD1FIELD2
Customize configuration
It is possible to customize how the configuration is loaded.
Rename
Using rename modifier on desenv field attribute is possible to instruct the library to look for a different variable.
In the example above, while loading the configuration, the library will try to find the FIELD_1 variable.
use Desenv;
Pay attention because rename attribute is case-sensitive. So in an example like the above one the library will try to find
an environment variable called downcased_field_1.
use Desenv;
Default
With this modifier is possible to instruct the library to deserialize an environment variable with a default value.
With value
If the FIELD1 environment variable does not exist the library will fill field1 with value.
use Desenv;
or
use Desenv;
With env
If the FIELD1 environment variable does not exist the library will try to load, as fallback, the FALLBACK_FIELD1
environment variable. If FALLBACK_FIELD1 does not exist too, then the load will fail.
use Desenv;
With std default
If the FIELD1 environment variable does not exist the library will fill field1 with the Default implementation for
that type. So, for example, for a string the default will be an empty string.
use Desenv;
Separator
Instruct the library on how to deserialize a string to a vector performing a split over the string. The provided value
must be a char. Cannot be used on non-vector fields.
use Desenv;
Nested
Tells the library that the specified field shouldn't be deserialized using an environment variable but using Desenv
macro too.
use Desenv;
Prefix
If set apply that prefix for every environment variable deserialized. In the example below field1 will be deserialized
as PREFIXED_FIELD1. Note that prefix is case-sensitive.
use Desenv;
If prefixed struct has a prefixed nested struct the result will be a concatenation of both the prefixes. In the example
the field in NestedConfig will be resolved using PARENT_NESTED_FIELD environment variable.
use Desenv;
OsString
OsStrings are supported for some specific cases where an environment variable is not utf8 encodable.
use OsString;
use Desenv;
Supported types
Right now every T that mix-in the FromStr trait could be used as simple fields. Other supported types are:
Option<T>Vec<T>OsString