Irx-config - The library provides convenient way to represent/parse configuration from different sources

Overview

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, YAML and TOML
  • 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:

[dependencies]
irx-config = { version = "1.0", features = ["env", "json"] }
use irx_config::parsers::{env, json};
use irx_config::ConfigBuilder;
use serde::Deserialize;

#[derive(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()
    .append_parser(
        json::ParserBuilder::default()
            .default_path("config.json")
            .build()?,
    )
    .append_parser(
        env::ParserBuilder::default()
            .default_prefix("APP_")
            .build()?,
    )
    .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:

[dependencies]
irx-config = { version = "1.0", features = ["cmd", "env", "toml-parser"] }
use clap::App;
use irx_config::parsers::{cmd, env, toml};
use irx_config::ConfigBuilder;
use serde::Deserialize;

#[derive(Deserialize)]
struct Logger {
    level: String,
    path: String,
}

#[derive(Deserialize)]
struct Connection {
    #[serde(default = "localhost")]
    host: String,
    port: u16,
}

#[derive(Deserialize)]
struct Conf {
    id: u32,
    logger: Logger,
    connection: Connection,
}

let yaml = clap::load_yaml!("cmd.yaml");
let matches = App::from_yaml(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()
    .append_parser(
        cmd::ParserBuilder::default()
            .matches(matches)
            .try_arg_names_from_yaml(include_str!("cmd.yaml"))?
            .build()?,
    )
    .append_parser(
        toml::ParserBuilder::default()
            .default_path("config.toml")
            .path_option("config")
            .build()?,
    )
    .append_parser(
        env::ParserBuilder::default()
            .default_prefix("APP_")
            .prefix_option("prefix")
            .build()?,
    )
    .load()?;

let conf_data: Conf = config.get()?;

Custom parser

use irx_config::{AnyResult, Case, ConfigBuilder, Parse, Value};
use serde::Deserialize;
use std::borrow::Cow;

#[derive(Deserialize)]
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<Cow<'a, str>>) -> Self {
        JsonStringParser { data: data.into() }
    }
}

impl Case for JsonStringParser<'_> {}

impl Parse for JsonStringParser<'_> {
    fn parse(&mut self, _value: &Value) -> AnyResult<Value> {
        Ok(serde_json::from_str(&self.data)?)
    }
}

let data = r#"{ "id": 42, "logger": "file", "tag": "test" }"#;
let config = ConfigBuilder::load_one(JsonStringParser::new(data))?;
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:

[dependencies]
irx-config = { version = "1.0", features = ["json"] }
use irx_config::parsers::json;
use irx_config::ConfigBuilder;
use serde::Deserialize;

#[derive(Deserialize)]
struct Logger {
    level: String,
    path: String,
}

#[derive(Deserialize)]
struct Connection {
    #[serde(default = "localhost")]
    host: String,
    port: u16,
}

let config = ConfigBuilder::load_one(
    json::ParserBuilder::default()
        .default_path("config.json")
        .build()?,
)?;

let logger: Logger = config.get_by_key_path("logger")?.unwrap();
let port: u16 = config.get_by_key_path("connection:port")?.unwrap();
Comments
  • converting Error to anyhow::Error

    converting Error to anyhow::Error

    When I trying to use ? on irx_config Result I see: изображение

    Related issue: https://github.com/dtolnay/anyhow/issues/66

    Although, it is not blocking for me to use this crate.

    opened by burrbull 6
  • Changes for 3.0.x

    Changes for 3.0.x

    • Upgrade clap dependency to the next major version 4.0.x (closed #14).
    • Remove single_flags_as_bool setting from cmd parser. That functionality should be achieved via clap::ArgAction.
    • The default value of use_arg_types settings was changed to true.
    enhancement 
    opened by abakay 5
  • Changes for `clap` defaults

    Changes for `clap` defaults

    • Fixed issue with default values for clap command-line parameters (#15).
    • Added a new setting use_defaults to cmd parser. Use defaults from clap arguments. Default is false. IMPORTANT: Once that setting will be set to true then all defined command-line parameters will have values which will override values with same key(s) from parsers which was added to ConfigBuilder after this parser.
    bug enhancement 
    opened by abakay 1
  • Enhancements:

    Enhancements:

    • Added a new setting single_flags_as_bool to cmd parser. If that setting is set to true and a parameter not allowed to have multiple occurrences by clap API then parameter's value will have boolean true as a value. By default all command-line parameters without values will have they values set to number of they occurrences. Closes #7.

    • Added a new setting ignore_missing_file to file based parsers. If that setting is set to true then a file does not exists, do not try to load it. The default Value will be returned. Default is false. Closes #8.

    opened by abakay 1
  • How to work with clap boolean flags?

    How to work with clap boolean flags?

    I'm trying to parse flags as boolean config fields. But when I enable cli flag:

    thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SerdeError(Error("invalid type: integer `1`, expected a boolean", line: 0, column: 0))'
    
    enhancement 
    opened by burrbull 1
  • Changes

    Changes

    • Upgrade clap dependency to the next minor version 3.2.x.
    • Upgrade serde_yaml dependency to the next minor version 0.9.x.
    • Upgrade derive_builder dependency to the next minor version 0.11.x.
    • Added a new setting use_arg_types to cmd parser. If that setting is set to true then use ArgAction or ValueParser type to calculate type of an argument. Default is false.
    opened by abakay 0
  • Changes:

    Changes:

    NOTE: The clap was upgraded to next major version 3.x. Some old API was deprecated (clap::load_yaml!, etc.). These changes also forced an increase in the major version for this crate.

    • Upgrade clap dependency to the next major version 3.x.
    • The following methods was removed from cmd::ParserBuilder:
      • default(), replaced by new(...)
      • matches(...)
      • arg_names(...)
      • try_arg_names_from_yaml(...)
    opened by abakay 0
Releases(3.1.1)
  • 3.1.1(Dec 18, 2022)

  • 3.1.0(Oct 8, 2022)

    Changes:

    • Fixed issue with default values for clap command-line parameters.
    • Added a new setting use_defaults to cmd parser. Use defaults from clap arguments. Default is false. IMPORTANT: Once that setting will be set to true then all defined command-line parameters will have values which will override values with same key(s) from parsers which was added to ConfigBuilder after this parser.
    Source code(tar.gz)
    Source code(zip)
  • 3.0.0(Oct 6, 2022)

    Changes:

    • Upgrade clap dependency to the next major version 4.0.x.
    • Remove single_flags_as_bool setting from cmd parser. That functionality should be achieved via clap::ArgAction.
    • The default value of use_arg_types settings was changed to true.
    Source code(tar.gz)
    Source code(zip)
  • 2.4.0(Sep 13, 2022)

    Changes:

    • Upgrade clap dependency to the next minor version 3.2.x.
    • Upgrade serde_yaml dependency to the next minor version 0.9.x.
    • Upgrade derive_builder dependency to the next minor version 0.11.x.
    • Added a new setting use_arg_types to cmd parser. If that setting is set to true then use ArgAction or ValueParser type to calculate type of an argument. Default is false.
    Source code(tar.gz)
    Source code(zip)
  • 2.3.2(Sep 9, 2022)

  • 2.3.1(Sep 7, 2022)

  • 2.3.0(Sep 6, 2022)

    Enhancements:

    • Added a new setting single_flags_as_bool to cmd parser. If that setting is set to true and a parameter not allowed to have multiple occurrences by clap API then parameter's value will have boolean true as a value. By default all command-line parameters without values will have they values set to number of they occurrences.
    • Added a new setting ignore_missing_file to file based parsers. If that setting is set to true then a file does not exists, do not try to load it. The default Value will be returned. Default is false.
    Source code(tar.gz)
    Source code(zip)
  • 2.2.0(Jul 11, 2022)

  • 2.1.0(Jun 2, 2022)

  • 2.0.0(Jan 23, 2022)

    NOTE: The clap was upgraded to next major version 3.x. Some old API was deprecated (clap::load_yaml!, etc.). These changes also forced an increase in the major version for this crate.

    • Upgrade clap dependency to the next major version 3.x.
    • The following methods was removed from cmd::ParserBuilder:
      • default(), replaced by new(...)
      • matches(...)
      • arg_names(...)
      • try_arg_names_from_yaml(...)
    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(Dec 31, 2021)

  • 1.0.1(Dec 27, 2021)

  • 1.0.0(Dec 27, 2021)

    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, YAML and TOML
    • Could be extended with custom parsers
    Source code(tar.gz)
    Source code(zip)
Owner
Andriy Bakay
Andriy Bakay
Generate Soufflé Datalog types, relations, and facts that represent ASTs from a variety of programming languages.

treeedb treeedb makes it easier to start writing a source-level program analysis in Soufflé Datalog. First, treeedb generates Soufflé types and relati

Langston Barrett 16 Nov 30, 2022
`matchable` provides a convenient enum for checking if a piece of text is matching a string or a regex.

matchable matchable provides a convenient enum for checking if a piece of text is matching a string or a regex. The common usage of this crate is used

Pig Fang 6 Dec 19, 2022
The dead easy way to use config files in your rust project

Configr The dead easy way to use config files in your project This will load a config.toml file if it exists, otherwise it will create the needed fold

Carsten Kragelund Jørgensen 4 Jul 1, 2022
plonky2 recursion framework handling different circuits in unified way.

generic_recursion Version: 0.1.0 generic_recursion is a crate that allows to easily aggregate an unlimited amount of plonky2 proofs, generated with a

null 6 Mar 4, 2024
apkeep - A command-line tool for downloading APK files from various sources

apkeep - A command-line tool for downloading APK files from various sources Installation Precompiled binaries for apkeep on various platforms can be d

Electronic Frontier Foundation 561 Dec 29, 2022
ufo2nft is a CLI Rust tool to automate creating on-chain SVG NFTs from UFO font sources

ufo2nft is a CLI Rust program created by Eli Heuer at the 2022 Seattle Solana Hacker House event. It uses Norad to create on-chain SVG images from UFO font sources, and prepares them for minting as Solana NFTs. For Ethereum NFTs the program can just export the SVGs and Ethereum NFTs can be built manually.

Eli Heuer 1 Feb 10, 2022
Provides a cross platform way to shut down, reboot or log out operations

@napi-rs/system-shutdown This package provides a cross platform way to shut down, reboot or log out operations. Supported platforms: Linux, Windows an

LongYinan 7 Nov 2, 2023
Simple but convenient CLI-based Matrix client app for sending and receiving (in Rust)

matrix-commander-rs simple but convenient CLI-based Matrix client app for sending and receiving Help create this Rust program! This project is current

null 19 Dec 30, 2022
simple but convenient CLI-based Nostr client app for following users and sending DMs

nostr-commander-rs TLDR: simple but convenient CLI-based Nostr client app for publishing, sending DMs, as well as following users and channels nostr-c

null 18 Dec 30, 2022
A convenient macro for building PathBufs in Rust.

pathbuf pathbuf is a simple crate which provides the pathbuf macro to conveniently construct the Rust PathBuf type. Example use pathbuf::pathbuf; use

Andrew Lilley Brinker 6 Jan 16, 2023
A simple yet convenient cross-platform ARP spoofer

ruuf - A simple yet convenient cross-platform ARP spoofer Poison the ARP cache of the given victim, thereby redirecting the traffic to the target ther

null 6 Feb 19, 2023
A more convenient `#[target_feature]` replacement

A more convenient #[target_feature] replacement To get good performance out of SIMD everything on the SIMD codepath must be inlined. With how SIMD is

Koute 3 Apr 10, 2023
FastSSH is a TUI that allows you to quickly connect to your services by navigating through your SSH config.

Connect quickly to your services ?? FastSSH is a TUI that allows you to quickly connect to your services by navigating through your SSH config. Instal

Julien 85 Dec 14, 2022
Most intuitive global cli maker. *(lazy_static + config-rs + clap)

argone Most intuitive global cli maker. *(lazy_static + config-rs + clap) | Examples | Docs | Latest Note | [dependencies] argone = "0.5" Phases Parsi

Doha Lee 6 Dec 9, 2022
⚙️ A curated list of static analysis (SAST) tools for all programming languages, config files, build tools, and more.

This repository lists static analysis tools for all programming languages, build tools, config files and more. The official website, analysis-tools.de

Analysis Tools 10.7k Jan 2, 2023
🕺 Run React code snippets/components from your command-line without config

Run React code snippets/components from your command-line without config.

Eliaz Bobadilla 11 Dec 30, 2022
🐙 Loads config and hosts for gh CLI in Rust.

gh-config-rs Loads config and hosts for gh CLI in Rust. Getting started [dependencies] gh-config = "0.2" Usage use std::error::Error; use gh_config::*

Naoki Ikeguchi 2 Jul 23, 2022
A zero-config leptos component to display markdown

A port of yew-markdown using leptos ! Usage You can use this component to render both static and dynamic markdown. Static markdown use leptos::*; {

Antonin Peronnet 4 Aug 4, 2023
Opinionated, zero-config linter for JavaScript monorepos

Sherif: Opinionated, zero-config linter for JavaScript monorepos About Sherif is an opinionated, zero-config linter for JavaScript monorepos. It runs

Tom Lienard 219 Oct 10, 2023