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_config
calls 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.