Skip to content

Commit

Permalink
Allow the pretty printer character to be changed
Browse files Browse the repository at this point in the history
This unfortunately loses the simd-ish whitespace printer, but since
pretty printing shouldn't be on a hot path, this shouldn't really
matter.

Partially addresses #65.
  • Loading branch information
erickt committed Apr 26, 2015
1 parent eb9c860 commit e509adc
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/json/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,29 @@ impl Formatter for CompactFormatter {
}
}

pub struct PrettyFormatter {
pub struct PrettyFormatter<'a> {
current_indent: usize,
indent: usize,
indent: &'a [u8],
}

impl Formatter for PrettyFormatter {
impl<'a> PrettyFormatter<'a> {
fn new() -> Self {
PrettyFormatter::with_indent(b" ")
}

fn with_indent(indent: &'a [u8]) -> Self {
PrettyFormatter {
current_indent: 0,
indent: indent,
}
}
}

impl<'a> Formatter for PrettyFormatter<'a> {
fn open<W>(&mut self, writer: &mut W, ch: u8) -> io::Result<()>
where W: io::Write,
{
self.current_indent += self.indent;
self.current_indent += 1;
writer.write_all(&[ch])
}

Expand All @@ -317,7 +330,7 @@ impl Formatter for PrettyFormatter {
try!(writer.write_all(b",\n"));
}

spaces(writer, self.current_indent)
indent(writer, self.current_indent, self.indent)
}

fn colon<W>(&mut self, writer: &mut W) -> io::Result<()>
Expand All @@ -329,9 +342,9 @@ impl Formatter for PrettyFormatter {
fn close<W>(&mut self, writer: &mut W, ch: u8) -> io::Result<()>
where W: io::Write,
{
self.current_indent -= self.indent;
self.current_indent -= 1;
try!(writer.write(b"\n"));
try!(spaces(writer, self.current_indent));
try!(indent(writer, self.current_indent, self.indent));

writer.write_all(&[ch])
}
Expand Down Expand Up @@ -439,10 +452,7 @@ pub fn to_writer_pretty<W, T>(writer: &mut W, value: &T) -> io::Result<()>
where W: io::Write,
T: ser::Serialize,
{
let mut ser = Serializer::new_with_formatter(writer, PrettyFormatter {
current_indent: 0,
indent: 2,
});
let mut ser = Serializer::new_with_formatter(writer, PrettyFormatter::new());
try!(value.serialize(&mut ser));
Ok(())
}
Expand Down Expand Up @@ -489,20 +499,12 @@ pub fn to_string_pretty<T>(value: &T) -> Result<String, FromUtf8Error>
String::from_utf8(vec)
}

fn spaces<W>(wr: &mut W, mut n: usize) -> io::Result<()>
fn indent<W>(wr: &mut W, n: usize, s: &[u8]) -> io::Result<()>
where W: io::Write,
{
const LEN: usize = 16;
const BUF: &'static [u8; LEN] = &[b' '; 16];

while n >= LEN {
try!(wr.write_all(BUF));
n -= LEN;
for _ in 0 .. n {
try!(wr.write_all(s));
}

if n > 0 {
wr.write_all(&BUF[..n])
} else {
Ok(())
}
Ok(())
}

0 comments on commit e509adc

Please sign in to comment.