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.
JSON
, YAML
and TOML
To enable parsers used in example below, one has to add the following to Cargo.toml
:
toml
[dependencies]
irx-config = { version = "1.0", features = ["env", "json"] }
```rust use irxconfig::parsers::{env, json}; use irxconfig::ConfigBuilder; use serde::Deserialize;
struct Conf { id: u32, logger: String, tag: String, }
// 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 = ConfigBuilder::default()
.appendparser(
json::ParserBuilder::default()
.defaultpath("config.json")
.build()?,
)
.appendparser(
env::ParserBuilder::default()
.defaultprefix("APP_")
.build()?,
)
.load()?;
let conf_data: Conf = config.get()?; ```
To enable parsers used in example below, one has to add the following to Cargo.toml
:
toml
[dependencies]
irx-config = { version = "1.0", features = ["cmd", "env", "toml-parser"] }
```rust use clap::App; use irxconfig::parsers::{cmd, env, toml}; use irxconfig::ConfigBuilder; use serde::Deserialize;
struct Logger { level: String, path: String, }
struct Connection { #[serde(default = "localhost")] host: String, port: u16, }
struct Conf { id: u32, logger: Logger, connection: Connection, }
let yaml = clap::loadyaml!("cmd.yaml"); let matches = App::fromyaml(yaml).get_matches();
// 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 = ConfigBuilder::default()
.appendparser(
cmd::ParserBuilder::default()
.matches(matches)
.tryargnamesfromyaml(includestr!("cmd.yaml"))?
.build()?,
)
.appendparser(
toml::ParserBuilder::default()
.defaultpath("config.toml")
.pathoption("config")
.build()?,
)
.appendparser(
env::ParserBuilder::default()
.defaultprefix("APP")
.prefix_option("prefix")
.build()?,
)
.load()?;
let conf_data: Conf = config.get()?; ```
```rust use irx_config::{AnyResult, Case, ConfigBuilder, Parse, Value}; use serde::Deserialize; use std::borrow::Cow;
struct Conf { id: u32, logger: String, tag: String, }
struct JsonStringParser<'a> { data: Cow<'a, str>, }
impl<'a> JsonStringParser<'a> {
pub fn new(data: impl Into
impl Case for JsonStringParser<'_> {}
impl Parse for JsonStringParser<'> {
fn parse(&mut self, _value: &Value) -> AnyResult
let data = r#"{ "id": 42, "logger": "file", "tag": "test" }"#; let config = ConfigBuilder::loadone(JsonStringParser::new(data))?; let confdata: Conf = config.get()?; ```
To enable parsers used in example below, one has to add the following to Cargo.toml
:
toml
[dependencies]
irx-config = { version = "1.0", features = ["json"] }
```rust use irxconfig::parsers::json; use irxconfig::ConfigBuilder; use serde::Deserialize;
struct Logger { level: String, path: String, }
struct Connection { #[serde(default = "localhost")] host: String, port: u16, }
let config = ConfigBuilder::loadone( json::ParserBuilder::default() .defaultpath("config.json") .build()?, )?;
let logger: Logger = config.getbykeypath("logger")?.unwrap(); let port: u16 = config.getbykeypath("connection:port")?.unwrap(); ```