Expand description
renvar
is library to help deserialize environment variables into Rust data structure
§Example
use renvar::{from_env, from_iter, from_str};
use serde::Deserialize;
use std::env;
let env_content = r#"
name=renvar
type=Library
dependencies=serde
"#;
#[derive(Debug, Deserialize, PartialEq, Eq)]
enum CrateType {
Library,
Binary,
}
#[derive(Debug, Deserialize, PartialEq, Eq)]
struct Renvar {
name: String,
#[serde(rename = "type")]
typ: CrateType,
dependencies: Vec<String>,
}
let actual = Renvar {
name: "renvar".to_owned(),
typ: CrateType::Library,
dependencies: vec!["serde".to_owned()],
};
// we can read from strings
let value = from_str::<Renvar>(env_content).unwrap();
assert_eq!(value, actual);
// directly from the environment
let envs = vec![
("name".to_owned(), "renvar".to_owned()),
("type".to_owned(), "Library".to_owned()),
("dependencies".to_owned(), "serde".to_owned()),
];
for (key, value) in envs.clone().into_iter() {
env::set_var(key, value);
}
let value = from_env::<Renvar>().unwrap();
assert_eq!(value, actual);
// or from iterables
let value = from_iter::<Renvar, _>(envs).unwrap();
assert_eq!(value, actual);
§Feature flags
Renvar has the following feature flags:
§prefixed
prefixed
gives you the prefixed
function, that accepts a prefix. The prefixes will be stripped away
before deserialization.
§postfixed
postfix
is exactly the same as prefix, just with postfixes
§case_insensitive_prefixed
Case insensitive variant of prefixed
§case_insensitive_postfixed
Case insensitive variant of postfixed
§with_trimmer
Finally, the with_trimmer
feature flag gives you *_with_trimmer
variants for all of the above,
where you can strip extraneous characters off of the beginning and end of envrironment variables
by passing a closure.
§Supported datatypes
Strings
andstr
senums
sequences
Unit structs
Modules§
- de
- Module that provides an
EnvVarDeserializer
, that does exactly as it says: deserializes environment variables into Rust structs
Structs§
- Case
Insensitive Postfixed - Deserialize environment variables with postfixes.
To create an instance of
CaseInsensitivePostfixed
, you can use thecase_insensitive_postfixed
function: - Case
Insensitive Prefixed - Deserialize environment variables with prefixes.
To create an instance of
CaseInsensitivePrefixed
, you can use thecase_insensitive_prefixed
function: - Postfixed
- Aids in deserializing some type
T
from environment variables, where the keys are postfixed. Users are meant to obtain this struct by callingpostfixed
. - Prefixed
- Aids in deserializing some type
T
from environment variables, where the keys are prefixed. Users are meant to obtain this struct by callingprefixed
.
Enums§
- Error
- Crate level
Error
type
Functions§
- case_
insensitive_ postfixed - Aids in deserializing some type
T
from environment variables, where the keys are postfixed. Users are meant to obtain aCaseInsensitivePostfixed
struct by callingcase_insensitive_postfixed
. - case_
insensitive_ prefixed - Aids in deserializing some type
T
from environment variables, where the keys are prefixed. Users are meant to obtain aCaseInsensitivePrefixed
struct by callingcase_insensitive_prefixed
. - from_
env - Deserialize some type
T
from a snapshot of the processes environment variables at the time of invocation. - from_
env_ with_ trimmer - Deserialize some type
T
from a snapshot of the processes environment variables at the time of invocation, where you can specify custom trimming logic with a closure. - from_
iter - Deserialize some type
T
from an iterator of key-value pairs - from_
iter_ with_ trimmer - Deserialize some type
T
from an iterator over(String, String)
(key, value)
pairs, where you can specify custom trimming logic with a closure. - from_
os_ env - Deserialize some type
T
from a snapshot of the processes environment variables at the time of invocation. - from_
os_ env_ with_ trimmer - Deserialize some type
T
from a snapshot of the processes environment variables at the time of invocation. - from_
str - Deserialize some type
T
from astr
- postfixed
- Aids in deserializing some type
T
from environment variables, where the keys are postfixed. Users are meant to obtain aPostfixed
struct by callingpostfixed
. - prefixed
- Aids in deserializing some type
T
from environment variables, where the keys are prefixed. Users are meant to obtain aPrefixed
struct by callingprefixed
.
Type Aliases§
- Result
Result
type alias used by this crate