Crate salak[][src]

A configuration loader with zero-boilerplate configuration management.

About

salak is a rust version for multi-layered configuration loader inspired by spring-boot. salak also has a haskell version.

salak defines following default PropertySources:

  1. Command line arguments using clap to parsing -P, --propery KEY=VALUE.
  2. System Environment.
  3. app.toml(*) in current dir and $HOME dir. Or if you specify APP_CONF_DIR dir, then only load toml in this dir.

* APP_CONF_NAME can be specified to replace app.

Placeholder format

salak use format {key:default} to reference to other key, and if key not exists then use value default.

Key format

  1. a.b.c is a normal key separated by dot(.).
  2. a.b.0, a.b.1, a.b.2... is a group of keys with arrays.
  3. System environment key will be changed from HELLO_WORLD to hello.world, vice versa.

Auto derived parameters.

attribute default to set default value.
  1. #[salak(default="string")]
  2. #[salak(default=1)]
attribute disable_placeholder to disable placeholder parsing.
  1. #[salak(disable_placeholder)]
  2. #[salak(disable_placeholder = true)]

Quick Example

use salak::*;
#[derive(FromEnvironment, Debug)]
pub struct DatabaseConfig {
    url: String,
    #[salak(default = "salak")]
    name: String,
    #[salak(default = "{database.name}")]
    username: String,
    password: Option<String>,
    #[salak(default = "{Hello}", disable_placeholder)]
    description: String,
}

fn main() {
  std::env::set_var("database.url", "localhost:5432");
  let env = SalakBuilder::new()
     .with_default_args(auto_read_sys_args_param!())
     .build();
  
  match env.require::<DatabaseConfig>("database") {
      Ok(val) => println!("{:?}", val),
      Err(e) => println!("{}", e),
  }
}
// Output: DatabaseConfig {
//  url: "localhost:5432",
//  name: "salak",
//  username: "salak",
//  password: None,
//  description: "{Hello}"
// }

Modules

args

Provide command line arguments PropertySource.

env

Provide system environment PropertySource.

map

Provide hashmap PropertySource.

property

Property converter.

toml

Provide toml PropertySource.

Macros

auto_read_sys_args_param

Auto generate SysArgsParam from Cargo.toml.

Structs

EnvironmentOption

An option be used to add default values for some keys.

PlaceholderResolver

An implementation of Environment that can resolve placeholder for values.

Salak

A wrapper for Environment, which can hide the implementation details.

SalakBuilder

Salak builder.

SourceRegistry

An implementation of Environment for registering PropertySource.

Enums

Property

Unified property structure.

PropertyError

Property Error

Traits

Environment

An environment for getting properties in multiple PropertySources.

FromEnvironment

Generate object from Environment.

PropertySource

An abstract source loader from various sources, such as commandline arguments, system environment, files, etc.

Derive Macros

FromEnvironment

Auto derive FromEnvironment for struct.