Function figment::util::nest

source ·
pub fn nest(key: &str, value: Value) -> Value
Expand description

Given a key path key of the form a.b.c, creates nested dictionaries for for every path component delimited by . in the path string (3 in a.b.c), each a parent of the next, and the leaf mapping to value (a -> b -> c -> value).

If key is empty, simply returns value. Otherwise, Value will be a dictionary with the nested mappings.

Example

use figment::{util::nest, value::Value};

let leaf = Value::from("I'm a leaf!");

let dict = nest("tea", leaf.clone());
assert_eq!(dict.find_ref("tea").unwrap(), &leaf);

let dict = nest("tea.leaf", leaf.clone());
let tea = dict.find_ref("tea").unwrap();
let found_leaf = tea.find_ref("leaf").unwrap();
assert_eq!(found_leaf, &leaf);
assert_eq!(dict.find_ref("tea.leaf").unwrap(), &leaf);

let just_leaf = nest("", leaf.clone());
assert_eq!(just_leaf, leaf);