1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! # Configulator
//!
//! A simple configuration manager for Rust applications with derive macro support.
//!
//! Supports configuration from multiple sources with clear precedence:
//!
//! 1. **Default values** (lowest priority)
//! 2. **Config files** (any serde format via [`serde_loader`])
//! 3. **Environment variables**
//! 4. **CLI flags** (highest priority)
//!
//! ## Features
//!
//! - `#[derive(Config)]` macro for declarative configuration structs
//! - Any serde-compatible file format - YAML, TOML, JSON, with a one-liner
//! - Pluggable file format support - bring your own parser via [`FileLoader`]
//! - Nested struct support
//! - [`Vec<T>`](Vec) list fields
//! - Custom types - anything implementing [`FromStr`](std::str::FromStr) + [`Default`]
//! - Optional validation via the [`Validate`] trait
//! - Boolean CLI flags (`--debug` sets true, `--debug false` sets false)
//!
//! ## Usage
//!
//! Add to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! configulator-rs = "0.1"
//! ```
//!
//! > **Note:** Because the configuration options are expressed as different cases
//! > (i.e. `http.host` in a config file would be `HTTP__HOST` in environment
//! > variables), this library cannot be used for configurations that contain the
//! > same field name in different cases.
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use configulator::{Config, Configulator, Validate};
//!
//! #[derive(Config, Default, Debug)]
//! struct AppConfig {
//! #[configulator(name = "host", default = "127.0.0.1", description = "Bind address")]
//! host: String,
//!
//! #[configulator(name = "port", default = "8080", description = "Listen port")]
//! port: u16,
//!
//! #[configulator(name = "debug", default = "false", description = "Enable debug mode")]
//! debug: bool,
//! }
//!
//! fn main() {
//! let config: AppConfig = Configulator::new()
//! .load_without_validation()
//! .expect("failed to load config");
//! println!("{config:?}");
//! }
//! ```
//!
//! ## Configuration Sources
//!
//! Enable as many or as few sources as you need via the builder:
//!
//! ```rust,no_run
//! use configulator::{
//! CLIFlagOptions, Config, Configulator,
//! EnvironmentVariableOptions, FileOptions, Validate,
//! serde_loader,
//! };
//!
//! #[derive(Config, Default, Debug)]
//! struct AppConfig {
//! #[configulator(name = "host", default = "127.0.0.1", description = "Bind address")]
//! host: String,
//!
//! #[configulator(name = "port", default = "8080", description = "Listen port")]
//! port: u16,
//!
//! #[configulator(name = "debug", default = "false", description = "Enable debug mode")]
//! debug: bool,
//!
//! #[configulator(name = "allowed-origins", default = "localhost,example.com")]
//! allowed_origins: Vec<String>,
//!
//! #[configulator(name = "database")]
//! database: DatabaseConfig,
//! }
//!
//! #[derive(Config, Default, Debug)]
//! struct DatabaseConfig {
//! #[configulator(name = "url", default = "postgres://localhost/mydb")]
//! url: String,
//!
//! #[configulator(name = "max-connections", default = "10")]
//! max_connections: u32,
//! }
//!
//! impl Validate for AppConfig {
//! fn validate(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! if self.port == 0 {
//! return Err("port must be non-zero".into());
//! }
//! Ok(())
//! }
//! }
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = Configulator::<AppConfig>::new()
//! .with_file(FileOptions {
//! paths: vec!["config.yaml".into(), "/etc/myapp/config.yaml".into()],
//! error_if_not_found: false,
//! // Any serde-compatible format works: serde_json, toml, etc.
//! loader: serde_loader(|s| serde_yaml_ng::from_str(s)),
//! })
//! // Env vars: MYAPP__HOST, MYAPP__DATABASE__MAX_CONNECTIONS, etc.
//! .with_environment_variables(EnvironmentVariableOptions {
//! prefix: "MYAPP".into(),
//! separator: "__".into(),
//! })
//! // CLI flags: --host, --database.url, --debug, etc.
//! .with_cli_flags(CLIFlagOptions {
//! separator: ".".into(),
//! })
//! // .load() validates; use .load_without_validation() to skip
//! .load()?;
//!
//! println!("Host: {}", config.host);
//! println!("Database URL: {}", config.database.url);
//! Ok(())
//! }
//! ```
//!
//! ## Derive Attributes
//!
//! Fields are annotated with `#[configulator(...)]` using these keys:
//!
//! | Key | Description |
//! |---------------|------------------------------------------------|
//! | `name` | Config key name (defaults to the field name) |
//! | `default` | Default value as a string literal |
//! | `description` | Help text shown in CLI `--help` output |
//!
//! ```rust,ignore
//! // Field appears in config files, env vars, and CLI flags as "my-name".
//! #[configulator(name = "my-name")]
//! my_name: String,
//!
//! // Field has a description shown in CLI --help
//! #[configulator(name = "my-name", description = "this text appears in --help")]
//! my_name: String,
//!
//! // Field has a default value of 1
//! #[configulator(name = "my-name", default = "1")]
//! my_name: u32,
//! ```
//!
//! ## Supported Types
//!
//! - All primitive scalars (`i8`–`i64`, `u8`–`u64`, `f32`, `f64`, `bool`, [`String`])
//! - [`PathBuf`](std::path::PathBuf) and any other [`FromStr`](std::str::FromStr) + [`Default`] type
//! - Custom enums (implement [`FromStr`](std::str::FromStr) + [`Default`])
//! - [`Vec<T>`](Vec) for list values (comma-separated defaults, repeated CLI flags)
//! - Nested structs (must also derive `Config`)
//!
//! ## Configuration Sources
//!
//! ### Config Files
//!
//! Configulator is format-agnostic, pass any serde-compatible deserializer via
//! [`serde_loader`], or implement the [`FileLoader`] trait for full control.
//! YAML, TOML, JSON, and any other serde format work out of the box.
//!
//! Provide a list of paths to search. The first file found is used.
//!
//! ```rust,ignore
//! // YAML
//! .with_file(FileOptions {
//! paths: vec!["config.yaml".into()],
//! error_if_not_found: false,
//! loader: serde_loader(|s| serde_yaml_ng::from_str(s)),
//! })
//!
//! // TOML
//! .with_file(FileOptions {
//! paths: vec!["config.toml".into()],
//! error_if_not_found: false,
//! loader: serde_loader(|s| toml::from_str(s)),
//! })
//!
//! // JSON
//! .with_file(FileOptions {
//! paths: vec!["config.json".into()],
//! error_if_not_found: false,
//! loader: serde_loader(|s| serde_json::from_str(s)),
//! })
//! ```
//!
//! The CLI also accepts `--config` / `-c` to specify a config file path at runtime
//! (requires calling [`.with_file()`](Configulator::with_file) first).
//!
//! ### Environment Variables
//!
//! Environment variables are formed as `PREFIX` + `SEPARATOR` + `FIELD_NAME`
//! (uppercased, dashes become underscores).
//!
//! ```rust,ignore
//! .with_environment_variables(EnvironmentVariableOptions {
//! prefix: "MYAPP".into(),
//! separator: "__".into(),
//! })
//! ```
//!
//! For example, a field named `max-connections` under a `database` parent with
//! prefix `MYAPP` and separator `__` would be `MYAPP__DATABASE__MAX_CONNECTIONS`.
//!
//! ### CLI Flags
//!
//! Nested fields use the separator to form flag names (e.g. `--database.host`).
//!
//! ```rust,ignore
//! .with_cli_flags(CLIFlagOptions {
//! separator: ".".into(),
//! })
//! ```
//!
//! Boolean fields work as flags (`--debug` sets to true, `--debug false` sets
//! to false). List fields can be repeated (`--ports 80 --ports 443`).
//!
//! You can also provide a custom `clap::Command` to set the app name, version,
//! or add your own flags:
//!
//! ```rust,ignore
//! .with_cli_command(clap::Command::new("myapp").version("1.0"))
//! .with_cli_flags(CLIFlagOptions {
//! separator: ".".into(),
//! })
//! ```
//!
//! ## Validation
//!
//! Implement the [`Validate`] trait and call [`.load()`](Configulator::load) to validate after loading.
//! Use [`.load_without_validation()`](Configulator::load_without_validation) to skip validation.
//!
//! ## Feature Flags
//!
//! Configulator uses feature flags to keep dependencies minimal. All features
//! are enabled by default.
//!
//! | Feature | Description | Dependencies |
//! |---------|----------------------------------------------------------------------|--------------|
//! | `file` | Config file loading (`FileOptions`, `serde_loader`, `--config` flag) | `serde` |
//! | `cli` | CLI flag parsing via clap | `clap` |
//! | `env` | Environment variable loading | - |
//!
//! To opt out of features you don't need:
//!
//! ```toml
//! [dependencies]
//! configulator-rs = { version = "0.1", default-features = false, features = ["env"] }
//! ```
// Re-export the derive macro
pub use Config;
// Re-export public types
pub use crateConfigulator;
pub use crateConfigulatorError;
pub use crateCLIFlagOptions;
pub use crateEnvironmentVariableOptions;
pub use crateFileLoader;
pub use crateserde_loader;
pub use crateFileOptions;
// Re-export derive-macro internals (used by generated code, not public API)
pub use crate;
pub use crate;
pub use crate;
/// Trait implemented by the `Config` derive macro. Provides field metadata.
/// Trait implemented by the `Config` derive macro. Constructs a struct from a `ValueMap`.
/// Trait for user-defined config validation.
///
/// Implement this on your config struct to add validation logic that runs
/// after all sources are merged.