Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(add compact arrays): we can now have nice one line syntax for arrays #299

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: (),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -204,6 +223,10 @@ fn default_enumerate_arrays() -> bool {
false
}

fn default_compact_arrays() -> bool {
false
}

impl Default for PrettyConfig {
fn default() -> Self {
PrettyConfig {
Expand All @@ -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: (),
}
}
Expand Down Expand Up @@ -283,12 +307,16 @@ impl<W: io::Write> Serializer<W> {
}

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())?;
}
}
Expand All @@ -297,8 +325,12 @@ impl<W: io::Write> Serializer<W> {
}

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())?;
}
Expand All @@ -308,11 +340,15 @@ impl<W: io::Write> Serializer<W> {
}

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())?;
}
Expand Down Expand Up @@ -520,7 +556,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
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);
Expand Down Expand Up @@ -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)?;

Expand All @@ -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())?;
}
Expand All @@ -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();
Expand Down
18 changes: 17 additions & 1 deletion tests/240_array_pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
"[(),(),()]"
);
}