rust-config-tree 0.1.7

Recursive include tree utilities for layered configuration files.
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! High-level runtime loading through Figment and `confique`.
//!
//! This module is responsible for discovering the recursive include tree,
//! merging config files in runtime precedence order, layering schema-declared
//! environment variables on top, and finally asking `confique` to apply
//! defaults and validation.

use std::path::Path;

use confique::{Config, Layer};
use figment::{
    Figment,
    providers::{Format, Json, Toml, Yaml},
};

use crate::{
    ConfigSource, ConfigTree, ConfigTreeOptions, IncludeOrder, absolutize_lexical,
    config::{ConfigResult, ConfigSchema},
    config_env::ConfiqueEnvProvider,
    config_format::ConfigFormat,
    config_trace::trace_config_sources,
};

/// Loads a complete `confique` schema from a root config path.
///
/// The loader follows recursive include paths exposed by [`ConfigSchema`],
/// resolves relative include paths from the declaring file, detects include
/// cycles, loads the first `.env` file found from the root config directory
/// upward, builds a [`Figment`] from config files and schema-declared
/// environment variables, and then asks `confique` to apply defaults and
/// validation. Existing process environment variables take precedence over
/// values loaded from `.env`.
///
/// # Type Parameters
///
/// - `S`: Config schema type that derives [`Config`] and implements
///   [`ConfigSchema`].
///
/// # Arguments
///
/// - `path`: Root config file path.
///
/// # Returns
///
/// Returns the merged config schema after loading the root file, recursive
/// includes, `.env` values, and environment values.
///
/// # Examples
///
/// ```
/// use std::fs;
/// use confique::Config;
/// use rust_config_tree::{ConfigSchema, load_config};
///
/// #[derive(Debug, Config)]
/// struct AppConfig {
///     #[config(default = [])]
///     include: Vec<std::path::PathBuf>,
///     #[config(default = "demo")]
///     mode: String,
/// }
///
/// impl ConfigSchema for AppConfig {
///     fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
///         layer.include.clone().unwrap_or_default()
///     }
/// }
///
/// # fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let path = std::env::temp_dir().join("rust-config-tree-load-config-doctest.yaml");
/// fs::write(&path, "mode: local\n")?;
///
/// let config = load_config::<AppConfig>(&path)?;
///
/// assert_eq!(config.mode, "local");
/// # let _ = fs::remove_file(path);
/// # Ok(())
/// # }
/// # run().unwrap();
/// ```
pub fn load_config<S>(path: impl AsRef<Path>) -> ConfigResult<S>
where
    S: ConfigSchema,
{
    let (config, _) = load_config_with_figment::<S>(path)?;
    Ok(config)
}

/// Loads a config schema and returns the Figment graph used for runtime loading.
///
/// The returned [`Figment`] can be inspected with [`Figment::find_metadata`] to
/// determine which provider supplied a runtime value.
///
/// # Type Parameters
///
/// - `S`: Config schema type that derives [`Config`] and implements
///   [`ConfigSchema`].
///
/// # Arguments
///
/// - `path`: Root config file path.
///
/// # Returns
///
/// Returns the merged config schema and its runtime Figment source graph.
///
/// # Examples
///
/// ```
/// use std::fs;
/// use confique::Config;
/// use rust_config_tree::{ConfigSchema, load_config_with_figment};
///
/// #[derive(Debug, Config)]
/// struct AppConfig {
///     #[config(default = [])]
///     include: Vec<std::path::PathBuf>,
///     #[config(default = "demo")]
///     mode: String,
/// }
///
/// impl ConfigSchema for AppConfig {
///     fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
///         layer.include.clone().unwrap_or_default()
///     }
/// }
///
/// # fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let path = std::env::temp_dir().join("rust-config-tree-load-with-figment-doctest.yaml");
/// fs::write(&path, "mode: local\n")?;
///
/// let (config, figment) = load_config_with_figment::<AppConfig>(&path)?;
///
/// assert_eq!(config.mode, "local");
/// # let _ = figment;
/// # let _ = fs::remove_file(path);
/// # Ok(())
/// # }
/// # run().unwrap();
/// ```
pub fn load_config_with_figment<S>(path: impl AsRef<Path>) -> ConfigResult<(S, Figment)>
where
    S: ConfigSchema,
{
    let figment = build_config_figment::<S>(path)?;
    let config = load_config_from_figment::<S>(&figment)?;

    Ok((config, figment))
}

/// Builds the Figment runtime source graph for a config tree.
///
/// Config files are merged in include order, then environment variables
/// declared by [`ConfiqueEnvProvider`] are merged with higher priority.
///
/// # Type Parameters
///
/// - `S`: Config schema type used to discover includes and environment names.
///
/// # Arguments
///
/// - `path`: Root config file path.
///
/// # Returns
///
/// Returns a Figment source graph with file and environment providers.
///
/// # Examples
///
/// ```
/// use std::fs;
/// use confique::Config;
/// use rust_config_tree::{ConfigSchema, build_config_figment};
///
/// #[derive(Debug, Config)]
/// struct AppConfig {
///     #[config(default = [])]
///     include: Vec<std::path::PathBuf>,
///     #[config(default = "demo")]
///     mode: String,
/// }
///
/// impl ConfigSchema for AppConfig {
///     fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
///         layer.include.clone().unwrap_or_default()
///     }
/// }
///
/// # fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let path = std::env::temp_dir().join("rust-config-tree-build-figment-doctest.yaml");
/// fs::write(&path, "mode: local\n")?;
///
/// let figment = build_config_figment::<AppConfig>(&path)?;
/// # let _ = figment;
/// # let _ = fs::remove_file(path);
/// # Ok(())
/// # }
/// # run().unwrap();
/// ```
pub fn build_config_figment<S>(path: impl AsRef<Path>) -> ConfigResult<Figment>
where
    S: ConfigSchema,
{
    let path = path.as_ref();
    load_dotenv_for_path(path)?;

    let tree = load_layer_tree::<S>(path)?;
    let mut figment = Figment::new();

    for node in tree.nodes().iter().rev() {
        figment = merge_file_provider(figment, node.path());
    }

    Ok(figment.merge(ConfiqueEnvProvider::new::<S>()))
}

/// Extracts and validates a config schema from a Figment source graph.
///
/// Figment supplies runtime values. `confique` supplies code defaults and final
/// validation.
///
/// # Type Parameters
///
/// - `S`: Config schema type to extract and validate.
///
/// # Arguments
///
/// - `figment`: Runtime source graph.
///
/// # Returns
///
/// Returns the final config schema.
///
/// # Examples
///
/// ```
/// use std::fs;
/// use confique::Config;
/// use rust_config_tree::{ConfigSchema, build_config_figment, load_config_from_figment};
///
/// #[derive(Debug, Config)]
/// struct AppConfig {
///     #[config(default = [])]
///     include: Vec<std::path::PathBuf>,
///     #[config(default = "demo")]
///     mode: String,
/// }
///
/// impl ConfigSchema for AppConfig {
///     fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
///         layer.include.clone().unwrap_or_default()
///     }
/// }
///
/// # fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// let path = std::env::temp_dir().join("rust-config-tree-load-from-figment-doctest.yaml");
/// fs::write(&path, "mode: local\n")?;
/// let figment = build_config_figment::<AppConfig>(&path)?;
///
/// let config = load_config_from_figment::<AppConfig>(&figment)?;
///
/// assert_eq!(config.mode, "local");
/// # let _ = fs::remove_file(path);
/// # Ok(())
/// # }
/// # run().unwrap();
/// ```
pub fn load_config_from_figment<S>(figment: &Figment) -> ConfigResult<S>
where
    S: ConfigSchema,
{
    let runtime_layer: <S as Config>::Layer = figment.extract()?;
    let config = S::from_layer(runtime_layer.with_fallback(S::Layer::default_values()))?;

    trace_config_sources::<S>(figment);

    Ok(config)
}

/// Loads one config layer from disk using the format inferred from the path.
///
/// # Type Parameters
///
/// - `S`: Config schema type whose intermediate `confique` layer should be
///   loaded.
///
/// # Arguments
///
/// - `path`: Config file path to load.
///
/// # Returns
///
/// Returns the loaded `confique` layer for `S`.
///
/// # Examples
///
/// ```no_run
/// let _ = ();
/// ```
pub(crate) fn load_layer<S>(path: &Path) -> ConfigResult<<S as Config>::Layer>
where
    S: ConfigSchema,
{
    Ok(figment_for_file(path).extract()?)
}

/// Loads every config layer reachable from the root include tree.
///
/// # Type Parameters
///
/// - `S`: Config schema type whose layer type is loaded for each file.
///
/// # Arguments
///
/// - `path`: Root config path used to start include traversal.
///
/// # Returns
///
/// Returns the loaded config tree containing one `confique` layer per source.
///
/// # Examples
///
/// ```no_run
/// let _ = ();
/// ```
fn load_layer_tree<S>(path: &Path) -> ConfigResult<ConfigTree<<S as Config>::Layer>>
where
    S: ConfigSchema,
{
    // Reverse traversal lets later declared includes override earlier files
    // after the collected nodes are merged from leaves back toward the root.
    Ok(ConfigTreeOptions::default()
        .include_order(IncludeOrder::Reverse)
        .load(
            path,
            |path| -> ConfigResult<ConfigSource<<S as Config>::Layer>> {
                let layer = load_layer::<S>(path)?;
                let include_paths = S::include_paths(&layer);
                Ok(ConfigSource::new(layer, include_paths))
            },
        )?)
}

/// Merges one file provider selected from the path extension.
///
/// # Arguments
///
/// - `figment`: Existing Figment graph to extend.
/// - `path`: Config file path whose extension selects the provider format.
///
/// # Returns
///
/// Returns `figment` with the selected file provider merged in.
///
/// # Examples
///
/// ```no_run
/// let _ = ();
/// ```
fn merge_file_provider(figment: Figment, path: &Path) -> Figment {
    match ConfigFormat::from_path(path) {
        ConfigFormat::Yaml => figment.merge(Yaml::file_exact(path)),
        ConfigFormat::Toml => figment.merge(Toml::file_exact(path)),
        ConfigFormat::Json => figment.merge(Json::file_exact(path)),
    }
}

/// Builds a Figment graph containing only one config file provider.
///
/// # Arguments
///
/// - `path`: Config file path to load through Figment.
///
/// # Returns
///
/// Returns a Figment graph containing exactly that file provider.
///
/// # Examples
///
/// ```no_run
/// let _ = ();
/// ```
pub(crate) fn figment_for_file(path: &Path) -> Figment {
    merge_file_provider(Figment::new(), path)
}

/// Loads the nearest ancestor `.env` file for a config path when it exists.
///
/// # Arguments
///
/// - `path`: Config file path whose ancestors should be searched.
///
/// # Returns
///
/// Returns `Ok(())` after loading the first discovered `.env`, or when none
/// exists.
///
/// # Examples
///
/// ```no_run
/// let _ = ();
/// ```
fn load_dotenv_for_path(path: &Path) -> ConfigResult<()> {
    let path = absolutize_lexical(path)?;
    let mut current_dir = path.parent();

    while let Some(dir) = current_dir {
        let dotenv_path = dir.join(".env");
        if dotenv_path.try_exists()? {
            // `dotenvy` preserves existing process variables, so explicit
            // environment values keep precedence over values from `.env`.
            dotenvy::from_path(&dotenv_path)?;
            break;
        }
        current_dir = dir.parent();
    }

    Ok(())
}