A rust layered configuration loader with zero-boilerplate configuration management.

Overview

salak

A layered configuration loader with zero-boilerplate configuration management.

Crates.io Crates.io Documentation dependency status License Actions Status

  1. About
  2. Features
  3. Placeholder
  4. Key Convension
  5. Cargo Features
    1. Default features
    2. Optional features
  6. Quick Example

About

salak is a rust version of layered configuration loader inspired by spring-boot. salak provide an [Environment] structure which load properties from various [PropertySource]s. Any structure which impmement [FromEnvironment] can get from [Environment] by a key. Feature enable_derive provide rust attributes for auto derive [FromEnvironment].

Features

Below are a few of the features which salak supports.

  • Auto mapping properties into configuration struct.
    • #[salak(default="value")] set default value.
    • #[salak(name="key")] rename property key.
    • #[salak(prefix="salak.database")] set prefix.
  • ** Supports load properties from various sources **
    • Support following random property key.
      • random.u8
      • random.u16
      • random.u32
      • random.i8
      • random.i16
      • random.i32
      • random.i64
    • Load properties from command line arguments.
    • Load properties from system environment.
    • Load properties from toml config file.
    • Load properties from yaml config file.
    • Easy to add a new property source.
  • Supports profile(develop/production) based configuration.
  • Supports placeholder resolve.
  • Supports reload configurations.
  • Supports factory builder.

Placeholder

  • ${key:default} means get value of key, if not exists then return default.
  • ${key} means get value of key, if not exists then return PropertyError::NotFound(_).
  • \$\{key\} means escape to ${key}.

Key Convension

  • a.b.c is a normal key separated by dot(.).
  • a.b[0], a.b[1], a.b[2]... is a group of keys with arrays.

Cargo Features

Default features

  1. enable_log, enable log record if enabled.
  2. enable_toml, enable toml support.
  3. enable_derive, enable auto derive [FromEnvironment] for struts.

Optional features

  1. enable_clap, enable default command line arguments parsing by clap.
  2. enable_yaml, enable yaml support.
  3. enable_rand, enable random value support.

Quick Example

use salak::*;

#[derive(FromEnvironment, Debug)]
pub struct SslConfig {
    key: String,
    pem: String,
}

#[derive(FromEnvironment, Debug)]
#[salak(prefix = "database")]
pub struct DatabaseConfig {
  url: String,
  #[salak(default = "salak")]
  username: String,
  password: Option<String>,
  description: String,
  #[salak(name="ssl")]
  ssl_config: Option<SslConfig>,  
}

std::env::set_var("database.url", "localhost:5432");
std::env::set_var("database.description", "\\$\\{Hello\\}");
let env = Salak::new()
   .with_default_args(auto_read_sys_args_param!()) // This line need enable feature `enable_clap`.
   .build();

match env.load_config::<DatabaseConfig>() {
    Ok(val) => println!("{:?}", val),
    Err(e) => println!("{}", e),
}

// Output: DatabaseConfig {
//  url: "localhost:5432",
//  username: "salak",
//  password: None,
//  description: "${Hello}",
//  ssl_config: None,
// }
You might also like...
A library to listen to global hotkeys in Rust

Rust Hotkey A library to listen to global hotkeys in Rust How to use See the examples folder for how to use this library. OS Support This lib aims to

๐Ÿ”ฎ Futuristic take on hexdump, made in Rust.
๐Ÿ”ฎ Futuristic take on hexdump, made in Rust.

hex (hx) Futuristic take on hexdump. hx accepts a file path as input and outputs a hexadecimal colorized view to stdout. $ hx tests/files/alphanumeric

Cross-platform Rust rewrite of the GNU coreutils

uutils coreutils uutils is an attempt at writing universal (as in cross-platform) CLI utilities in Rust. This repository is intended to aggregate GNU

A TUI system monitor written in Rust
A TUI system monitor written in Rust

NO LONGER MAINTAINED. For a similar program, check out https://github.com/ClementTsang/bottom. ytop Another TUI based system monitor, this time in Rus

Untrusted IPC with maximum performance and minimum latency. On Rust, on Linux.

Untrusted IPC with maximum performance and minimum latency. On Rust, on Linux. When is this Rust crate useful? Performance or latency is crucial, and

An over-engineered rewrite of pipes.sh in Rust
An over-engineered rewrite of pipes.sh in Rust

pipes-rs An over-engineered rewrite of pipes.sh in Rust Installlation macOS Install using Homebrew or download manually from releases. $ brew install

Trup-rewrite in rust! Finally!

Trup, but Rust! A Discord bot for the Unixporn community Now written in a good language! Dependencies Rust nightly sqlx-cli (if you need to change the

A collection of small Rust programs for doing weird things

This is a repo of small programs, proof of concepts, or templates written in Rust that relate in some way to hacking and/or CTF. I think Rust is real

๐Ÿ‚ A Rust-based simulated DOM (browser-independent replacement for web_sys)
๐Ÿ‚ A Rust-based simulated DOM (browser-independent replacement for web_sys)

DOM in Rust without a browser Hello and welcome. This library provides server-side or browserless simulation of a DOM. Example Usage use std::sync::Ar

Comments
  • Support other delimiter than dot(.) like _

    Support other delimiter than dot(.) like _

    Hi,

    Setting an environment variable with a dot in it is sometimes impossible. This thread illustrates the issue: https://unix.stackexchange.com/questions/93532/exporting-a-variable-with-dot-in-it

    I think it would be nice to be able to configure the separator. Maybe in the builder?

    I imagine you used the dot to be easily compliant with the rust variable that might contain underscore. This can be handled with #[salak(name="...")] and any string that does not contain the separator.

    documentation 
    opened by SWHes 3
Releases(v0.12.0)
Owner
Daniel YU
GPG: 0x120152685ACE7222
Daniel YU
A customisable client for Discord rich presence using simple Lua configuration.

Installation | Usage | Configuration | Example Disco Disco is a customisable client for Discord rich presence using simple Lua configuration. Installa

KaitlynEthylia 5 Aug 7, 2023
minimalistic command launcher in rust

rrun Note: Apart from the occasional fix, this project is not actively developed anymore. rrun works fine and should run/compile for the time being on

null 105 Nov 18, 2022
Yet another fancy watcher. (Rust)

funzzy Yet another fancy watcher. (Inspired by antr / entr) Configure execution of different commands using semantic yaml. # .watch.yaml # list here a

Cristian Oliveira 188 Dec 12, 2022
A modern replacement for ps written in Rust

procs procs is a replacement for ps written in Rust. Documentation quick links Features Platform Installation Usage Configuration Features Output by t

null 3.6k Jan 5, 2023
A more intuitive version of du in rust

Dust du + rust = dust. Like du but more intuitive. Why Because I want an easy way to see where my disk is being used. Demo Install Cargo cargo install

andy.boot 5.5k Jan 8, 2023
Blazing ๐Ÿ’ฅ fast terminal-ui for git written in rust ๐Ÿฆ€

Blazing fast terminal client for git written in Rust Features Fast and intuitive keyboard only control Context based help (no need to memorize tons of

Stephan Dilly 11.8k Jan 5, 2023
A simple and fast download accelerator, written in Rust

zou A simple and fast download accelerator, written in Rust Zou is a Snatch fork by @k0pernicus. Snatch is a fast and interruptable download accelerat

Antonin Carette 173 Dec 4, 2022
Fuzzy Finder in rust!

Life is short, skim! Half of our life is spent on navigation: files, lines, commandsโ€ฆ You need skim! It is a general fuzzy finder that saves you time.

Jinzhou Zhang 3.7k Jan 4, 2023
A bash-like Unix shell written in Rust

Cicada Unix Shell Cicada is a simple Unix shell written in Rust. Documents Install cicada Environment Variables Cicada Builtins Completion RC File His

Hugo Wang 921 Dec 28, 2022
Performs distributed command execution, written in Rust w/ Tokio

Concurr: Distributed and Concurrent Command Execution, in Rust This project is dual licensed under MIT and Apache 2.0. Originally inspired by the GNU

Michael Murphy 93 Dec 18, 2022