10

I'm trying to read a TOML file to create a struct that contains a vector of enums with associated values. Here's the sample code:

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

use std::fs::File;
use std::io::Read;

#[derive(Debug, Deserialize, PartialEq)]
struct Actor {
    name: String,
    actions: Vec<Actions>,
}

#[derive(Debug, Deserialize, PartialEq)]
enum Actions {
    Wait(usize),
    Move { x: usize, y: usize },
}

fn main() {
    let input_file = "./sample_actor.toml";
    let mut file = File::open(input_file).unwrap();
    let mut file_content = String::new();
    let _bytes_read = file.read_to_string(&mut file_content).unwrap();
    let actor: Actor = toml::from_str(&file_content).unwrap();
    println!("Read actor {:?}", actor);
}

Cargo.toml

[dependencies]
serde_derive = "1.0.10"
serde = "1.0.10"
toml = "0.4.2"

sample_actor.toml

name = "actor1"
actions = [move 3 4, wait 20, move 5 6]

I know the file looks "wrong", but I have no idea how I should write the actions in the TOML file such that the crate would be able to recognize them as an enum with X number of values.

The error I get when running this example with cargo run is the following:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { inner: ErrorInner { kind: NumberInvalid, line: Some(1), col: 11, message: "", key: [] } }', /checkout/src/libcore/result.rs:906:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.

I know that I probably need to implement FromStr for my enum to convert a string into my enum, and I briefly know that custom deserializers can be implemented to deserialize in a specific way, but I'm not sure how these pieces fit together.

It seems an equivalent example using serde_json instead of TOML works straight out (using JSON files instead of TOML of course).

JSON version of the code:

extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate serde_derive;

use serde_json::Error;
use std::fs::File;
use std::io::Read;

#[derive(Debug, Serialize, Deserialize)]
enum Foo {
    bar(u32),
    baz { x: u32, y: u32 },
}

#[derive(Debug, Serialize, Deserialize)]
struct Address {
    street: String,
    city: String,
    nums: Vec<Foo>,
}

fn main() {
    /*
        let address = Address {
            street: "10 Downing Street".to_owned(),
            city: "London".to_owned(),
            nums : vec![Foo::bar(1), Foo::baz{x : 2, y : 3}],
        };

        // Serialize it to a JSON string.
        let j = serde_json::to_string(&address).unwrap();

        // Print, write to a file, or send to an HTTP server.
        println!("{}", j);
    */
    let input_file = "./sample_address.json";
    let mut file = File::open(input_file).unwrap();
    let mut file_content = String::new();
    let _bytes_read = file.read_to_string(&mut file_content).unwrap();
    let address: Address = serde_json::from_str(&file_content).unwrap();
    println!("{:?}", address);
}

The JSON data read/written in this example is:

Address { street: "10 Downing Street", city: "London", nums: [bar(1), baz { x: 2, y: 3 }] }

Maybe the TOML crate can't support my use-case?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Dash83
  • 1,357
  • 2
  • 17
  • 31
  • 4
    The quickest way to figure out how your toml should be looking is by deriving `Serialize` and emitting toml for your structures. – oli_obk Feb 06 '18 at 11:47
  • @oli_obk-ker that's a really good idea, thank you! – Dash83 Feb 06 '18 at 13:04
  • 1
    And now that you mentioned it, it seems so obvious, I feel stupid for not trying this in the first place lol. – Dash83 Feb 06 '18 at 13:05
  • Well, it seems the toml crate has the same questions I do: how do you serialize the enum? When I create an actor and use toml::to_string() on it, it produces the output Err(UnsupportedType). If I change the definition of actor so that actions be Vec instead of Vec, it produces valid TOML. – Dash83 Feb 06 '18 at 14:00

1 Answers1

8

Serde has lots of options for serializing enums. One that works for your case:

use serde::{Deserialize, Serialize}; // 1.0.117
use toml; // 0.5.7

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", content = "args")]
enum Actions {
    Wait(usize),
    Move { x: usize, y: usize },
}

fn main() {
    let a_wait = Actions::Wait(5);
    println!("{}", toml::to_string(&a_wait).unwrap());

    let a_move = Actions::Move { x: 1, y: 1 };
    println!("{}", toml::to_string(&a_move).unwrap());
}
type = "Wait"
args = 5
type = "Move"

[args]
x = 1
y = 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366