Skip to main content

load_config_tree

Function load_config_tree 

Source
pub fn load_config_tree<T, E, F>(
    root_path: impl AsRef<Path>,
    load: F,
) -> Result<ConfigTree<T>>
where E: Into<BoxError>, F: FnMut(&Path) -> Result<ConfigSource<T>, E>,
Expand description

Loads a config tree with default traversal options.

§Type Parameters

  • T: Loaded value type stored for each config source.
  • E: Error type returned by load.
  • F: Source loader callback type.

§Arguments

  • root_path: Root config path to load first.
  • load: Callback that receives each normalized absolute source path and returns the source value with its declared include paths.

§Returns

Returns a ConfigTree containing loaded nodes in traversal order.

§Examples

use std::{io, path::{Path, PathBuf}};
use rust_config_tree::tree::{ConfigSource, load_config_tree};

let tree = load_config_tree(
    "root.yaml",
    |path: &Path| -> io::Result<ConfigSource<&'static str>> {
        if path.ends_with("root.yaml") {
            Ok(ConfigSource::new("root", vec![PathBuf::from("child.yaml")]))
        } else {
            Ok(ConfigSource::new("child", Vec::new()))
        }
    },
)?;

assert_eq!(tree.into_values(), vec!["root", "child"]);
Examples found in repository?
examples/tree_api.rs (line 16)
14fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
15    let root_config = write_demo_tree()?;
16    let tree = load_config_tree(&root_config, load_source)?;
17
18    println!("declared order:");
19    for node in tree.nodes() {
20        println!("{} -> {:?}", node.path().display(), node.includes());
21    }
22
23    let reverse_tree = ConfigTreeOptions::default()
24        .include_order(IncludeOrder::Reverse)
25        .load(&root_config, load_source)?;
26
27    println!("reverse sibling order:");
28    for value in reverse_tree.into_values() {
29        println!("{}", value.lines().next().unwrap_or_default());
30    }
31
32    Ok(())
33}