pretty arrays
This commit is contained in:
parent
27757113ec
commit
3ec47e6758
12
src/ser.rs
12
src/ser.rs
|
@ -142,6 +142,7 @@ pub enum Error {
|
|||
#[derive(Debug, Default, Clone)]
|
||||
pub struct ArraySettings {
|
||||
indent: u64,
|
||||
trailing_comma: bool,
|
||||
}
|
||||
|
||||
/// Formatting Settings
|
||||
|
@ -224,6 +225,7 @@ impl<'a> Serializer<'a> {
|
|||
settings: Settings {
|
||||
array: Some(ArraySettings {
|
||||
indent: 4,
|
||||
trailing_comma: true,
|
||||
}),
|
||||
pretty_string: true,
|
||||
},
|
||||
|
@ -667,12 +669,18 @@ impl<'a, 'b> ser::SerializeSeq for SerializeSeq<'a, 'b> {
|
|||
match self.type_.get() {
|
||||
Some("table") => return Ok(()),
|
||||
Some(_) => {
|
||||
if self.ser.settings.array.is_some() {
|
||||
match self.ser.settings.array {
|
||||
Some(ref a) => {
|
||||
if a.trailing_comma {
|
||||
self.ser.dst.push_str(",");
|
||||
}
|
||||
self.ser.dst.push_str("\n]");
|
||||
} else {
|
||||
},
|
||||
None => {
|
||||
self.ser.dst.push_str("]");
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
assert!(self.first.get());
|
||||
self.ser.emit_key("array")?;
|
||||
|
|
|
@ -5,6 +5,10 @@ use serde::ser::Serialize;
|
|||
|
||||
const EXAMPLE: &str = "\
|
||||
[example]
|
||||
array = [
|
||||
\"item 1\",
|
||||
\"item 2\",
|
||||
]
|
||||
text = '''
|
||||
this is the first line
|
||||
this is the second line
|
||||
|
@ -16,5 +20,7 @@ fn test_pretty() {
|
|||
let value: toml::Value = toml::from_str(EXAMPLE).unwrap();
|
||||
let mut result = String::with_capacity(128);
|
||||
value.serialize(&mut toml::Serializer::pretty(&mut result)).unwrap();
|
||||
println!("example:\n{}", EXAMPLE);
|
||||
println!("result:\n{}", result);
|
||||
assert_eq!(EXAMPLE, &result);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue