use builder pattern
This commit is contained in:
parent
81d7303b19
commit
1e060c55d6
84
src/ser.rs
84
src/ser.rs
|
@ -137,18 +137,27 @@ pub enum Error {
|
||||||
__Nonexhaustive,
|
__Nonexhaustive,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If given in `Settings` will result in a "pretty array" with the given settings
|
|
||||||
/// TODO: add better docs
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct ArraySettings {
|
#[doc(hidden)]
|
||||||
indent: u64,
|
/// Internal place for holding array setings
|
||||||
|
struct ArraySettings {
|
||||||
|
indent: usize,
|
||||||
trailing_comma: bool,
|
trailing_comma: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Formatting Settings
|
impl ArraySettings {
|
||||||
/// TODO: add better docs
|
fn pretty() -> ArraySettings {
|
||||||
|
ArraySettings {
|
||||||
|
indent: 4,
|
||||||
|
trailing_comma: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct Settings {
|
#[doc(hidden)]
|
||||||
|
/// Internal struct for holding serialization settings
|
||||||
|
struct Settings {
|
||||||
array: Option<ArraySettings>,
|
array: Option<ArraySettings>,
|
||||||
pretty_string: bool,
|
pretty_string: bool,
|
||||||
}
|
}
|
||||||
|
@ -215,23 +224,72 @@ impl<'a> Serializer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// instantiate a "pretty" formatter
|
/// Instantiate a "pretty" formatter
|
||||||
///
|
///
|
||||||
/// TODO: add better docs
|
/// 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> {
|
pub fn pretty(dst: &'a mut String) -> Serializer<'a> {
|
||||||
Serializer {
|
Serializer {
|
||||||
dst: dst,
|
dst: dst,
|
||||||
state: State::End,
|
state: State::End,
|
||||||
settings: Settings {
|
settings: Settings {
|
||||||
array: Some(ArraySettings {
|
array: Some(ArraySettings::pretty()),
|
||||||
indent: 4,
|
|
||||||
trailing_comma: true,
|
|
||||||
}),
|
|
||||||
pretty_string: true,
|
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,
|
fn display<T: fmt::Display>(&mut self,
|
||||||
t: T,
|
t: T,
|
||||||
type_: &'static str) -> Result<(), Error> {
|
type_: &'static str) -> Result<(), Error> {
|
||||||
|
|
Loading…
Reference in a new issue