toml/ser/document/
mod.rs

1//! Serializing Rust structures into TOML.
2//!
3//! This module contains all the Serde support for serializing Rust structures
4//! into TOML documents (as strings). Note that some top-level functions here
5//! are also provided at the top of the crate.
6
7mod array;
8mod array_of_tables;
9mod buffer;
10mod map;
11mod strategy;
12
13use toml_writer::TomlWrite as _;
14
15use super::style;
16use super::value;
17use super::Error;
18use crate::alloc_prelude::*;
19use buffer::Table;
20use strategy::SerializationStrategy;
21
22pub use buffer::Buffer;
23
24/// Serialization for TOML documents.
25///
26/// This structure implements serialization support for TOML to serialize an
27/// arbitrary type to TOML. Note that the TOML format does not support all
28/// datatypes in Rust, such as enums, tuples, and tuple structs. These types
29/// will generate an error when serialized.
30///
31/// Currently a serializer always writes its output to an in-memory `String`,
32/// which is passed in when creating the serializer itself.
33///
34/// To serialize TOML values, instead of documents, see
35/// [`ValueSerializer`][super::value::ValueSerializer].
36pub struct Serializer<'d> {
37    buf: &'d mut Buffer,
38    style: style::Style,
39    table: Table,
40}
41
42impl<'d> Serializer<'d> {
43    /// Creates a new serializer which will emit TOML into the buffer provided.
44    ///
45    /// The serializer can then be used to serialize a type after which the data
46    /// will be present in `buf`.
47    pub fn new(buf: &'d mut Buffer) -> Self {
48        let table = buf.root_table();
49        Self {
50            buf,
51            style: Default::default(),
52            table,
53        }
54    }
55
56    /// Apply a default "pretty" policy to the document
57    ///
58    /// For greater customization, instead serialize to a
59    /// [`toml_edit::DocumentMut`](https://docs.rs/toml_edit/latest/toml_edit/struct.DocumentMut.html).
60    pub fn pretty(buf: &'d mut Buffer) -> Self {
61        let mut ser = Serializer::new(buf);
62        ser.style.multiline_array = true;
63        ser
64    }
65
66    pub(crate) fn with_table(buf: &'d mut Buffer, table: Table, style: style::Style) -> Self {
67        Self { buf, style, table }
68    }
69
70    fn end(self) -> Result<&'d mut Buffer, Error> {
71        self.buf.push(self.table);
72        Ok(self.buf)
73    }
74}
75
76impl<'d> serde::ser::Serializer for Serializer<'d> {
77    type Ok = &'d mut Buffer;
78    type Error = Error;
79    type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>;
80    type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>;
81    type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
82    type SerializeTupleVariant = array::SerializeDocumentTupleVariant<'d>;
83    type SerializeMap = map::SerializeDocumentTable<'d>;
84    type SerializeStruct = map::SerializeDocumentTable<'d>;
85    type SerializeStructVariant = map::SerializeDocumentTable<'d>;
86
87    fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> {
88        Err(Error::unsupported_type(Some("bool")))
89    }
90
91    fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
92        Err(Error::unsupported_type(Some("i8")))
93    }
94
95    fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
96        Err(Error::unsupported_type(Some("i16")))
97    }
98
99    fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
100        Err(Error::unsupported_type(Some("i32")))
101    }
102
103    fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
104        Err(Error::unsupported_type(Some("i64")))
105    }
106
107    fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> {
108        Err(Error::unsupported_type(Some("u8")))
109    }
110
111    fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
112        Err(Error::unsupported_type(Some("u16")))
113    }
114
115    fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
116        Err(Error::unsupported_type(Some("u32")))
117    }
118
119    fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> {
120        Err(Error::unsupported_type(Some("u64")))
121    }
122
123    fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
124        Err(Error::unsupported_type(Some("f32")))
125    }
126
127    fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
128        Err(Error::unsupported_type(Some("f64")))
129    }
130
131    fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
132        Err(Error::unsupported_type(Some("char")))
133    }
134
135    fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
136        Err(Error::unsupported_type(Some("str")))
137    }
138
139    fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
140        Err(Error::unsupported_type(Some("bytes")))
141    }
142
143    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
144        Err(Error::unsupported_none())
145    }
146
147    fn serialize_some<T>(self, v: &T) -> Result<Self::Ok, Self::Error>
148    where
149        T: serde::ser::Serialize + ?Sized,
150    {
151        v.serialize(self)
152    }
153
154    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
155        Err(Error::unsupported_type(Some("unit")))
156    }
157
158    fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
159        Err(Error::unsupported_type(Some(name)))
160    }
161
162    fn serialize_unit_variant(
163        self,
164        name: &'static str,
165        _variant_index: u32,
166        _variant: &'static str,
167    ) -> Result<Self::Ok, Self::Error> {
168        Err(Error::unsupported_type(Some(name)))
169    }
170
171    fn serialize_newtype_struct<T>(
172        self,
173        _name: &'static str,
174        v: &T,
175    ) -> Result<Self::Ok, Self::Error>
176    where
177        T: serde::ser::Serialize + ?Sized,
178    {
179        v.serialize(self)
180    }
181
182    fn serialize_newtype_variant<T>(
183        mut self,
184        _name: &'static str,
185        _variant_index: u32,
186        variant: &'static str,
187        value: &T,
188    ) -> Result<Self::Ok, Self::Error>
189    where
190        T: serde::ser::Serialize + ?Sized,
191    {
192        match SerializationStrategy::from(value) {
193            SerializationStrategy::Value | SerializationStrategy::ArrayOfTables => {
194                let dst = self.table.body_mut();
195
196                dst.key(variant)?;
197                dst.space()?;
198                dst.keyval_sep()?;
199                dst.space()?;
200                let value_serializer = value::ValueSerializer::with_style(dst, self.style);
201                let dst = value.serialize(value_serializer)?;
202                dst.newline()?;
203            }
204            SerializationStrategy::Table | SerializationStrategy::Unknown => {
205                let child = self.buf.child_table(&mut self.table, variant.to_owned());
206                let value_serializer = Serializer::with_table(self.buf, child, self.style);
207                value.serialize(value_serializer)?;
208            }
209            SerializationStrategy::Skip => {
210                // silently drop these key-value pairs
211            }
212        }
213
214        self.end()
215    }
216
217    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
218        Err(Error::unsupported_type(Some("array")))
219    }
220
221    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
222        self.serialize_seq(Some(len))
223    }
224
225    fn serialize_tuple_struct(
226        self,
227        _name: &'static str,
228        len: usize,
229    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
230        self.serialize_seq(Some(len))
231    }
232
233    fn serialize_tuple_variant(
234        self,
235        _name: &'static str,
236        _variant_index: u32,
237        variant: &'static str,
238        len: usize,
239    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
240        array::SerializeDocumentTupleVariant::tuple(self.buf, self.table, variant, len, self.style)
241    }
242
243    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
244        map::SerializeDocumentTable::map(self.buf, self.table, self.style)
245    }
246
247    fn serialize_struct(
248        self,
249        _name: &'static str,
250        len: usize,
251    ) -> Result<Self::SerializeStruct, Self::Error> {
252        self.serialize_map(Some(len))
253    }
254
255    fn serialize_struct_variant(
256        mut self,
257        _name: &'static str,
258        _variant_index: u32,
259        variant: &'static str,
260        _len: usize,
261    ) -> Result<Self::SerializeStructVariant, Self::Error> {
262        let child = self.buf.child_table(&mut self.table, variant.to_owned());
263        self.buf.push(self.table);
264        map::SerializeDocumentTable::map(self.buf, child, self.style)
265    }
266}