toml-rs/tests/tables-last.rs
Alex Crichton 67fb9f1953 Add a serialization helper to put tables last
This should help serializing maps where it's unknown up front whether the tables
and/or values come first.

Closes #142
2017-02-10 15:29:36 -08:00

31 lines
597 B
Rust

#[macro_use]
extern crate serde_derive;
extern crate toml;
use std::collections::HashMap;
#[derive(Serialize)]
struct A {
#[serde(serialize_with = "toml::ser::tables_last")]
vals: HashMap<&'static str, Value>,
}
#[derive(Serialize)]
#[serde(untagged)]
enum Value {
Map(HashMap<&'static str, &'static str>),
Int(i32),
}
#[test]
fn always_works() {
let mut a = A { vals: HashMap::new() };
a.vals.insert("foo", Value::Int(0));
let mut sub = HashMap::new();
sub.insert("foo", "bar");
a.vals.insert("bar", Value::Map(sub));
toml::to_string(&a).unwrap();
}