aboutsummaryrefslogtreecommitdiff
path: root/src/ser.rs
diff options
context:
space:
mode:
authorGarrett Berg <vitiral@gmail.com>2017-07-09 14:58:48 -0600
committerGarrett Berg <vitiral@gmail.com>2017-07-09 14:58:48 -0600
commit391cb61dffd51322bd310be7cba8641fe3398c19 (patch)
tree2c198e0504159af8f2cbe7cdb123e9f69bfb5122 /src/ser.rs
parent3c5a9486cb84c0076ee12b7d40372f819a54ac05 (diff)
downloadmilf-rs-391cb61dffd51322bd310be7cba8641fe3398c19.tar.gz
milf-rs-391cb61dffd51322bd310be7cba8641fe3398c19.zip
add pretty sting serialization
Diffstat (limited to 'src/ser.rs')
-rw-r--r--src/ser.rs41
1 files changed, 38 insertions, 3 deletions
diff --git a/src/ser.rs b/src/ser.rs
index bf12278..d92b8a3 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -214,6 +214,22 @@ impl<'a> Serializer<'a> {
}
}
+ /// instantiate a "pretty" formatter
+ ///
+ /// TODO: add better docs
+ pub fn pretty(dst: &'a mut String) -> Serializer<'a> {
+ Serializer {
+ dst: dst,
+ state: State::End,
+ settings: Settings {
+ array: Some(ArraySettings {
+ indent: 4,
+ }),
+ pretty_string: true,
+ },
+ }
+ }
+
fn display<T: fmt::Display>(&mut self,
t: T,
type_: &'static str) -> Result<(), Error> {
@@ -300,12 +316,27 @@ impl<'a> Serializer<'a> {
}
fn emit_str(&mut self, value: &str) -> Result<(), Error> {
- drop(write!(self.dst, "\""));
+ let do_pretty = if self.settings.pretty_string {
+ value.contains('\n')
+ } else {
+ false
+ };
+ if do_pretty {
+ drop(write!(self.dst, "'''\n"));
+ } else {
+ drop(write!(self.dst, "\""));
+ }
for ch in value.chars() {
match ch {
'\u{8}' => drop(write!(self.dst, "\\b")),
'\u{9}' => drop(write!(self.dst, "\\t")),
- '\u{a}' => drop(write!(self.dst, "\\n")),
+ '\u{a}' => {
+ if do_pretty {
+ drop(write!(self.dst, "\n"));
+ } else {
+ drop(write!(self.dst, "\\n"));
+ }
+ },
'\u{c}' => drop(write!(self.dst, "\\f")),
'\u{d}' => drop(write!(self.dst, "\\r")),
'\u{22}' => drop(write!(self.dst, "\\\"")),
@@ -316,7 +347,11 @@ impl<'a> Serializer<'a> {
ch => drop(write!(self.dst, "{}", ch)),
}
}
- drop(write!(self.dst, "\""));
+ if do_pretty {
+ drop(write!(self.dst, "'''"));
+ } else {
+ drop(write!(self.dst, "\""));
+ }
Ok(())
}