pub fn to_string_collapsed(value: &Value) -> String
Expand description

The function converts a given Value to a pretty table, recursively creating new tables if nessary.

let file = r#"
[materials]
metal = { reflectivity = 1.0 }
plastic = { reflectivity = 0.5 }

[[entities]]
name = "hero"
material = "metal"

[[entities]]
name = "monster"
material = "plastic"
"#;

let scene = toml::from_str(file).unwrap();
let table = toml_to_table::to_string_collapsed(&scene);

assert_eq!(
    table,
    concat!(
        "+-----------+----------+-------------------+\n",
        "| entities  | material | metal             |\n",
        "|           +----------+-------------------+\n",
        "|           | name     | hero              |\n",
        "|           +----------+-------------------+\n",
        "|           | material | plastic           |\n",
        "|           +----------+-------------------+\n",
        "|           | name     | monster           |\n",
        "+-----------+---------++-------------+-----+\n",
        "| materials | metal   | reflectivity | 1   |\n",
        "|           +---------+--------------+-----+\n",
        "|           | plastic | reflectivity | 0.5 |\n",
        "+-----------+---------+--------------+-----+",
    )
);
Examples found in repository?
examples/toml_to_string_collapsed.rs (line 19)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let data = r#"
        [materials]
        metal = { reflectivity = 1.0 }
        plastic = { reflectivity = 0.5 }
        
        [[entities]]
        name = "hero"
        material = "metal"
        
        [[entities]]
        name = "monster"
        material = "plastic"
    "#;

    let scene = toml::from_str(data).unwrap();
    let table = toml_to_table::to_string_collapsed(&scene);

    println!("{table}");
}