Skip to content

A general dynamic YAML configuration library for Rust projects.

License

Notifications You must be signed in to change notification settings

angrygoats/yaml-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dynamic Configurator for Rust

Tests codecov

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:

  1. Keys are recursively named according to hierarchy. In the example above, one key would be DATABASE_USERNAME.
  2. If a key is null the environment will be searched using the hierarchy name described in (1).
  3. 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.

Notes

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.

Examples

Load a File with Environment Preference

use yaml_config::Preference;
use yaml_config::load;
let configuration = load("path/to/yaml/file.yaml",
                         Some(Preference::PreferEnv))?;

Load a File with YAML Preference

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)?;

Accessing Values

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.

About

A general dynamic YAML configuration library for Rust projects.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages