Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse a TOML file with a table at the top level into a Rust structure?

Tags:

rust

serde

toml

For example:

#[macro_use]
extern crate serde_derive;
extern crate toml;

#[derive(Deserialize)]
struct Entry {
    foo: String,
    bar: String,
}

let toml_string = r#"
  [[entry]]
  foo = "a0"
  bar = "b0"

  [[entry]]
  foo = "a1"
  bar = "b1"
  "#;
let config: toml::value::Table<Entry> = toml::from_str(&toml_string)?;

However, this does not work and throws an error about unexpected type argument for Table.

like image 501
Max Maier Avatar asked Oct 23 '25 11:10

Max Maier


1 Answers

Printing the arbitrary parsed value shows you what structure you have:

let config: toml::Value = toml::from_str(&toml_string)?;
println!("{:?}", config)

The reformatted output shows that you have a table with a single key entry which is an array of tables with the keys foo and bar:

Table({
  "entry": Array([
    Table({
      "bar": String("b0"),
      "foo": String("a0")
    }),
    Table({
      "bar": String("b1"),
      "foo": String("a1")
    })
  ])
})

When deserializing, you need to match this structure:

#[derive(Debug, Deserialize)]
struct Outer {
    entry: Vec<Entry>,
}

#[derive(Debug, Deserialize)]
struct Entry {
    foo: String,
    bar: String,
}
let config: Outer = toml::from_str(&toml_string)?;
like image 107
Shepmaster Avatar answered Oct 26 '25 04:10

Shepmaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!