Crate metre

Source
Expand description

§metre. The configuration loader for Rust.

§AKA: The #[derive(Config)] macro

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.
   

§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.    

§Show me the code

The following code shows how to load configurations from different sources and in different formats with descriptions for most macro attributes

To see all macro attributes available see the Config derive macro documentation.

use metre::{Config, ConfigLoader, Format};
 
#[derive(Config)]
struct MyConfig {
  #[config(default = 3000)]
  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, or loader.finish() returns an error
let config = loader.finish()?;
 

Re-exports§

pub use error::Error;

Modules§

error
List of errors that can happen during the config loading process
merge
Utility functions to use with #[config(merge)] attribute
parse
Utility functions to use with #[config(parse_env)] attribute

Structs§

ConfigLoader
The configuration loader
StdEnvenv
An implementation of EnvProvider that reads from the standard library’s std::env::var

Enums§

Format
List of known configuration formats
LoadLocation
A location from where a configuration was loaded

Traits§

Config
The Config trait that is implemented from the Config derive macro
EnvProvider
Implement this trait if you want to load a configuration from custom environment variables that are not in std::env::var
PartialConfig
The partial configuration trait that is automatically implemented by the Config derive macro.

Derive Macros§

Configderive
Derive macro for Config trait