diff --git a/src/ser/mod.rs b/src/ser/mod.rs index ca1cea3d..130d4ee6 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -91,6 +91,8 @@ pub struct PrettyConfig { pub decimal_floats: bool, /// Enable extensions. Only configures 'implicit_some' for now. pub extensions: Extensions, + /// Enable compact arrays + pub compact_arrays: bool, /// Private field to ensure adding a field is non-breaking. #[serde(skip)] _future_proof: (), @@ -165,6 +167,23 @@ impl PrettyConfig { self } + /// Configures whether every array should be a single line (true) or a multi line one (false) + /// When false `["a","b"]` (as well as any array) will serialize to + /// ` + /// [ + /// "a", + /// "b", + /// ] + /// ` + /// When true `["a","b"]` (as well as any array) will serialize to `["a","b"]` + /// + /// Default: `false` + pub fn with_compact_arrays(mut self, compact_arrays: bool) -> Self { + self.compact_arrays = compact_arrays; + + self + } + /// Configures extensions /// /// Default: Extensions::empty() @@ -204,6 +223,10 @@ fn default_enumerate_arrays() -> bool { false } +fn default_compact_arrays() -> bool { + false +} + impl Default for PrettyConfig { fn default() -> Self { PrettyConfig { @@ -214,6 +237,7 @@ impl Default for PrettyConfig { enumerate_arrays: default_enumerate_arrays(), extensions: Extensions::default(), decimal_floats: default_decimal_floats(), + compact_arrays: default_compact_arrays(), _future_proof: (), } } @@ -283,12 +307,16 @@ impl Serializer { } fn start_indent(&mut self) -> Result<()> { + self.start_indent_if(|_| true) + } + + fn start_indent_if(&mut self, condition: impl Fn(&PrettyConfig) -> bool) -> Result<()> { if let Some((ref config, ref mut pretty)) = self.pretty { pretty.indent += 1; if pretty.indent <= config.depth_limit { let is_empty = self.is_empty.unwrap_or(false); - if !is_empty { + if !is_empty && condition(config) { self.output.write_all(config.new_line.as_bytes())?; } } @@ -297,8 +325,12 @@ impl Serializer { } fn indent(&mut self) -> io::Result<()> { + self.indent_if(|_| true) + } + + fn indent_if(&mut self, condition: impl Fn(&PrettyConfig) -> bool) -> io::Result<()> { if let Some((ref config, ref pretty)) = self.pretty { - if pretty.indent <= config.depth_limit { + if pretty.indent <= config.depth_limit && condition(config) { for _ in 0..pretty.indent { self.output.write_all(config.indentor.as_bytes())?; } @@ -308,11 +340,15 @@ impl Serializer { } fn end_indent(&mut self) -> io::Result<()> { + self.end_indent_if(|_| true) + } + + fn end_indent_if(&mut self, condition: impl Fn(&PrettyConfig) -> bool) -> io::Result<()> { if let Some((ref config, ref mut pretty)) = self.pretty { if pretty.indent <= config.depth_limit { let is_empty = self.is_empty.unwrap_or(false); - if !is_empty { + if !is_empty && condition(config) { for _ in 1..pretty.indent { self.output.write_all(config.indentor.as_bytes())?; } @@ -520,7 +556,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { self.is_empty = Some(len == 0); } - self.start_indent()?; + self.start_indent_if(|config| !config.compact_arrays)?; if let Some((_, ref mut pretty)) = self.pretty { pretty.sequence_index.push(0); @@ -662,13 +698,16 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { write!(ser.output, "// [{}]", index).unwrap(); *index += 1; } - ser.output.write_all(config.new_line.as_bytes())?; + if !config.compact_arrays { + ser.output.write_all(config.new_line.as_bytes())?; + } } } ser } }; - ser.indent()?; + + ser.indent_if(|config| !config.compact_arrays)?; value.serialize(&mut **ser)?; @@ -682,7 +721,7 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { state: State::Rest, } => { if let Some((ref config, ref mut pretty)) = ser.pretty { - if pretty.indent <= config.depth_limit { + if pretty.indent <= config.depth_limit && !config.compact_arrays { ser.output.write_all(b",")?; ser.output.write_all(config.new_line.as_bytes())?; } @@ -691,7 +730,8 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { } Compound::Map { ser, .. } => ser, }; - ser.end_indent()?; + + ser.end_indent_if(|config| !config.compact_arrays)?; if let Some((_, ref mut pretty)) = ser.pretty { pretty.sequence_index.pop(); diff --git a/tests/240_array_pretty.rs b/tests/240_array_pretty.rs index 957be157..526ce536 100644 --- a/tests/240_array_pretty.rs +++ b/tests/240_array_pretty.rs @@ -4,11 +4,27 @@ use ron::ser::{to_string_pretty, PrettyConfig}; fn small_array() { let arr = &[(), (), ()][..]; assert_eq!( - to_string_pretty(&arr, PrettyConfig::new().with_new_line("\n".to_string())).unwrap(), + to_string_pretty( + &arr, + PrettyConfig::new() + .with_new_line("\n".to_string()) + .with_compact_arrays(false), + ) + .unwrap(), "[ (), (), (), ]" ); + assert_eq!( + to_string_pretty( + &arr, + PrettyConfig::new() + .with_new_line("\n".to_string()) + .with_compact_arrays(true) + ) + .unwrap(), + "[(),(),()]" + ); }