aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett Berg <vitiral@gmail.com>2017-08-13 16:15:15 -0600
committerGarrett Berg <vitiral@gmail.com>2017-08-13 17:19:02 -0600
commiteaa0c17220dc8ea1f5ed0b6bdb71ecbe32305404 (patch)
treec40738dd0041bc8a21400a937df2ca66eba1367a /src
parent4b3139e2b66896376dd257040014a313db5fcc1e (diff)
downloadmilf-rs-eaa0c17220dc8ea1f5ed0b6bdb71ecbe32305404.tar.gz
milf-rs-eaa0c17220dc8ea1f5ed0b6bdb71ecbe32305404.zip
add pretty_string_literal to be able to disable literal strings
Diffstat (limited to 'src')
-rw-r--r--src/ser.rs79
1 files changed, 69 insertions, 10 deletions
diff --git a/src/ser.rs b/src/ser.rs
index 3c4c220..ef037ed 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -169,9 +169,19 @@ impl ArraySettings {
#[doc(hidden)]
#[derive(Debug, Default, Clone)]
-/// String settings. Currently empty but may contain settings
-/// eventually.
-struct StringSettings();
+/// String settings
+struct StringSettings {
+ /// Whether to use literal strings when possible
+ literal: bool,
+}
+
+impl StringSettings {
+ fn pretty() -> StringSettings {
+ StringSettings {
+ literal: true,
+ }
+ }
+}
#[derive(Debug, Default, Clone)]
#[doc(hidden)]
@@ -259,7 +269,7 @@ impl<'a> Serializer<'a> {
state: State::End,
settings: Rc::new(Settings {
array: Some(ArraySettings::pretty()),
- string: Some(StringSettings()),
+ string: Some(StringSettings::pretty()),
}),
}
}
@@ -289,13 +299,56 @@ impl<'a> Serializer<'a> {
/// ```
pub fn pretty_string(&mut self, value: bool) -> &mut Self {
Rc::get_mut(&mut self.settings).unwrap().string = if value {
- Some(StringSettings())
+ Some(StringSettings::pretty())
} else {
None
};
self
}
+ /// Enable or Disable Literal strings for pretty strings
+ ///
+ /// If enabled, literal strings will be used when possible and strings with
+ /// one or more newlines will use triple quotes (i.e.: `'''` or `"""`)
+ ///
+ /// If disabled, literal strings will NEVER be used and strings with one or
+ /// more newlines will use `"""`
+ ///
+ /// # Examples
+ ///
+ /// Instead of:
+ ///
+ /// ```toml,ignore
+ /// single = "no newlines"
+ /// text = "\nfoo\nbar\n"
+ /// ```
+ ///
+ /// You will have:
+ ///
+ /// ```toml,ignore
+ /// single = "no newlines"
+ /// text = """
+ /// foo
+ /// bar
+ /// """
+ /// ```
+ pub fn pretty_string_literal(&mut self, value: bool) -> &mut Self {
+ let use_default = if let &mut Some(ref mut s) = &mut Rc::get_mut(&mut self.settings)
+ .unwrap().string {
+ s.literal = value;
+ false
+ } else {
+ true
+ };
+
+ if use_default {
+ let mut string = StringSettings::pretty();
+ string.literal = value;
+ Rc::get_mut(&mut self.settings).unwrap().string = Some(string);
+ }
+ self
+ }
+
/// Enable or Disable pretty arrays
///
/// If enabled, arrays will always have each item on their own line.
@@ -548,7 +601,11 @@ impl<'a> Serializer<'a> {
}
let repr = if !is_key && self.settings.string.is_some() {
- do_pretty(value)
+ match (&self.settings.string, do_pretty(value)) {
+ (&Some(StringSettings { literal: false, .. }), Repr::Literal(_, ty)) =>
+ Repr::Std(ty),
+ (_, r @ _) => r,
+ }
} else {
Repr::Std(Type::OnelineSingle)
};
@@ -569,8 +626,11 @@ impl<'a> Serializer<'a> {
Repr::Std(ty) => {
match ty {
Type::NewlineTripple => self.dst.push_str("\"\"\"\n"),
- Type::OnelineSingle => self.dst.push('"'),
- _ => unreachable!(),
+ // note: OnelineTripple can happen if do_pretty wants to do
+ // '''it's one line'''
+ // but settings.string.literal == false
+ Type::OnelineSingle |
+ Type::OnelineTripple => self.dst.push('"'),
}
for ch in value.chars() {
match ch {
@@ -593,8 +653,7 @@ impl<'a> Serializer<'a> {
}
match ty {
Type::NewlineTripple => self.dst.push_str("\"\"\""),
- Type::OnelineSingle => self.dst.push('"'),
- _ => unreachable!(),
+ Type::OnelineSingle | Type::OnelineTripple => self.dst.push('"'),
}
},
}