Crates.io | yaml-config |
lib.rs | yaml-config |
version | 1.0.0 |
source | src |
created_at | 2022-09-09 14:57:23.746203 |
updated_at | 2022-09-09 14:57:23.746203 |
description | Dynamic YAML configuration parser. |
homepage | https://github.com/angrygoats/yaml-config |
repository | https://github.com/angrygoats/yaml-config |
max_upload_size | |
id | 661923 |
size | 44,798 |
The dynamic configuration library is simple: it takes an arbitrary YAML file, parses it into a common format, and returns a hashmap of configurations and their keys. It supports environment loading as well.
Take for example the following:
database:
name: null
username: null
password: null
port: null
logging:
level: "INFO"
performance:
threads: 8
The YAML file will be parsed into a hashmap using the following rules:
Keys are recursively named according to hierarchy. In the example above, one key would be DATABASE_USERNAME
.
If a key is null
the environment will be searched using the hierarchy name described in (1).
If a key has a value the behavior is determined by the preference
argument to load
. If Preference::PreferEnv
is
given, an environment value will be taken like (2) in all cases. If the environment value is not available it
will use the YAML file's given key value. If Preference::PreferYaml
is given, it will take the YAML file always.
The parser does not currently support arrays.
The YAML parser is recursive. As a result there is a stack-size limit to the depth of nesting that can be handled.
use yaml_config::Preference;
use yaml_config::load;
let configuration = load("path/to/yaml/file.yaml",
Some(Preference::PreferEnv))?;
use yaml_config::Preference;
use yaml_config::load;
let configuration = load("path/to/yaml/file.yaml",
Some(Preference::PreferYaml))?;
or
use yaml_config::load;
let configuration = load("path/to/yaml/file.yaml", None)?;
Values are stored in an enum representing the type.
use yaml_config::load;
let configuration = load("path/to/yaml/file.yaml", None)?;
let val = *config["TEST_ENVIRONMENT_VARIABLE"].as_i64().unwrap();
Due to Rust's strict typing you will be responsible for knowing ahead of time what can populate your variables.
Even though unwrap
is shown here it is highly recommend you use match
to compensate for this.