#[confide] is a proc-macro for config structs: generates Default + Debug with serde defaults in one attribute.
use confide::confide;
use std::time::Duration;
#[confide]
pub struct MyConfig {
#[confide(default = 8080)]
pub port: u16,
#[confide(default_duration = "30s")]
pub timeout: Duration,
#[confide(default_bytes = "1 MiB")]
pub buffer_size: u64,
#[confide(default)]
pub mode: String,
#[confide(default = "127.0.0.1".to_string(), secret)]
pub bind_address: String,
}This generates Default, Debug, and per-field #[serde(default = ...)] so missing keys in config files are filled in automatically.
| Argument | Effect |
|---|---|
| (none) | Generates both Default and Debug |
no_default |
Suppresses impl Default |
no_debug |
Suppresses impl Debug |
Combine freely: #[confide(no_default, no_debug)].
Use the type's Default::default(). Adds #[serde(default)].
Use the given Rust expression. If the expression is a bare path (e.g. Vec::new), serde calls it directly. Otherwise a hidden helper function is generated.
#[confide(default = vec![1, 2, 3])]
pub allowed_ports: Vec<u16>,
#[confide(default = 42)]
pub retries: u32,Parse a humantime duration string at compile time. The field type should be std::time::Duration. The generated code also adds #[serde(with = "confide::humantime_serde")] so durations serialize as human-readable strings like "5m" / "2h".
#[confide(default_duration = "10s")]
pub heartbeat: Duration,Parse a bytesize string at compile time. The field should be an integer type (u64, u32, usize, etc.). Serialization produces IEC-formatted strings (e.g. "1 MiB"); deserialization accepts both raw integers and strings like "500 KB" / "2 GiB".
#[confide(default_bytes = "16 MiB")]
pub max_size: u64,Masks the field in Debug output (shows "***"). The field is still serialized/deserialized normally.
#[confide(default = "".to_string(), secret)]
pub api_key: String,Multiple annotations can be combined: #[confide(default_bytes = "1 MiB", secret)].
- Named fields only. Tuple structs and unit structs are not supported.
- Every field needs a default annotation unless you pass
no_defaultto the macro. Without it the generatedDefaultimpl will be incomplete. - The
serdecrate must be in scope (as a dependency of your crate) for#[serde(...)]attributes to resolve. - The
confidecrate must be in your dependencies for generated code referencesconfide::humantime_serdeandconfide::bytesize::ByteSizeat runtime.
Licensed under either of Apache License 2.0 or MIT license at your option.