
Cruct
A procedural macro for loading configuration files into Rust structs with compile‑time validation and type safety.
Table of Contents 📖
Features 👀
- Multi‑format support: TOML, YAML, JSON (via Cargo feature flags)
- Merge & override: CLI args, environment variables, config files, defaults
- Compile‑time safety: Missing or mismatched fields become compile or runtime errors
- Nested structures: Automatically derive for nested custom types
Installation 📦
Add to your Cargo.toml:
[]
= "1.0.0"
Enable only the formats you need:
[]
= "1.0.0"
= false
= ["toml", "json"] # only TOML and JSON support
Basic Usage 🔍
Annotate your config‐struct with #[cruct], pointing at one or more sources:
use cruct;
Use‑Case: Environment‑Variable Override 💡
Often you want a default in your file, but allow ops to override via env vars. For example, given tests/fixtures/test_config.toml:
= 8080
You can override http_port at runtime:
use cruct;
This pattern is drawn directly from our end‑to‑end tests.
Advanced 🥷
- Multiple files & priority: Chain
load_configcalls with explicitpriority - Case‑insensitive keys: Use
#[field(name = "HTTP_PORT", insensitive = true)] - Default values: Supply literals, expressions, or functions for
default
See the full API docs for details on all options.
License 📜
This repository is dual licensed, TLDR. If your repository is open source, the library is free of use, otherwise contact licensing@flaky.es for a custom license for your use case.
For more information read the license file.