parse

Function parse 

Source
pub fn parse<'a>(
    on: &'a str,
    cb: impl for<'b> FnMut(&'b [TOMLKey<'a>], RootTOMLValue<'a>),
) -> Result<(), TOMLParseError>
Expand description

If you want to return early (not parse the whole input) use parse_with_exit_signal

ยงErrors

Returns an error if it tries to parse invalid TOML input

Examples found in repository?
examples/main.rs (lines 43-45)
3fn main() {
4    let example = r#"
5# This is a TOML document
6
7title = "TOML Example"
8
9[owner]
10name = "Tom Preston-Werner"
11dob = 1979-05-27T07:32:00-08:00
12
13[database]
14enabled = true
15ports = [8000, 8001, 8002]
16data = [["delta", "phi"], [3.14]]
17temp_targets = { cpu = 79.5, case = 72.0 }
18
19[servers]
20
21[servers.alpha]
22ip = "10.0.0.1"
23role = "frontend"
24
25[servers.beta]
26ip = "10.0.0.2"
27"#
28    .trim_start();
29
30    let source = if let Some(path) = std::env::args().nth(1) {
31        std::fs::read_to_string(path).unwrap()
32    } else {
33        example.to_owned()
34    };
35
36    // let second_arg = std::env::args().nth(2);
37    // let mode = second_arg
38    //     .and_then(|arg| {
39    //         matches!(arg.as_str(), "--verbose" | "--text").then_some(arg[2..].to_owned())
40    //     })
41    //     .unwrap_or_default();
42
43    parse_toml(&source, |keys, value| {
44        eprintln!("{keys:?} -> {value:?}");
45    })
46    .unwrap();
47}