toml_edit/ser/
value.rs

1use super::Error;
2use super::SerializeMap;
3use super::SerializeStructVariant;
4use super::SerializeTupleVariant;
5use super::SerializeValueArray;
6
7/// Serialization for TOML [values][crate::Value].
8///
9/// This structure implements serialization support for TOML to serialize an
10/// arbitrary type to TOML. Note that the TOML format does not support all
11/// datatypes in Rust, such as enums, tuples, and tuple structs. These types
12/// will generate an error when serialized.
13///
14/// Currently a serializer always writes its output to an in-memory `String`,
15/// which is passed in when creating the serializer itself.
16///
17/// # Examples
18///
19/// ```
20/// # #[cfg(feature = "parse")] {
21/// # #[cfg(feature = "display")] {
22/// use serde::Serialize;
23///
24/// #[derive(Serialize)]
25/// struct Config {
26///     database: Database,
27/// }
28///
29/// #[derive(Serialize)]
30/// struct Database {
31///     ip: String,
32///     port: Vec<u16>,
33///     connection_max: u32,
34///     enabled: bool,
35/// }
36///
37/// let config = Config {
38///     database: Database {
39///         ip: "192.168.1.1".to_string(),
40///         port: vec![8001, 8002, 8003],
41///         connection_max: 5000,
42///         enabled: false,
43///     },
44/// };
45///
46/// let value = serde::Serialize::serialize(
47///     &config,
48///     toml_edit::ser::ValueSerializer::new()
49/// ).unwrap();
50/// println!("{}", value)
51/// # }
52/// # }
53/// ```
54#[derive(Default)]
55#[non_exhaustive]
56pub struct ValueSerializer {}
57
58impl ValueSerializer {
59    /// Creates a new serializer generate a TOML document.
60    pub fn new() -> Self {
61        Self {}
62    }
63}
64
65impl serde::ser::Serializer for ValueSerializer {
66    type Ok = crate::Value;
67    type Error = Error;
68    type SerializeSeq = SerializeValueArray;
69    type SerializeTuple = SerializeValueArray;
70    type SerializeTupleStruct = SerializeValueArray;
71    type SerializeTupleVariant = SerializeTupleVariant;
72    type SerializeMap = SerializeMap;
73    type SerializeStruct = SerializeMap;
74    type SerializeStructVariant = SerializeStructVariant;
75
76    fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
77        Ok(v.into())
78    }
79
80    fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
81        self.serialize_i64(v as i64)
82    }
83
84    fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
85        self.serialize_i64(v as i64)
86    }
87
88    fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
89        self.serialize_i64(v as i64)
90    }
91
92    fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
93        Ok(v.into())
94    }
95
96    fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
97        self.serialize_i64(v as i64)
98    }
99
100    fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
101        self.serialize_i64(v as i64)
102    }
103
104    fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
105        self.serialize_i64(v as i64)
106    }
107
108    fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
109        let v: i64 = v
110            .try_into()
111            .map_err(|_err| Error::out_of_range(Some("u64")))?;
112        self.serialize_i64(v)
113    }
114
115    fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
116        self.serialize_f64(v as f64)
117    }
118
119    fn serialize_f64(self, mut v: f64) -> Result<Self::Ok, Self::Error> {
120        // Discard sign of NaN when serialized using Serde.
121        //
122        // In all likelihood the sign of NaNs is not meaningful in the user's
123        // program. Ending up with `-nan` in the TOML document would usually be
124        // surprising and undesirable, when the sign of the NaN was not
125        // intentionally controlled by the caller, or may even be
126        // nondeterministic if it comes from arithmetic operations or a cast.
127        if v.is_nan() {
128            v = v.copysign(1.0);
129        }
130        Ok(v.into())
131    }
132
133    fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
134        let mut buf = [0; 4];
135        self.serialize_str(v.encode_utf8(&mut buf))
136    }
137
138    fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
139        Ok(v.into())
140    }
141
142    fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> {
143        use serde::ser::Serialize;
144        value.serialize(self)
145    }
146
147    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
148        Err(Error::unsupported_none())
149    }
150
151    fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
152    where
153        T: serde::ser::Serialize + ?Sized,
154    {
155        value.serialize(self)
156    }
157
158    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
159        Err(Error::unsupported_type(Some("unit")))
160    }
161
162    fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
163        Err(Error::unsupported_type(Some(name)))
164    }
165
166    fn serialize_unit_variant(
167        self,
168        _name: &'static str,
169        _variant_index: u32,
170        variant: &'static str,
171    ) -> Result<Self::Ok, Self::Error> {
172        self.serialize_str(variant)
173    }
174
175    fn serialize_newtype_struct<T>(
176        self,
177        _name: &'static str,
178        value: &T,
179    ) -> Result<Self::Ok, Self::Error>
180    where
181        T: serde::ser::Serialize + ?Sized,
182    {
183        value.serialize(self)
184    }
185
186    fn serialize_newtype_variant<T>(
187        self,
188        _name: &'static str,
189        _variant_index: u32,
190        variant: &'static str,
191        value: &T,
192    ) -> Result<Self::Ok, Self::Error>
193    where
194        T: serde::ser::Serialize + ?Sized,
195    {
196        let value = value.serialize(self)?;
197        let mut table = crate::InlineTable::new();
198        table.insert(variant, value);
199        Ok(table.into())
200    }
201
202    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
203        Ok(SerializeValueArray::seq(len))
204    }
205
206    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
207        self.serialize_seq(Some(len))
208    }
209
210    fn serialize_tuple_struct(
211        self,
212        _name: &'static str,
213        len: usize,
214    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
215        self.serialize_seq(Some(len))
216    }
217
218    fn serialize_tuple_variant(
219        self,
220        _name: &'static str,
221        _variant_index: u32,
222        variant: &'static str,
223        len: usize,
224    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
225        Ok(SerializeTupleVariant::tuple(variant, len))
226    }
227
228    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
229        Ok(SerializeMap::map(len))
230    }
231
232    fn serialize_struct(
233        self,
234        name: &'static str,
235        len: usize,
236    ) -> Result<Self::SerializeStruct, Self::Error> {
237        Ok(SerializeMap::struct_(name, Some(len)))
238    }
239
240    fn serialize_struct_variant(
241        self,
242        _name: &'static str,
243        _variant_index: u32,
244        variant: &'static str,
245        len: usize,
246    ) -> Result<Self::SerializeStructVariant, Self::Error> {
247        Ok(SerializeStructVariant::struct_(variant, len))
248    }
249}