irx-config library
The irx-config
library provides convenient way to represent/parse configuration from different sources. The main
goals is to be very easy to use and to be extendable.
Features
- Fully compatible with serde
- Full deep merge of nested dictionaries/mappings
- Case sensitive/insensitive parameters names matching/merging
- Sealing secrets during display/debugging
- Get all configuration parameters or just cherry pick few
- Several embedded parsers available via library features:
- Command-line argument (via clap)
- Environment variables
- File based parsers:
JSON
,JSON5
,YAML
andTOML
- Could be extended with custom parsers
Examples
JSON with environment variables
To enable parsers used in example below, one has to add the following to Cargo.toml
:
[]
= { = "3.4", = ["env", "json"] }
use ;
use ConfigBuilder;
use Deserialize;
// Data from two parsers will be merged. The values from parser appended first (`JSON`)
// will take precedence if values have a same names
let config = default
.append_parser
.append_parser
.load?;
let conf_data: Conf = config.get?;
Command-line, TOML and environment variables
To enable parsers used in example below, one has to add the following to Cargo.toml
:
[]
= { = "3.4", = ["cmd", "env", "toml-parser"] }
use app_from_crate;
use ;
use ConfigBuilder;
use Deserialize;
let app = app_from_crate!;
// Data from three parsers will be merged. The values from parser appended first (`cmd`)
// will take precedence if values have a same names
let config = default
.append_parser
.append_parser
.append_parser
.load?;
let conf_data: Conf = config.get?;
Custom parser
use ;
use Deserialize;
use Cow;
let data = r#"{ "id": 42, "logger": "file", "tag": "test" }"#;
let config = load_one?;
let conf_data: Conf = config.get?;
JSON parser get partial data
To enable parsers used in example below, one has to add the following to Cargo.toml
:
[]
= { = "3.4", = ["json"] }
use json;
use ConfigBuilder;
use Deserialize;
let config = load_one?;
let logger: Logger = config.get_by_key_path?.unwrap;
let port: u16 = config.get_by_key_path?.unwrap;