Crates.io | cruct_proc |
lib.rs | cruct_proc |
version | 1.0.0 |
created_at | 2025-07-11 21:52:28.6445+00 |
updated_at | 2025-07-11 21:52:28.6445+00 |
description | Procedural macro for Cruct, enabling compile-time configuration file parsing and validation |
homepage | |
repository | https://github.com/FlakySL/cruct |
max_upload_size | |
id | 1748521 |
size | 41,177 |
A procedural macro for loading configuration files into Rust structs with compileātime validation and type safety.
Add to your Cargo.toml
:
[dependencies]
cruct = "1.0.0"
Enable only the formats you need:
[dependencies.cruct]
version = "1.0.0"
default-features = false
features = ["toml", "json"] # only TOML and JSON support
Annotate your configāstruct with #[cruct]
, pointing at one or more sources:
use cruct::cruct;
#[cruct(load_config(path = "config/settings.toml"))]
struct AppConfig {
#[field(default = 8080)]
http_port: u16,
database_url: String,
}
fn main() -> Result<(), cruct_shared::ParserError> {
let cfg = AppConfig::loader()
.with_config()
.load()?;
println!("Listening on port {}", cfg.http_port);
Ok(())
}
Often you want a default in your file, but allow ops to override via env vars. For example, given tests/fixtures/test_config.toml
:
http_port = 8080
You can override http_port
at runtime:
use cruct::cruct;
#[cruct(load_config(path = "tests/fixtures/test_config.toml"))]
#[derive(Debug, PartialEq)]
struct TestEnv {
#[field(env_override = "TEST_HTTP_PORT")]
http_port: u16,
}
fn main() {
// Simulate setting the env var:
unsafe { std::env::set_var("TEST_HTTP_PORT", "9999"); }
let config = TestEnv::loader()
.with_config()
.load()
.unwrap();
assert_eq!(config.http_port, 9999);
println!("Overridden port: {}", config.http_port);
}
This pattern is drawn directly from our endātoāend tests.
load_config
calls with explicit priority
#[field(name = "HTTP_PORT", insensitive = true)]
default
See the full API docs for details on all options.
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.