array doesn't break anything...

This commit is contained in:
Garrett Berg 2017-07-09 15:10:36 -06:00
parent fe894fee6c
commit 27757113ec
2 changed files with 28 additions and 8 deletions

View file

@ -274,10 +274,24 @@ impl<'a> Serializer<'a> {
} }
fn emit_array(&mut self, first: &Cell<bool>) -> Result<(), Error> { fn emit_array(&mut self, first: &Cell<bool>) -> Result<(), Error> {
if first.get() { match self.settings.array {
self.dst.push_str("["); Some(ref a) => {
} else { if first.get() {
self.dst.push_str(", "); self.dst.push_str("[\n")
} else {
self.dst.push_str(",\n")
}
for _ in 0..a.indent {
self.dst.push_str(" ");
}
},
None => {
if first.get() {
self.dst.push_str("[")
} else {
self.dst.push_str(", ")
}
},
} }
Ok(()) Ok(())
} }
@ -652,7 +666,13 @@ impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> {
fn end(self) -> Result<(), Error> { fn end(self) -> Result<(), Error> {
match self.type_.get() { match self.type_.get() {
Some("table") => return Ok(()), Some("table") => return Ok(()),
Some(_) => self.ser.dst.push_str("]"), Some(_) => {
if self.ser.settings.array.is_some() {
self.ser.dst.push_str("\n]");
} else {
self.ser.dst.push_str("]");
}
}
None => { None => {
assert!(self.first.get()); assert!(self.first.get());
self.ser.emit_key("array")?; self.ser.emit_key("array")?;

View file

@ -3,7 +3,7 @@ extern crate serde;
use serde::ser::Serialize; use serde::ser::Serialize;
const example: &str = "\ const EXAMPLE: &str = "\
[example] [example]
text = ''' text = '''
this is the first line this is the first line
@ -13,8 +13,8 @@ this is the second line
#[test] #[test]
fn test_pretty() { fn test_pretty() {
let value: toml::Value = toml::from_str(example).unwrap(); let value: toml::Value = toml::from_str(EXAMPLE).unwrap();
let mut result = String::with_capacity(128); let mut result = String::with_capacity(128);
value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap(); value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap();
assert_eq!(example, &result); assert_eq!(EXAMPLE, &result);
} }