Einstellung
Einstellung is a flexible configuration parser for Rust based on serde. It
allows you to define your application's configuration securely and ergonomically
using strongly-typed structs.
By providing a #[derive(Config)] macro, einstellung automatically
generates the necessary boilerplate to parse, validate, and merge configurations
from multiple sources—including JSON, TOML, YAML, and hardcoded defaults—into a
single, cohesive application state.
Overview
- Strongly Typed: Define your configuration using standard Rust structs and enums.
- Layered Configurations: Merge configurations from multiple layers, such as hardcoded defaults, global files, and user-specific overrides.
- Format Agnostic: Flexible storage providers backed by
serde. Built in support for JSON, TOML, and YAML. - Granular Merging: Choose to extend collections (like
HashSetorVec), replace fields entirely, or write custom merge logic. - Freezable Fields: Lock specific configuration layers to prevent downstream overrides.
- Validation: Run custom validation logic on fields during the build process to ensure data integrity.
Installation
Add einstellung to your Cargo.toml:
[]
= "0.1.1"
Feature Flags
You can customize enabled features to reduce compilation time or binary size:
derive(default): Enables the#[derive(Config)]macro.json(default): EnablesJsonFileProvider.toml(default): EnablesTomlFileProvider.yaml(default): EnablesYamlFileProvider.full(default): Enables all format providers and the derive macro.
Examples
Simple Configuration
Loading a complete configuration from a single YAML file.
use IpAddr;
use ;
Layered & Frozen Configuration
Combining hardcoded defaults with external files while protecting specific fields.
use ;
Layering Features
The core power of einstellung lies in its partial configuration system. When
you derive Config, the macro generates a companion "Partial" struct where all
fields are optional.
.merge(): Combines two partial configurations. By default, values in the "newer" layer overwrite the "older" layer.merge = "extend": Instead of overwriting, this strategy uses theExtendtrait to combine collections likeVecorBTreeSet..freeze(): Marks a partial configuration as frozen. Any fields tagged with#[config(freezable)]in a frozen layer cannot be modified by subsequent merges.
Customizability
- Validation: Use
#[config(validate = path::to::func)]to ensure fields meet specific criteria before the final config is built. - Custom Merging: Implement custom merge logic via
#[config(merge(function = "path"))]. - Serde Forwarding: Attributes like
#[config(serde(rename = "..."))]oraliasare forwarded to the generated partial structs to maintain consistent naming across formats. - Subconfigs: Nest
Configstructs using the#[config(subconfig)]attribute to keep your data organized.
Documentation
- Main Crate Documentation: Visit the
einstellung docs for detailed information on
the
Config,PartialConfig, andConfigProvidertraits. - Derive Macro Reference: See the
einstellung_derive docs for a full list
of supported
#[config(...)]attributes.
Contributing
Please feel free to open an Issue or submit a PR at https://github.com/soruh/einstellung.