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 4 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
66 changes: 61 additions & 5 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 @@ -520,7 +544,16 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
self.is_empty = Some(len == 0);
}

self.start_indent()?;
if let Some((ref config, ref mut pretty)) = self.pretty {
hube12 marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down Expand Up @@ -662,13 +695,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, _)) = ser.pretty {
if !config.compact_arrays {
ser.indent()?;
}
} else {
ser.indent()?;
}

value.serialize(&mut **ser)?;

Expand All @@ -682,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 {
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 +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();
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(),
"[(),(),()]"
);
}