• Stars
    star
    156
  • Rank 239,000 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Type-safe, layered, light-weight, `serde`-based configuration library

Confique: type-safe, layered configuration library

CI status of main Crates.io Version docs.rs

Confique is a rather light-weight library that helps with configuration management in a type-safe and DRY (don't repeat yourself) fashion.

Features:

  • Type safe: the code using the config values does not need to parse strings or unwrap any Options. All values already have the correct type.
  • Layered configuration: you can load from and then merge multiple sources of configuration.
  • Load config values from:
    • Environment variables
    • Files: TOML, YAML, and JSON5
    • Anything with a serde Deserializer
  • Based on serde: less code in confique (more light-weight) and access to a huge ecosystem of high quality parsers.
  • Easily generate configuration "templates": describe all available config values to your users without repeating yourself.

Simple example

use std::{net::IpAddr, path::PathBuf};
use confique::Config;


#[derive(Config)]
struct Conf {
    /// Port to listen on.
    #[config(env = "PORT", default = 8080)]
    port: u16,

    /// Bind address.
    #[config(default = "127.0.0.1")]
    address: IpAddr,

    #[config(nested)]
    log: LogConf,
}

#[derive(Config)]
struct LogConf {
    #[config(default = true)]
    stdout: bool,

    file: Option<PathBuf>,

    #[config(default = ["debug"])]
    ignored_modules: Vec<String>,
}


let config = Conf::builder()
    .env()
    .file("example-app.toml")
    .file("/etc/example-app/config.toml")
    .load()?;

See the documentation for more information.

Configuration Template

With the above example, you can automatically generate a configuration template: a file in a chosen format that lists all values with their description, default values, and env values.

toml::template::<Conf>() yaml::template::<Conf>() json5::template::<Conf>()
# Port to listen on.
#
# Can also be specified via
# environment variable `PORT`.
#
# Default value: 8080
#port = 8080

# Bind address.
#
# Default value: "127.0.0.1"
#address = "127.0.0.1"

[log]
# <omitted>
# Port to listen on.
#
# Can also be specified via
# environment variable `PORT`.
#
# Default value: 8080
#port: 8080

# Bind address.
#
# Default value: 127.0.0.1
#address: 127.0.0.1

log:
  # <omitted>
{
  // Port to listen on.
  //
  // Can also be specified via
  // environment variable `PORT`.
  //
  // Default value: 8080
  //port: 8080,

  // Bind address.
  //
  // Default value: "127.0.0.1"
  //address: "127.0.0.1",

  log: {
    // <omitted>
  },
}

(Note: The "environment variable" sentence is on a single line; I just split it into two lines for readability in this README.)

Comparison with other libraries/solutions

config

  • Loosely typed:
    • You access configuration values via string path (e.g. "http.port") and deserialize at "use site".
    • No defined schema
  • More features
  • Larger library
  • If you need a "config template", you need to repeat code/docs

figment

  • Also based on serde and also uses your own structs as data store, thus type safe
  • Instead of using partial types, aggregates different layers in a dynamic data store
  • If you need a "config template", you need to repeat code/docs

Just serde?

Serde is not a configuration, but a deserialization library. But you can get surprisingly far with just serde and it might actually be sufficient for your project. However, once you want to load from multiple sources, you either have make all your fields Option or repeat code/docs. With confique you also get some other handy helpers.

Status of this project

There is still some design space to explore and there are certainly still many features one could add. However, the core interface (the derive macro and the core traits) probably won't change a lot anymore. Confique is used by a web project (that's already used in production) which I'm developing alongside of confique.



License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

programmieren-in-rust

Course page „Programmieren in Rust“, University Osnabrück (German!)
Rust
266
star
2

bunt

Simple macros to write colored and formatted text to a terminal. Based on `termcolor`, thus also cross-platform.
Rust
219
star
3

libtest-mimic

A small test framework to write your own test harness that looks and behaves like the built-in test harness used by `rustc --test`
Rust
92
star
4

term-painter

Cross-platform Rust library for coloring and formatting terminal output
Rust
78
star
5

lox

Fast polygon mesh library with different data structures and traits to abstract over those.
Rust
71
star
6

litrs

Parsing and inspecting Rust literals (particularly useful for proc macros)
Rust
41
star
7

atomig

Generic and convenient `std` atomics via `Atomic<T>`
Rust
36
star
8

penguin

Dev server with live-reloading, a file server, proxy support, and more. Language and framework agnostic. 🐧
Rust
32
star
9

reinda

Easily embed and manage assets for your web application to build standalone-executables. Offers filename hashing, templating and more.
Rust
28
star
10

mahboi

Yet another Game Boy emulator
Rust
19
star
11

stable-vec

A Vec-like collection which guarantees stable indices and features O(1) deletion of elements
Rust
14
star
12

floof

Language-agnostic, simple-to-use development server, file-watcher and tiny task runner mainly useful for webdev.
Rust
11
star
13

beaver

Simulating Turing machines for the Busy Beaver game
Rust
11
star
14

lumina

(Not maintained anymore!) Lumina Graphics Library
C++
10
star
15

lina

Strongly typed linear algebra library with a focus on 3D applications like games (i.e. low-dimensional vectors and matrices)
Rust
9
star
16

cantucci

🍞 Fractal Renderer
Rust
7
star
17

LukasKalbertodt.github.io

Personal Github Page
SCSS
4
star
18

splop

Helper functions to determine the first/last repetition of something
Rust
3
star
19

my-albert-extensions

My personal Albert extensions
Python
3
star
20

gnome-snazzy

Snazzy color scheme for Gnome-Terminal
Shell
3
star
21

drencher

Rust implementation of and solver for http://flashbynight.com/drench/
Rust
3
star
22

masters-thesis

My master's thesis about writing the polygon mesh library `lox` in Rust
TeX
3
star
23

leer

A tiny trait to abstract over types that have a notion of “being empty”
Rust
2
star
24

domsl

Abandoned experiment
Rust
2
star
25

mauzi

Experimental i18n library based on proc_macros (not actively maintained anymore)
Rust
2
star
26

srgb-test

Rust
1
star
27

xswag-syntax-java

(Not maintained anymore) Java Parser in Rust (library for xswag)
Rust
1
star
28

psymagen

Shell
1
star
29

novice-tools

Small and easy functions for programming beginners in Rust
Rust
1
star
30

powerpoint-rust-tools

Horribly written PowerPoint Add-In to help with Rust code on slides
C#
1
star
31

seek-ext

[Deprecated] Convenience Methods for `io::Seek`
Rust
1
star
32

sim

Implementation of the Sim pencil game
Rust
1
star
33

luten

(Not maintained anymore!)
Rust
1
star
34

miniql

Minimal graphql server test app
Rust
1
star