Git Product home page Git Product logo

layeredconf's Introduction

Rust Crates.io Crates.io docs.rs

LayeredConf

Yet Another Config Package

Future

Hopefully this one will be useful to someone. Incoming features:

  • More Documentation
  • Features

Features

  • Generate config Layers loaded from multiple sources: files, strings, command line arguments...
  • Uses Clap to auto-generate command line help + usage info
  • Most of Clap's derive features are usable
  • Can define futher config files to load within config files, or command line options

Quick Example

Add layeredconf, clap, and serde to your Cargo.toml

[dependencies]
layeredconf = "0.2.0"
clap = "3.0.0-beta.5"
serde = { version = "1.0", features = ["derive"] }

Define your config

use layeredconf::{Builder, Format, LayeredConf, Source};
use serde::Deserialize;

#[derive(LayeredConf, Deserialize)]
struct Config {
    /// Will also load this config file
    #[layered(load_config)]
    #[clap(long)]
    config: Option<std::path::PathBuf>,

    /// Required to be set in at least one Layer (config file, command line, etc.)
    #[clap(long)]
    name: String,

    /// Optional field
    #[clap(long)]
    input: Option<String>,

    /// Defaulted field
    #[layered(default)]
    #[clap(long)]
    number: u32,
}

fn main() -> anyhow::Result<()> {
    let config: Config = Builder)::new()
        .new_layer(Source::OptionalFile {
            path: "/etc/my_app/config.yaml",
            format: Format::Auto,
        })
        .new_layer(Source::File {
            path: "relative/config.yaml",
            format: Format::Auto,
        })
        .new_layer(Source::Arguments)
        .solidify()?;

    // Use config in your application
}

layeredconf's People

Contributors

gothack avatar

Stargazers

Davide Peressoni avatar

Watchers

 avatar James Cloos avatar  avatar

layeredconf's Issues

Strange errors from `LayeredConf`

I have this:

#[derive(LayeredConf, Deserialize, Serialize, Debug)] // line 13
struct Config {
    #[layered(load_config)]
    #[clap(long, short = 's', help = "The server to connect to", display_order = 0)]
    host: Option<String>,

    #[layered(load_config)]
    #[clap(long, short, help = "The port to use", display_order = 1)]
    port: Option<u16>,

    #[layered(load_config)]
    #[clap(
        long,
        short,
        conflicts_with = "account-number",
        help = "The account number to use",
        display_order = 2
    )]
    account: Option<String>,

    #[layered(load_config)]
    #[clap(
        long,
        short = 'n',
        conflicts_with = "account",
        help = "The account name to use",
        display_order = 3
    )]
    account_number: Option<usize>,
}

I get these errors:

error[E0308]: mismatched types
  --> examples/basic.rs:13:10
   |
13 | #[derive(LayeredConf, Deserialize, Serialize, Debug)]
   |          ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
   |          |
   |          expected struct `std::string::String`, found `u16`
   |
   = note: this error originates in the derive macro `LayeredConf` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> examples/basic.rs:13:10
   |
13 | #[derive(LayeredConf, Deserialize, Serialize, Debug)]
   |          ^^^^^^^^^^^- help: try using a conversion method: `.to_string()`
   |          |
   |          expected struct `std::string::String`, found `usize`
   |
   = note: this error originates in the derive macro `LayeredConf` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> examples/basic.rs:13:10
   |
13 | #[derive(LayeredConf, Deserialize, Serialize, Debug)]
   |          ^^^^^^^^^^^
   |          |
   |          expected struct `PathBuf`, found struct `std::string::String`
   |          expected `Vec<PathBuf>` because of return type
   |
   = note: expected struct `Vec<PathBuf>`
              found struct `Vec<std::string::String>`
   = note: this error originates in the derive macro `LayeredConf` (in Nightly builds, run with -Z macro-backtrace for more info)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.