pub fn load_config<S>(path: impl AsRef<Path>) -> ConfigResult<S>where
S: ConfigSchema + JsonSchema,Expand description
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 derivesConfigand implementsConfigSchema.
§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::config::load_config;
use schemars::JsonSchema;
#[derive(Debug, Config, JsonSchema, rust_config_tree::ConfigSchema)]
struct AppConfig {
#[config(default = [])]
include: Vec<std::path::PathBuf>,
#[config(default = "demo")]
mode: String,
}
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");Examples found in repository?
examples/basic_loading.rs (line 41)
39fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
40 let root_config = write_demo_config()?;
41 let config = load_config::<AppConfig>(&root_config)?;
42
43 println!("config path: {}", root_config.display());
44 println!("include count: {}", config.include.len());
45 println!("mode: {}", config.mode);
46 println!("server bind: {}", config.server.bind);
47 println!("server port: {}", config.server.port);
48
49 Ok(())
50}More examples
examples/transparent_array_section.rs (line 75)
63fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
64 let dir = temp_example_dir("transparent-array-section")?;
65 let root = dir.join("config.yaml");
66 let schema = dir.join("app.schema.json");
67 let children = dir.join("children.yaml");
68
69 fs::write(&root, "include:\n - children.yaml\nmode: demo\n")?;
70 fs::write(&children, "- name: api\n")?;
71
72 write_config_schemas::<AppConfig>(&schema)?;
73 println!("schema: {}", schema.display());
74
75 let config = load_config::<AppConfig>(&root)?;
76 println!("loaded children: {}", config.children.len());
77 assert_eq!(config.children.items[0].name, "api");
78
79 println!("split file: {}", children.display());
80
81 let _ = fs::remove_dir_all(dir);
82 Ok(())
83}examples/config_commands.rs (line 68)
59fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
60 let cli = Cli::parse();
61 let config_path = match &cli.config {
62 Some(path) => path.clone(),
63 None => write_demo_config()?,
64 };
65
66 match cli.command.unwrap_or(Command::Run) {
67 Command::Run => {
68 let config = load_config::<AppConfig>(&config_path)?;
69 println!("config path: {}", config_path.display());
70 println!("include count: {}", config.include.len());
71 println!("mode: {}", config.mode);
72 println!("server bind: {}", config.server.bind);
73 println!("server port: {}", config.server.port);
74 }
75 Command::Config(command) => {
76 handle_config_command::<Cli, AppConfig>(command, &config_path)?;
77 }
78 }
79
80 Ok(())
81}