Skip to main content

rust_config_tree/
config.rs

1//! High-level `confique` integration and generate-template rendering.
2//!
3//! This module loads `.env` values, builds a Figment runtime source graph,
4//! extracts it into a `confique` schema for defaults and validation, renders
5//! example templates that mirror the same include tree, and writes JSON Schema
6//! files that editors can use for completion and validation. YAML templates can
7//! also be split across nested schema sections.
8
9use std::path::PathBuf;
10
11use confique::Config;
12
13pub use crate::config_env::ConfiqueEnvProvider;
14pub use crate::config_format::ConfigFormat;
15pub use crate::config_load::{
16    build_config_figment, load_config, load_config_from_figment, load_config_with_figment,
17};
18pub use crate::config_load_adapt::TransparentSectionContext;
19pub(crate) use crate::config_output::default_config_schema_output;
20pub use crate::config_schema::target::ConfigSchemaTarget;
21pub use crate::config_schema::write::{
22    config_schema_targets_for_path, write_config_schema, write_config_schemas,
23};
24pub use crate::config_templates::{
25    ConfigTemplateTarget, template_for_path, template_targets_for_paths,
26    template_targets_for_paths_with_schema, write_config_templates,
27    write_config_templates_with_schema,
28};
29pub use crate::config_trace::trace_config_sources;
30pub use crate::transparent_section::ArraySection;
31
32/// Result type used by the high-level configuration API.
33///
34/// The error type is [`ConfigError`](crate::error::ConfigError).
35pub type ConfigResult<T> = std::result::Result<T, crate::error::ConfigError>;
36
37/// A `confique` schema that can expose recursive include paths and template
38/// section layout.
39///
40/// Implement this trait for the same type that derives `confique::Config`.
41/// `include_paths` receives a partially loaded layer so the crate can discover
42/// child config files before the final schema is merged.
43pub trait ConfigSchema: Config + Sized {
44    /// Returns include paths declared by a loaded config layer.
45    ///
46    /// Relative paths are resolved from the file that declared them. Empty paths
47    /// are rejected before traversal continues.
48    ///
49    /// # Arguments
50    ///
51    /// - `layer`: Partially loaded `confique` layer for one config file.
52    ///
53    /// # Returns
54    ///
55    /// Returns include paths declared by `layer`.
56    ///
57    /// # Examples
58    ///
59    /// The simplest way to implement `ConfigSchema` is with the derive macro:
60    ///
61    /// ```ignore
62    /// #[derive(confique::Config, rust_config_tree::ConfigSchema)]
63    /// struct AppConfig {
64    ///     #[config(default = [])]
65    ///     include: Vec<std::path::PathBuf>,
66    /// }
67    /// ```
68    ///
69    /// See the crate-level [`ConfigSchema`](crate::config::ConfigSchema) documentation for the
70    /// manual implementation pattern.
71    fn include_paths(layer: &<Self as Config>::Layer) -> Vec<PathBuf>;
72
73    /// Overrides the generated template file path for a split nested section.
74    ///
75    /// A nested section is split only when its field schema has
76    /// `x-tree-split = true`, for example
77    /// `#[schemars(extend("x-tree-split" = true))]`. By default, top-level
78    /// split sections are generated as `<field>.yaml` relative to the root
79    /// template directory and nested split sections as children of their
80    /// parent section file stem, e.g. `trading/risk.yaml`.
81    ///
82    /// # Arguments
83    ///
84    /// - `section_path`: Path of nested schema field names from the root schema
85    ///   to the section being rendered.
86    ///
87    /// # Returns
88    ///
89    /// Returns `Some(path)` to override the generated file path, or `None` to
90    /// use the default section path.
91    ///
92    /// # Examples
93    ///
94    /// ```
95    /// use confique::Config;
96    /// use rust_config_tree::config::ConfigSchema;
97    ///
98    /// #[derive(Config)]
99    /// struct AppConfig {
100    ///     #[config(default = [])]
101    ///     include: Vec<std::path::PathBuf>,
102    /// }
103    ///
104    /// impl ConfigSchema for AppConfig {
105    ///     fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
106    ///         layer.include.clone().unwrap_or_default()
107    ///     }
108    ///
109    ///     fn template_path_for_section(section_path: &[&str]) -> Option<std::path::PathBuf> {
110    ///         match section_path {
111    ///             ["server"] => Some(std::path::PathBuf::from("config/server.yaml")),
112    ///             _ => None,
113    ///         }
114    ///     }
115    /// }
116    /// ```
117    fn template_path_for_section(section_path: &[&str]) -> Option<PathBuf> {
118        let _ = section_path;
119        None
120    }
121}
122
123#[cfg(test)]
124#[path = "unit_tests/config.rs"]
125mod unit_tests;