I'm trying to use the TOML crate to read a configuration file into a Rust struct. I was getting a consistent Serde error that seemed unrelated to my code, so I decided to try the decode examples from the TOML documentation and to my surprise, it failed to build with the exact same error.
I have filed an issue with the crate maintainer, but I have a nagging feeling I might be missing something.
The code example in question is the following:
//! An example showing off the usage of `Deserialize` to automatically decode
//! TOML into a Rust `struct`
#![deny(warnings)]
extern crate toml;
extern crate serde;
#[macro_use]
extern crate serde_derive;
/// This is what we're going to decode into. Each field is optional, meaning
/// that it doesn't have to be present in TOML.
#[derive(Debug, Deserialize)]
struct Config {
global_string: Option<String>,
global_integer: Option<u64>,
server: Option<ServerConfig>,
peers: Option<Vec<PeerConfig>>,
}
/// Sub-structs are decoded from tables, so this will decode from the `[server]`
/// table.
///
/// Again, each field is optional, meaning they don't have to be present.
#[derive(Debug, Deserialize)]
struct ServerConfig {
ip: Option<String>,
port: Option<u64>,
}
#[derive(Debug, Deserialize)]
struct PeerConfig {
ip: Option<String>,
port: Option<u64>,
}
fn main() {
let toml_str = r#"
global_string = "test"
global_integer = 5
[server]
ip = "127.0.0.1"
port = 80
[[peers]]
ip = "127.0.0.1"
port = 8080
[[peers]]
ip = "127.0.0.1"
"#;
let decoded: Config = toml::from_str(toml_str).unwrap();
println!("{:#?}", decoded);
}
The error I get when building is the following:
error[E0277]: the trait bound `Config: serde::de::Deserialize<'_>` is not satisfied
--> src/main.rs:51:27
|
51 | let decoded: Config = toml::from_str(toml_str).unwrap();
| ^^^^^^^^^^^^^^ the trait `serde::de::Deserialize<'_>` is not implemented for `Config`
|
= note: required by `toml::from_str`
I have tried to build it with the following toolchains:
rustc 1.20.0-nightly (2652ce677 2017-07-17)
rustc 1.18.0 (03fc9d622 2017-06-06)
My Cargo.toml includes the following:
[dependencies]
serde = "*"
serde_derive = "*"
toml = "*"
Am I missing something, or is the base example of decoding with this crate simply broken?