
envious allows you to deserialize your serde enabled structs from
environment variables.
See it in action:
use ;
let config: Config = from_env.expect;
With the following environment variables:
it will parse it from the environment and give you a Rust struct you can use in your application.
Note: The environment variables are case sensitive! This is due to how
serdeworks internally. If you want your structs to use SCREAMING_SNAKE_CASE, then be sure to use the#[serde(rename_all = "SCREAMING_SNAKE_CASE"]annotation on all concerned structs.
Getting Started
To use envious simply add it to your Cargo.toml with:
and deserialize from your environment with envious::from_env!
⚠️ Current Shortcomings
- Tuple Enum Variants can currently not be longer than one element!
- Ordering of arrays is highly sensitive to environment order
- No ordering is currently done, and the ordering depends on how the operating system propagates variables
How deserialization works
The mapping between environment variables and the serde model is as follows:
Nested fields are seperated by __ in their names
For example, if you have the following struct:
use ;
You can deserialize Config with the following variables:
Arrays are serialized using nested fields with the individual keys being discarded
Arrays are represented as anonymous structs, with the 'fields' being the individual elements.
A more complex example could look like this:
use ;
Now, to deserialize a House we can set the following variables:
As you can see, the individual 'keys' of the array do not matter! The same key refers to the same object though.
Unit enums variants (without fields), are serialized from strings
As you can see in the example above, the Material enum gets simply deserialized from the name of the variant. Be careful about upper/lower case Serde expects per-default that the case is exactly the same!
Complex enum variants are serialzed just like structs
Per default serde uses external tagging for more complicated enum variants.
Tuple enums are currently only supported with a single value.
To see what this means, lets take this enum as an example:
use ;
To deserialize Config here, we can use the following variables:
Any of these sets of variables would give you the expected outcome.
Should you change the tagging of your struct, be sure to adapt the given variables.
License
envious is licensed under MIT or Apache 2.0, as you wish.
Contributing
To contribute to envious you can:
- Open up issues with ideas, remarks, bug reports, etc...
- Fork and implement new features and send them in as pull requests
- Leave it a Star and spread the word! ;)