Crates.io | confgr |
lib.rs | confgr |
version | 0.2.1 |
source | src |
created_at | 2024-04-27 17:16:24.445653 |
updated_at | 2024-05-04 17:25:08.429739 |
description | A simple rust application configuration derive macro. |
homepage | https://github.com/N4D1K-lgtm/confgr |
repository | https://github.com/N4D1K-lgtm/confgr |
max_upload_size | |
id | 1222773 |
size | 46,531 |
The Config
derive macro simplifies application configuration by automatically loading
settings from sources in the following order:
toml
, json
, yaml
, ini
, ron
, json5
).toml
, json
, yaml
, ini
, ron
, and json5
.smart_default
.There are also several useful helper attributes for customizing the behavior of the derive macro.
Attribute | Functionality |
---|---|
prefix |
Sets a prefix for environment variables. Can be applied at the struct or field level. |
path |
Specifies the static path to a configuration file. The file extension may (though probably shouldn't) be omitted. |
env_path |
Resolves an environment variable at runtime to determine the configuration file path. |
default_path |
Specifies a fallback path used if the path determined by env_path does not exist. |
key |
Overrides the default environment variable name. This ignores the prefix and uses the provided key directly. |
name |
forwards to #[serde(rename = "_")] to rename fields during serialization/deserialization. It does not affect environment variable names. |
nest |
Required for non-standard types which must also derive Config , used for nesting configuration structs. |
skip |
Skips loading the attribute from an environment variable. Necessary for types that don't implement FromStr but are present in the configuration file. |
separator |
Specifies a character to separate the prefix and the field name. The default separator is "_". |
env_path
: Resolves the provided environment variable into a config filepath. This
takes precedence over path
and default_path
, but will not panic if the file or environment
does not exist.path
: Directly sets the path to the configuration file. When set, default_path
may not be used. Panics if the file does not exist.default_path
: Identical to path
, but does not panic if the file does not exist.serde
is a required dependency.
[dependencies]
confgr = "0.2.0"
serde = { version = "1.0", features = ["derive"] }
Then define your configuration like so:
use confgr::prelude::*;
#[derive(Config)]
#[config(path = "docs.toml", prefix = "APP")]
pub struct AppConfig {
port: u32,
address: String,
#[config(key = "DEBUG_MODE")]
debug: bool,
}
// Default implementation is required.
impl Default for AppConfig {
fn default() -> Self {
Self {
port: 3000,
address: "127.0.0.1".to_string(),
debug: false
}
}
}
std::env::set_var("APP_PORT", "4000");
std::env::set_var("DEBUG_MODE", "true");
let settings = AppConfig::load_config();
assert_eq!(settings.port, 4000);
assert_eq!(settings.address, "127.0.0.1");
assert!(settings.debug)
Check out the examples directory for more.
path
attributes. If
you would like multiple files to be loaded, you must use multiple structs with multiple
load_config()
calls. This may change in a future version.FromStr
must use #[config(skip)]
or #[config(nest)]
.separator
character is only inserted between the prefix and the field name, not in any
part of the parsed field name.prefix
is applied per field or for the entire struct, but is ignored if #[config(key = "_")]
is used.Default
.Deserialize
,
Clone
, and
Debug
.Option
is not currently compatible with #[config(nest)]
on types that implement Confgr
.When encountering issues using the macro, the following methods may be of use.
The get_env_keys()
method can be used to retrieve the
environment variable key names built by the derive macro.
use std::collections::HashMap;
use confgr::prelude::*;
#[derive(Config, Default)]
#[config(prefix = "APP")]
pub struct AppConfig {
port: u32,
#[config(separator = "__")]
address: String,
#[config(key = "DEBUG_MODE")]
debug: bool,
}
let keys: HashMap<String, String> = AppConfig::get_env_keys();
assert_eq!(keys["port"], "APP_PORT");
assert_eq!(keys["address"], "APP__ADDRESS");
assert_eq!(keys["debug"], "DEBUG_MODE");
You can use check_file()
to ensure that the configuration file
is accessible using the provided path
, path_env
or default_path
attributes.
use confgr::prelude::*;
#[derive(Config, Default)]
#[config(path = "docs.toml", env_path = "APP_CONFIG_FILE")]
pub struct AppConfig {
port: u32,
debug: bool,
}
std::env::set_var("APP_CONFIG_FILE", "env_config.toml");
AppConfig::check_file().expect("Failed to open configuration file.");
std::env::remove_var("APP_CONFIG_FILE");
AppConfig::check_file().expect("Failed to open configuration file.");
The deserialize_from_file()
method can be used to manually test the config deserialization step. Returns
a Result<Self::Layer, ConfgrError>
.
use confgr::prelude::\*;
#[derive(Config, Default)] #[config(path = "docs.toml")]
pub struct AppConfig {
port: u32,
debug: bool,
}
let config = AppConfig::deserialize_from_file().expect("Failed to deserialize configuration.");
println!("Deserialized configuration: {:?}", config);
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.