From 639583c7e84c203519acf3a159b2aef6cf579a7e Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 12 Mar 2021 11:31:18 +0100 Subject: [PATCH 1/6] feat(add compact arrays): we can now have nice one line syntax for arrays --- src/ser/mod.rs | 47 +++++++++++++++++++++++++++++++++++---- tests/240_array_pretty.rs | 7 +++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index ca1cea3d..2c4b5f28 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,22 @@ 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 +222,8 @@ fn default_enumerate_arrays() -> bool { false } +fn default_compact_arrays()->bool{false} + impl Default for PrettyConfig { fn default() -> Self { PrettyConfig { @@ -214,6 +234,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: (), } } @@ -520,7 +541,16 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { self.is_empty = Some(len == 0); } - self.start_indent()?; + 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 && !config.compact_arrays { + self.output.write_all(config.new_line.as_bytes())?; + } + } + } if let Some((_, ref mut pretty)) = self.pretty { pretty.sequence_index.push(0); @@ -662,13 +692,22 @@ 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()?; + + if let Some((ref config, ref mut pretty)) = ser.pretty { + if !config.compact_arrays{ + ser.indent()?; + } + }else{ + ser.indent()?; + } 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())?; } diff --git a/tests/240_array_pretty.rs b/tests/240_array_pretty.rs index 957be157..8ab6ef12 100644 --- a/tests/240_array_pretty.rs +++ b/tests/240_array_pretty.rs @@ -4,11 +4,16 @@ 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(), + "[(),(),()]" + ); + } From 60b6d5e2b50d07bf2d595e3fb445631442bba737 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 12 Mar 2021 11:42:07 +0100 Subject: [PATCH 2/6] fix(correct test and fmt): --- src/ser/mod.rs | 19 +++++++++++-------- tests/240_array_pretty.rs | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 2c4b5f28..188a8ddb 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -92,7 +92,7 @@ pub struct PrettyConfig { /// Enable extensions. Only configures 'implicit_some' for now. pub extensions: Extensions, /// Enable compact arrays - pub compact_arrays:bool, + pub compact_arrays: bool, /// Private field to ensure adding a field is non-breaking. #[serde(skip)] _future_proof: (), @@ -169,11 +169,12 @@ impl PrettyConfig { /// 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` @@ -222,7 +223,9 @@ fn default_enumerate_arrays() -> bool { false } -fn default_compact_arrays()->bool{false} +fn default_compact_arrays() -> bool { + false +} impl Default for PrettyConfig { fn default() -> Self { @@ -546,7 +549,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { if pretty.indent <= config.depth_limit { let is_empty = self.is_empty.unwrap_or(false); - if !is_empty && !config.compact_arrays { + if !is_empty && !config.compact_arrays { self.output.write_all(config.new_line.as_bytes())?; } } @@ -692,7 +695,7 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { write!(ser.output, "// [{}]", index).unwrap(); *index += 1; } - if !config.compact_arrays{ + if !config.compact_arrays { ser.output.write_all(config.new_line.as_bytes())?; } } @@ -702,10 +705,10 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { }; if let Some((ref config, ref mut pretty)) = ser.pretty { - if !config.compact_arrays{ + if !config.compact_arrays { ser.indent()?; } - }else{ + } else { ser.indent()?; } @@ -721,7 +724,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 && !config.compact_arrays{ + if pretty.indent <= config.depth_limit && !config.compact_arrays { ser.output.write_all(b",")?; ser.output.write_all(config.new_line.as_bytes())?; } diff --git a/tests/240_array_pretty.rs b/tests/240_array_pretty.rs index 8ab6ef12..526ce536 100644 --- a/tests/240_array_pretty.rs +++ b/tests/240_array_pretty.rs @@ -4,7 +4,13 @@ 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()).with_compact_arrays(false)).unwrap(), + to_string_pretty( + &arr, + PrettyConfig::new() + .with_new_line("\n".to_string()) + .with_compact_arrays(false), + ) + .unwrap(), "[ (), (), @@ -12,8 +18,13 @@ fn small_array() { ]" ); assert_eq!( - to_string_pretty(&arr, PrettyConfig::new().with_new_line("\n".to_string()).with_compact_arrays(true)).unwrap(), + to_string_pretty( + &arr, + PrettyConfig::new() + .with_new_line("\n".to_string()) + .with_compact_arrays(true) + ) + .unwrap(), "[(),(),()]" ); - } From 199d10540d36c364cf22b4a4298c7eb0b6f72257 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 12 Mar 2021 11:44:55 +0100 Subject: [PATCH 3/6] fix(remove a warning): --- src/ser/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 188a8ddb..b042fdb4 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -704,7 +704,7 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { } }; - if let Some((ref config, ref mut pretty)) = ser.pretty { + if let Some((ref config, _)) = ser.pretty { if !config.compact_arrays { ser.indent()?; } From a9738a1d4b674a66adc227a9e400408c6cbf70f6 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 12 Mar 2021 11:54:48 +0100 Subject: [PATCH 4/6] fix(end indent being out of proportions in nested compact arrays): --- src/ser/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index b042fdb4..1ff3070a 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -733,7 +733,21 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { } Compound::Map { ser, .. } => ser, }; - ser.end_indent()?; + + if let Some((ref config, ref mut pretty)) = ser.pretty { + if pretty.indent <= config.depth_limit { + let is_empty = ser.is_empty.unwrap_or(false); + + if !is_empty && !config.compact_arrays { + for _ in 1..pretty.indent { + ser.output.write_all(config.indentor.as_bytes())?; + } + } + } + pretty.indent -= 1; + + ser.is_empty = None; + } if let Some((_, ref mut pretty)) = ser.pretty { pretty.sequence_index.pop(); From 8c1430c9e873d61916017e61330ef2c0487b8cfb Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 15 Mar 2021 12:21:29 +0100 Subject: [PATCH 5/6] fix(generify the start_indent and end_indent): now with config dependance --- src/ser/mod.rs | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 1ff3070a..d121cb4c 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -307,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())?; } } @@ -332,11 +336,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())?; } @@ -544,16 +552,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer { self.is_empty = Some(len == 0); } - 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 && !config.compact_arrays { - self.output.write_all(config.new_line.as_bytes())?; - } - } - } + self.start_indent_if(|config| !config.compact_arrays)?; if let Some((_, ref mut pretty)) = self.pretty { pretty.sequence_index.push(0); @@ -734,20 +733,7 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { Compound::Map { ser, .. } => ser, }; - if let Some((ref config, ref mut pretty)) = ser.pretty { - if pretty.indent <= config.depth_limit { - let is_empty = ser.is_empty.unwrap_or(false); - - if !is_empty && !config.compact_arrays { - for _ in 1..pretty.indent { - ser.output.write_all(config.indentor.as_bytes())?; - } - } - } - pretty.indent -= 1; - - ser.is_empty = None; - } + ser.end_indent_if(|config| !config.compact_arrays)?; if let Some((_, ref mut pretty)) = ser.pretty { pretty.sequence_index.pop(); From ae55ca1594b7945dfa06f32e897cad51ab582295 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 15 Mar 2021 12:28:20 +0100 Subject: [PATCH 6/6] fix(generify indent too): remove unecessary lines --- src/ser/mod.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index d121cb4c..130d4ee6 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -325,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())?; } @@ -703,13 +707,7 @@ impl<'a, W: io::Write> ser::SerializeSeq for Compound<'a, W> { } }; - if let Some((ref config, _)) = ser.pretty { - if !config.compact_arrays { - ser.indent()?; - } - } else { - ser.indent()?; - } + ser.indent_if(|config| !config.compact_arrays)?; value.serialize(&mut **ser)?;