Skip to main content

parse_toml

Function parse_toml 

Source
pub fn parse_toml<T>(
    text: &str,
    options: Option<FormatOptions>,
) -> Result<Formatted<T>, Error>
Expand description

Parses a TOML string into a value, capturing outer whitespace only.

Examples found in repository?
examples/parse_toml.rs (line 17)
6fn main() {
7    let text = r#"
8
9[package]
10name = "c12-parser"
11version = "1.0.0"
12
13[dependencies]
14serde = "1.0"
15"#;
16
17    let formatted = parse_toml::<Value>(text, None).expect("parse");
18    if let Some(Value::Table(deps)) = formatted.value.get("dependencies") {
19        println!("Dependencies: {:?}", deps.keys().collect::<Vec<_>>());
20    }
21    if let Some(Value::Table(pkg)) = formatted.value.get("package") {
22        println!(
23            "Package: {} {}",
24            pkg.get("name").and_then(|v| v.as_str()).unwrap_or(""),
25            pkg.get("version").and_then(|v| v.as_str()).unwrap_or("")
26        );
27    }
28
29    let out = stringify_toml(&formatted, None).expect("stringify");
30    println!("Stringify (outer whitespace preserved):\n{}", out);
31}