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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! # Verbosity System for Configuration Debugging
//!
//! This module provides a comprehensive verbosity system for debugging configuration loading
//! issues. Similar to CLI tools like `curl -v`, `git --verbose`, or `rsync -vvv`, SuperConfig
//! supports multiple verbosity levels to help troubleshoot configuration problems step-by-step.
//!
//! ## Why Verbosity Matters
//!
//! Configuration loading often fails silently or with cryptic error messages. Users need to
//! understand:
//! - Which configuration sources are being checked
//! - What values are being loaded from each source
//! - Where conflicts or overrides are happening
//! - Why certain values aren't being applied
//!
//! The verbosity system makes this transparent.
//!
//! ## Verbosity Levels
//!
//! SuperConfig provides 4 verbosity levels, following CLI tool conventions:
//!
//! - **Silent (0)**: No debug output - production mode
//! - **Info (1)**: Basic loading progress - shows major steps
//! - **Debug (2)**: Detailed steps with success/failure indicators - troubleshooting mode
//! - **Trace (3)**: Full introspection with actual configuration values - deep debugging
//!
//! ## Basic Usage
//!
//! Enable verbosity during configuration building:
//!
//! ```rust,no_run
//! use superconfig::{SuperConfig, VerbosityLevel};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize, Serialize, Default)]
//! struct AppConfig {
//! name: String,
//! port: u16,
//! }
//!
//! let config = SuperConfig::new()
//! .with_verbosity(VerbosityLevel::Debug) // Enable debug output
//! .with_file("config.toml")
//! .with_env("APP_");
//!
//! let result: AppConfig = config.extract()?;
//! # Ok::<(), figment::Error>(())
//! ```
//!
//! ## CLI Integration Pattern
//!
//! Integrate with CLI argument parsing for user-friendly debugging:
//!
//! ```rust,no_run
//! use superconfig::{SuperConfig, VerbosityLevel};
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize, Serialize, Default)]
//! struct AppConfig {
//! database_url: String,
//! log_level: String,
//! }
//!
//! // Simulate CLI argument parsing (normally from clap, structopt, etc.)
//! fn parse_cli_verbosity() -> u8 {
//! let args: Vec<String> = std::env::args().collect();
//! args.iter()
//! .skip(1)
//! .filter(|arg| arg.starts_with("-v"))
//! .map(|arg| arg.matches('v').count() as u8)
//! .max()
//! .unwrap_or(0)
//! }
//!
//! let verbose_count = parse_cli_verbosity();
//! let verbosity = VerbosityLevel::from_cli_args(verbose_count);
//!
//! let config = SuperConfig::new()
//! .with_verbosity(verbosity)
//! .with_hierarchical_config("myapp")
//! .with_env("APP_");
//!
//! let app_config: AppConfig = config.extract()?;
//! # Ok::<(), figment::Error>(())
//! ```
//!
//! ## Verbosity Level Examples
//!
//! ### Silent Mode (Production)
//! ```bash
//! myapp # No verbosity flags, no debug output
//! ```
//!
//! ### Info Mode (-v)
//! ```bash
//! myapp -v
//! ```
//! **Output:**
//! ```text
//! CONFIG: Loading hierarchical config for: myapp
//! CONFIG: Loading environment variables with prefix: APP_
//! CONFIG: Extracting final configuration
//! CONFIG: Configuration extraction successful ✓
//! ```
//!
//! ### Debug Mode (-vv)
//! ```bash
//! myapp -vv
//! ```
//! **Output:**
//! ```text
//! CONFIG: [1/4] Loading hierarchical config for: myapp
//! CONFIG: Checking hierarchical config paths:
//! CONFIG: - /etc/myapp/config.toml ✗
//! CONFIG: - ~/.config/myapp/config.toml ✓
//! CONFIG: [2/4] Loading environment variables with prefix: APP_
//! CONFIG: Found 3 environment variables ✓
//! CONFIG: [3/4] No CLI arguments provided ✗
//! CONFIG: [4/4] Extracting final configuration
//! CONFIG: Configuration extraction successful ✓
//! ```
//!
//! ### Trace Mode (-vvv)
//! ```bash
//! myapp -vvv
//! ```
//! **Output:**
//! ```text
//! CONFIG: [1/4] Loading hierarchical config for: myapp
//! CONFIG: Checking hierarchical config paths:
//! CONFIG: - /etc/myapp/config.toml ✗
//! CONFIG: - ~/.config/myapp/config.toml ✓
//! CONFIG: database_url = "postgresql://localhost/dev"
//! CONFIG: log_level = "debug"
//! CONFIG: [2/4] Loading environment variables with prefix: APP_
//! CONFIG: Found 3 environment variables ✓
//! CONFIG: APP_DATABASE_URL = "postgresql://prod.db/myapp"
//! CONFIG: APP_LOG_LEVEL = "info"
//! CONFIG: APP_DEBUG_PASSWORD = ***MASKED***
//! CONFIG: [3/4] No CLI arguments provided ✗
//! CONFIG: [4/4] Extracting final configuration
//! CONFIG: Final merged configuration:
//! {
//! "database_url": "postgresql://prod.db/myapp",
//! "log_level": "info"
//! }
//! CONFIG: Configuration extraction successful ✓
//! ```
//!
//! ## Security Features
//!
//! The verbosity system automatically masks sensitive data in trace output.
//! Environment variables containing these keywords are masked:
//! - `password`
//! - `secret`
//! - `token`
//! - `key`
//!
//! ```rust,no_run
//! use superconfig::{SuperConfig, VerbosityLevel};
//!
//! // Environment variables with sensitive keywords are automatically masked
//! // Example: APP_DATABASE_PASSWORD=***MASKED*** in trace output
//! // Example: APP_API_TOKEN=***MASKED*** in trace output
//!
//! let config = SuperConfig::new()
//! .with_verbosity(VerbosityLevel::Trace) // Will mask sensitive values
//! .with_env("APP_");
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Collecting Debug Messages
//!
//! Access debug messages programmatically for custom logging or analysis:
//!
//! ```rust,no_run
//! use superconfig::{SuperConfig, VerbosityLevel};
//!
//! let config = SuperConfig::new()
//! .with_verbosity(VerbosityLevel::Debug)
//! .with_file("config.toml")
//! .with_env("APP_");
//!
//! // Messages are collected regardless of display verbosity
//! let debug_messages = config.debug_messages();
//! println!("Collected {} debug messages", debug_messages.len());
//!
//! // Filter messages by level
//! let error_messages = config.debug_messages_at_level(VerbosityLevel::Info);
//! for msg in error_messages {
//! println!("Info: {}", msg.message);
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
use Instant;
/// Verbosity levels for configuration debugging
///
/// Controls the amount of debug information displayed during configuration loading.
/// Higher levels include all information from lower levels.
/// A debug message captured during configuration loading
/// Helper trait for collecting debug messages during configuration operations