Crates.io | autoconf |
lib.rs | autoconf |
version | 0.1.0 |
source | src |
created_at | 2024-04-26 19:55:24.599694 |
updated_at | 2024-04-26 19:55:24.599694 |
description | A simple and powerful derive macro for loading configuration structs from various sources |
homepage | https://github.com/yourusername/autoconf |
repository | https://github.com/N4D1K-lgtm/autoconf |
max_upload_size | |
id | 1221745 |
size | 23,054 |
autoconf
is a crate that enables easily managing rust application configuration by automatically deriving functionality to load settings from environment variables, configuration files, and default values. This is done by procedurally parsing struct fields to build environment variable keys as well as deserialization using serde
from a provided config file path. Functionality is customizable through several macro attribute helpers.
Attribute | Functionality |
---|---|
prefix | Sets the prefix for environment variables, can be set at the struct or field level. |
path | Specifies the path to the configuration file, the extension may be omitted. |
key | Overrides the default key name for an attribute, ignores the prefix and field name. |
nest | Necessary for non standard types, these must also derive Config |
skip | Skips loading the attribute from an environment variable. |
separator | Sets the separator character that is placed between the prefix and the field name, can be set at the struct or field level, default is "_" |
toml
, json
, yaml
, xml
, ini
, ron
or json5
configuration file with default trait implementations as a fall-back.smart_default
.Here's a complete example with all the currently implemented attributes. First define a configuration struct that the derive macro will fill from specified sources:
use autoconf::prelude::*;
use smart_default::SmartDefault;
#[derive(Config, Clone, SmartDefault)]
#[config(prefix = "PREFIX", path = "tests/config")]
pub struct Test {
#[config(key = "CUSTOM_KEY")]
#[default = "World"]
name: String,
#[config(prefix = "APP")]
#[default = 3]
id: i32,
#[config(nest)]
nested: Nested,
#[config(key = "TIMEOUT_MS")]
#[default = 1000]
timeout: u64,
#[config(key = "FEATURE_ENABLED")]
#[default = false]
feature_enabled: bool,
#[default = 1.5]
ratio: f64,
#[config(nest)]
metadata: Metadata,
#[config(skip)]
#[default(Some("Unused".to_string()))]
unused_field: Option<String>,
}
#[derive(Config, Default, Clone)]
pub struct Nested {
name: String,
}
#[derive(Config, Default, Clone)]
#[config(prefix = "META", separator = "__")]
pub struct Metadata {
description: String,
version: i32,
}
Then you can load your settings like so:
fn main() {
let settings = Test::config();
}
This is intended to easily be used inside of something like std::sync::OnceLock
I highly recommend checking out the
config
crate as it is a feature complete non-proc-macro alternative. This crate actually relies onconfig
for file parsing.