close #199: add header information for empty structs
- also add test which fails without this change - also add a few helpful unit tests to table
This commit is contained in:
parent
05a8c73439
commit
61ff3c295c
|
@ -930,6 +930,15 @@ impl<'a, 'b> ser::SerializeStruct for SerializeTable<'a, 'b> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(self) -> Result<(), Error> {
|
fn end(self) -> Result<(), Error> {
|
||||||
|
match self {
|
||||||
|
SerializeTable::Datetime(_) => {},
|
||||||
|
SerializeTable::Table { mut ser, first, .. } => {
|
||||||
|
if first.get() {
|
||||||
|
let state = ser.state.clone();
|
||||||
|
ser.emit_table_header(&state)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -554,3 +554,25 @@ fn newtypes2() {
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||||
|
struct CanBeEmpty {
|
||||||
|
a: Option<String>,
|
||||||
|
b: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn table_structs_empty() {
|
||||||
|
let text = "[bar]\n\n[baz]\n\n[bazv]\na = \"foo\"\n\n[foo]\n";
|
||||||
|
let value: BTreeMap<String, CanBeEmpty> = toml::from_str(text).unwrap();
|
||||||
|
let mut expected: BTreeMap<String, CanBeEmpty> = BTreeMap::new();
|
||||||
|
expected.insert("bar".to_string(), CanBeEmpty::default());
|
||||||
|
expected.insert("baz".to_string(), CanBeEmpty::default());
|
||||||
|
expected.insert(
|
||||||
|
"bazv".to_string(),
|
||||||
|
CanBeEmpty {a: Some("foo".to_string()), b: None},
|
||||||
|
);
|
||||||
|
expected.insert("foo".to_string(), CanBeEmpty::default());
|
||||||
|
assert_eq!(value, expected);
|
||||||
|
assert_eq!(toml::to_string(&value).unwrap(), text);
|
||||||
|
}
|
||||||
|
|
|
@ -162,6 +162,9 @@ test!(table_empty,
|
||||||
test!(table_sub_empty,
|
test!(table_sub_empty,
|
||||||
include_str!("valid/table-sub-empty.toml"),
|
include_str!("valid/table-sub-empty.toml"),
|
||||||
include_str!("valid/table-sub-empty.json"));
|
include_str!("valid/table-sub-empty.json"));
|
||||||
|
test!(table_multi_empty,
|
||||||
|
include_str!("valid/table-multi-empty.toml"),
|
||||||
|
include_str!("valid/table-multi-empty.json"));
|
||||||
test!(table_whitespace,
|
test!(table_whitespace,
|
||||||
include_str!("valid/table-whitespace.toml"),
|
include_str!("valid/table-whitespace.toml"),
|
||||||
include_str!("valid/table-whitespace.json"));
|
include_str!("valid/table-whitespace.json"));
|
||||||
|
|
5
tests/valid/table-multi-empty.json
Normal file
5
tests/valid/table-multi-empty.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"a": { "b": {} },
|
||||||
|
"b": {},
|
||||||
|
"c": { "a": {} }
|
||||||
|
}
|
5
tests/valid/table-multi-empty.toml
Normal file
5
tests/valid/table-multi-empty.toml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[a]
|
||||||
|
[a.b]
|
||||||
|
[b]
|
||||||
|
[c]
|
||||||
|
[c.a]
|
Loading…
Reference in a new issue