| 1 |
use core::fmt::Write as _; |
| 2 |
|
| 3 |
use toml_writer::TomlWrite as _; |
| 4 |
|
| 5 |
use super::style::Style; |
| 6 |
use super::value::ValueSerializer; |
| 7 |
use super::Buffer; |
| 8 |
use super::Error; |
| 9 |
use super::Table; |
| 10 |
|
| 11 |
#[doc(hidden)] |
| 12 |
pub struct SerializeDocumentTupleVariant<'d> { |
| 13 |
buf: &'d mut Buffer, |
| 14 |
table: Table, |
| 15 |
seen_value: bool, |
| 16 |
style: Style, |
| 17 |
} |
| 18 |
|
| 19 |
impl<'d> SerializeDocumentTupleVariant<'d> { |
| 20 |
pub(crate) fn tuple( |
| 21 |
buf: &'d mut Buffer, |
| 22 |
mut table: Table, |
| 23 |
variant: &'static str, |
| 24 |
_len: usize, |
| 25 |
style: Style, |
| 26 |
) -> Result<Self, Error> { |
| 27 |
let dst = table.body_mut(); |
| 28 |
dst.key(variant)?; |
| 29 |
dst.space()?; |
| 30 |
dst.keyval_sep()?; |
| 31 |
dst.space()?; |
| 32 |
dst.open_array()?; |
| 33 |
Ok(Self { |
| 34 |
buf, |
| 35 |
table, |
| 36 |
seen_value: false, |
| 37 |
style, |
| 38 |
}) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
impl<'d> serde_core::ser::SerializeTupleVariant for SerializeDocumentTupleVariant<'d> { |
| 43 |
type Ok = &'d mut Buffer; |
| 44 |
type Error = Error; |
| 45 |
|
| 46 |
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Error> |
| 47 |
where |
| 48 |
T: serde_core::ser::Serialize + ?Sized, |
| 49 |
{ |
| 50 |
let dst = self.table.body_mut(); |
| 51 |
|
| 52 |
if self.style.multiline_array { |
| 53 |
dst.newline()?; |
| 54 |
write!(dst, " ")?; |
| 55 |
} else { |
| 56 |
if self.seen_value { |
| 57 |
dst.val_sep()?; |
| 58 |
dst.space()?; |
| 59 |
} |
| 60 |
} |
| 61 |
self.seen_value = true; |
| 62 |
value.serialize(ValueSerializer::with_style(dst, self.style))?; |
| 63 |
if self.style.multiline_array { |
| 64 |
dst.val_sep()?; |
| 65 |
} |
| 66 |
Ok(()) |
| 67 |
} |
| 68 |
|
| 69 |
fn end(mut self) -> Result<Self::Ok, Self::Error> { |
| 70 |
let dst = self.table.body_mut(); |
| 71 |
if self.style.multiline_array && self.seen_value { |
| 72 |
dst.newline()?; |
| 73 |
} |
| 74 |
dst.close_array()?; |
| 75 |
dst.newline()?; |
| 76 |
self.buf.push(self.table); |
| 77 |
Ok(self.buf) |
| 78 |
} |
| 79 |
} |