aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett Berg <vitiral@gmail.com>2017-07-20 12:47:51 -0600
committerGarrett Berg <vitiral@gmail.com>2017-07-20 12:47:51 -0600
commit8e4813618556165542bf953140713f6dc71b62b8 (patch)
tree830d55ab0398d5e4651bb23accc16ac462459d5a /src
parentef8af0e52a01f1a7c92bff6f5bebde805e19d022 (diff)
downloadmilf-rs-8e4813618556165542bf953140713f6dc71b62b8.tar.gz
milf-rs-8e4813618556165542bf953140713f6dc71b62b8.zip
use builder pattern
Diffstat (limited to 'src')
-rw-r--r--src/ser.rs86
1 files changed, 72 insertions, 14 deletions
diff --git a/src/ser.rs b/src/ser.rs
index 4b4ac6e..2431737 100644
--- a/src/ser.rs
+++ b/src/ser.rs
@@ -137,18 +137,27 @@ pub enum Error {
__Nonexhaustive,
}
-/// If given in `Settings` will result in a "pretty array" with the given settings
-/// TODO: add better docs
#[derive(Debug, Default, Clone)]
-pub struct ArraySettings {
- indent: u64,
+#[doc(hidden)]
+/// Internal place for holding array setings
+struct ArraySettings {
+ indent: usize,
trailing_comma: bool,
}
-/// Formatting Settings
-/// TODO: add better docs
+impl ArraySettings {
+ fn pretty() -> ArraySettings {
+ ArraySettings {
+ indent: 4,
+ trailing_comma: true,
+ }
+ }
+}
+
#[derive(Debug, Default, Clone)]
-pub struct Settings {
+#[doc(hidden)]
+/// Internal struct for holding serialization settings
+struct Settings {
array: Option<ArraySettings>,
pretty_string: bool,
}
@@ -215,23 +224,72 @@ impl<'a> Serializer<'a> {
}
}
- /// instantiate a "pretty" formatter
- ///
- /// TODO: add better docs
+ /// Instantiate a "pretty" formatter
+ ///
+ /// By default this will use :
+ /// - pretty strings
+ /// - arrays with an indentation of 4 and a trailing comma
pub fn pretty(dst: &'a mut String) -> Serializer<'a> {
Serializer {
dst: dst,
state: State::End,
settings: Settings {
- array: Some(ArraySettings {
- indent: 4,
- trailing_comma: true,
- }),
+ array: Some(ArraySettings::pretty()),
pretty_string: true,
},
}
}
+ /// Enable or Disable pretty strings
+ pub fn pretty_string(&mut self, value: bool) -> &mut Self {
+ self.settings.pretty_string = value;
+ self
+ }
+
+ /// Enable or Disable pretty arrays
+ pub fn pretty_array(&mut self, value: bool) -> &mut Self {
+ self.settings.array = Some(if value {
+ ArraySettings::pretty()
+ } else {
+ ArraySettings::default()
+ });
+ self
+ }
+
+ /// Set the indent to value for pretty arrays
+ pub fn pretty_array_indent(&mut self, value: usize) -> &mut Self {
+ let use_default = if let &mut Some(ref mut a) = &mut self.settings.array {
+ a.indent = value;
+ false
+ } else {
+ true
+ };
+
+ if use_default {
+ let mut array = ArraySettings::pretty();
+ array.indent = value;
+ self.settings.array = Some(array);
+ }
+ self
+ }
+
+ /// Specify whether to use a trailing comma when serializing pretty arrays
+ pub fn pretty_array_trailing_comma(&mut self, value: bool) -> &mut Self {
+ let use_default = if let &mut Some(ref mut a) = &mut self.settings.array {
+ a.trailing_comma = value;
+ false
+ } else {
+ true
+ };
+
+ if use_default {
+ let mut array = ArraySettings::pretty();
+ array.trailing_comma = value;
+ self.settings.array = Some(array);
+ }
+ self
+ }
+
fn display<T: fmt::Display>(&mut self,
t: T,
type_: &'static str) -> Result<(), Error> {