Crates.io | declarative-env |
lib.rs | declarative-env |
version | 0.1.0 |
created_at | 2025-09-11 18:55:35.646856+00 |
updated_at | 2025-09-11 18:55:35.646856+00 |
description | This crate parses and populates a struct with the environmental variables defined in a file, for having the documentation and the code always synchronized. |
homepage | |
repository | https://github.com/asperan/declarative-env |
max_upload_size | |
id | 1834362 |
size | 42,362 |
This crate contains a macro for generating a struct which reads the environmental variables defined in the configured file.
This allows to have a single point of definition for the env configuration, thus avoiding a possible incoherence between the documentation and the implementation.
The macro must be attached to an empty struct definition:
#[declarative_env!(path = "./example.hjson", format = "hjson")]
struct MyConfig;
Note: the struct can have any visibility.
The macro creates a struct with the same name of the empty struct, but it injects as fields all the variables defined in the path
keyword.
For example, the definition file:
SERVER_PORT: {
type: u16
description: The port the server will listen on
default: 8080
}
generates the struct
struct MyConfig {
SERVER_PORT: u16,
}
The path of the definition file is relative to the cargo manifest dir (the one with
Cargo.toml
).
The supported formats are specified in the AcceptedFormat
enum; formats are the lowercase version of the enum variants, so AcceptedFormat::Hjson
is selected with format = "hjson"
.
The env variable definition file contains a map of objects, where the key is the name of the env variable, and the value contains the metadata of the variable:
AcceptedRustType
).Supported formats are listed in the AcceptedFormat enum.
Types variables can be are listed in the AcceptedRustType enum.
This project is distributed according to the Mozilla Public License 2.0
.