From 3ec47e67584d481676b1531080880057af51159e Mon Sep 17 00:00:00 2001 From: Garrett Berg Date: Sun, 9 Jul 2017 15:20:29 -0600 Subject: [PATCH] pretty arrays --- src/ser.rs | 16 ++++++++++++---- tests/pretty.rs | 6 ++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/ser.rs b/src/ser.rs index 2bb9333..4b4ac6e 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -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); }