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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(generify the start_indent and end_indent): now with config depend…
…ance
  • Loading branch information
hube12 committed Mar 15, 2021
commit 8c1430c9e873d61916017e61330ef2c0487b8cfb
38 changes: 12 additions & 26 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,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 @@ -332,11 +336,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 @@ -544,16 +552,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
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);
Expand Down Expand Up @@ -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();
Expand Down