diff options
author | Garrett Berg <vitiral@gmail.com> | 2017-07-09 15:20:29 -0600 |
---|---|---|
committer | Garrett Berg <vitiral@gmail.com> | 2017-07-09 15:20:29 -0600 |
commit | ae6096ab2611750fe44d3092b407d496ab94b0b9 (patch) | |
tree | aceb38bf1955662b18e439e2033b4c683cd0b816 | |
parent | ffdafc4d361df0d6f6063af50863b7f87f4aa46a (diff) | |
download | milf-rs-ae6096ab2611750fe44d3092b407d496ab94b0b9.tar.gz milf-rs-ae6096ab2611750fe44d3092b407d496ab94b0b9.zip |
pretty arrays
-rw-r--r-- | src/ser.rs | 16 | ||||
-rw-r--r-- | tests/pretty.rs | 6 |
2 files changed, 18 insertions, 4 deletions
@@ -142,6 +142,7 @@ pub enum Error { #[derive(Debug, Default, Clone)] pub struct ArraySettings { indent: u64, + trailing_comma: bool, } /// Formatting Settings @@ -224,6 +225,7 @@ impl<'a> Serializer<'a> { settings: Settings { array: Some(ArraySettings { indent: 4, + trailing_comma: true, }), pretty_string: true, }, @@ -667,10 +669,16 @@ impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> { match self.type_.get() { Some("table") => return Ok(()), Some(_) => { - if self.ser.settings.array.is_some() { - self.ser.dst.push_str("\n]"); - } else { - self.ser.dst.push_str("]"); + match self.ser.settings.array { + Some(ref a) => { + if a.trailing_comma { + self.ser.dst.push_str(","); + } + self.ser.dst.push_str("\n]"); + }, + None => { + self.ser.dst.push_str("]"); + } } } None => { diff --git a/tests/pretty.rs b/tests/pretty.rs index 6758cfb..718ef07 100644 --- a/tests/pretty.rs +++ b/tests/pretty.rs @@ -5,6 +5,10 @@ use serde::ser::Serialize; const EXAMPLE: &str = "\ [example] +array = [ + \"item 1\", + \"item 2\", +] text = ''' this is the first line this is the second line @@ -16,5 +20,7 @@ fn test_pretty() { let value: toml::Value = toml::from_str(EXAMPLE).unwrap(); let mut result = String::with_capacity(128); value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap(); + println!("example:\n{}", EXAMPLE); + println!("result:\n{}", result); assert_eq!(EXAMPLE, &result); } |