How to parse toml::value::Table into a struct

I want to parse a Table into struct Event using try_into() function which should work fine, according to the documentation. However, the compiler didn't seem to understand my intention and appeared to think the reference in the following code is this try_into().

#[derive(Clone, Debug, serde::Deserialize, PartialEq)]
pub struct Event {
    pub description: Option<String>,
    pub shootingdate: toml::value::Datetime,
    pub images: Vec<usize>,
}

fn parse_table(table: toml::value::Table) -> Event {
    table.try_into().unwrap()
}

With the error:

error[E0277]: the trait bound `Event: From<toml::map::Map<std::string::String, Value>>` is not satisfied
  --> src/lib.rs:10:5
   |
10 |     table.try_into().unwrap()
   |     ^^^^^ -------- required by a bound introduced by this call
   |     |
   |     the trait `From<toml::map::Map<std::string::String, Value>>` is not implemented for `Event`
   |
   = note: required for `toml::map::Map<std::string::String, Value>` to implement `Into<Event>`
   = note: required for `Event` to implement `TryFrom<toml::map::Map<std::string::String, Value>>`
   = note: required for `toml::map::Map<std::string::String, Value>` to implement `TryInto<Event>`

What version of toml are you using? That method looks to be new to 0.6.0.

I am using toml-0.6.0 as shown in the screenshot.

What does this show?

cargo tree -p toml --depth 0

E.g.

% cargo test
   Compiling asdf v0.1.0 (/home/rust/asdf)
error[E0277]: the trait bound `Event: From<toml::map::Map<std::string::String, Value>>` is not satisfied
 --> src/main.rs:9:11
  |
9 |     table.try_into().unwrap()
  |           ^^^^^^^^ the trait `From<toml::map::Map<std::string::String, Value>>` is not implemented for `Event`
  |
  = note: required because of the requirements on the impl of `Into<Event>` for `toml::map::Map<std::string::String, Value>`
  = note: required because of the requirements on the impl of `TryFrom<toml::map::Map<std::string::String, Value>>` for `Event`
  = note: required because of the requirements on the impl of `TryInto<Event>` for `toml::map::Map<std::string::String, Value>`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `asdf` due to previous error
% fgrep toml Cargo.toml 
toml = "0.5.0"

% sed -i 's/toml = "0.5/toml = "0.6/' Cargo.toml 

% fgrep toml Cargo.toml 
toml = "0.6.0"
% cargo test
    Updating crates.io index
warning: function `parse_table` is never used
 --> src/main.rs:8:4
  |
8 | fn parse_table(table: toml::value::Table) -> Event {
  |    ^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `asdf` (bin "asdf" test) generated 1 warning
    Finished test [unoptimized + debuginfo] target(s) in 0.17s
     Running unittests src/main.rs (target/debug/deps/asdf-ba901003186eaeb9)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Thanks. It turns out that I forgot to run cargo update and I didn't notice that the crates on playground don't always use the latest version.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.