#configuration #loader #env

macro metre-macros

Macros for the metre crate

9 releases

0.2.6 Jul 28, 2024
0.2.5 Jul 23, 2024
0.2.2 May 31, 2024
0.1.1 May 30, 2024

#923 in Configuration

Download history 112/week @ 2024-07-20 174/week @ 2024-07-27 2/week @ 2024-08-03 3/week @ 2024-08-31 40/week @ 2024-09-14 38/week @ 2024-09-21 15/week @ 2024-09-28

695 downloads per month
Used in 2 crates

Apache-2.0

22KB
516 lines

metre: The rust configuration loader

AKA: #[derive(Config)]

crates.io build unit-tests integration-tests docs

What is it?

metre is a configuration loader for Rust that allows you to load configurations from a variety of formats such as toml, json, jsonc and yaml

It also supports a variety of sources such as program defaults, env variables, files, and urls.

Usage

use metre::{Config, ConfigLoader, Format};

#[derive(Config)]
struct MyConfig {
  #[config(default = 8000)]
  port: u16,
  foo: String
}

let mut loader = ConfigLoader::<MyConfig>::new();

loader.defaults()?;
loader.file("config.json", Format::Json)?;
loader.env()?;

// config have the type MyConfig here
let config = loader.finish()?;  

Focus

metre focus is to provide a declarative and type-safe way to load configurations in Rust.

How?

metre works by defining a struct that implements the Config trait, usually via the #[derive(Config)] macro.

Under the hood metre creates deep partial version of the struct to accumulate the configuration from different sources.

Once all the configuration is accumulated, you can access the final configuration as the defined struct. If the sum of all sources does not comply with the required properties, metre will return an error.

Install

cargo add metre

Docs

https://docs.rs/metre

Dependencies

~3–4.5MB
~81K SLoC