1#[derive(Copy, Clone, PartialEq, Eq, Debug)]
2pub(crate) enum SerializationStrategy {
3 Value,
4 Table,
5 ArrayOfTables,
6 Skip,
7 Unknown,
8}
9
10impl<T> From<&T> for SerializationStrategy
11where
12 T: serde_core::ser::Serialize + ?Sized,
13{
14 fn from(value: &T) -> Self {
15 value.serialize(WalkValue).unwrap_err()
16 }
17}
18
19impl serde_core::ser::Error for SerializationStrategy {
20 fn custom<T>(_msg: T) -> Self
21 where
22 T: core::fmt::Display,
23 {
24 Self::Unknown
25 }
26}
27
28impl core::fmt::Display for SerializationStrategy {
29 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30 "error".fmt(f)
31 }
32}
33
34impl core::error::Error for SerializationStrategy {}
35
36struct WalkValue;
37
38impl serde_core::ser::Serializer for WalkValue {
39 type Ok = core::convert::Infallible;
40 type Error = SerializationStrategy;
41 type SerializeSeq = ArrayWalkValue;
42 type SerializeTuple = ArrayWalkValue;
43 type SerializeTupleStruct = ArrayWalkValue;
44 type SerializeTupleVariant = serde_core::ser::Impossible<Self::Ok, Self::Error>;
45 type SerializeMap = serde_core::ser::Impossible<Self::Ok, Self::Error>;
46 type SerializeStruct = StructWalkValue;
47 type SerializeStructVariant = serde_core::ser::Impossible<Self::Ok, Self::Error>;
48
49 fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> {
50 Err(SerializationStrategy::Value)
51 }
52
53 fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
54 Err(SerializationStrategy::Value)
55 }
56
57 fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
58 Err(SerializationStrategy::Value)
59 }
60
61 fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
62 Err(SerializationStrategy::Value)
63 }
64
65 fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
66 Err(SerializationStrategy::Value)
67 }
68
69 fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> {
70 Err(SerializationStrategy::Value)
71 }
72
73 fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> {
74 Err(SerializationStrategy::Value)
75 }
76
77 fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
78 Err(SerializationStrategy::Value)
79 }
80
81 fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
82 Err(SerializationStrategy::Value)
83 }
84
85 fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> {
86 Err(SerializationStrategy::Value)
87 }
88
89 fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> {
90 Err(SerializationStrategy::Value)
91 }
92
93 fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
94 Err(SerializationStrategy::Value)
95 }
96
97 fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
98 Err(SerializationStrategy::Value)
99 }
100
101 fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
102 Err(SerializationStrategy::Value)
103 }
104
105 fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
106 Err(SerializationStrategy::Value)
107 }
108
109 fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
110 Err(SerializationStrategy::Value)
111 }
112
113 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
114 Err(SerializationStrategy::Skip)
115 }
116
117 fn serialize_some<T>(self, v: &T) -> Result<Self::Ok, Self::Error>
118 where
119 T: serde_core::ser::Serialize + ?Sized,
120 {
121 v.serialize(self)
122 }
123
124 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
125 Err(SerializationStrategy::Value)
126 }
127
128 fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
129 Err(SerializationStrategy::Value)
130 }
131
132 fn serialize_unit_variant(
133 self,
134 _name: &'static str,
135 _variant_index: u32,
136 _variant: &'static str,
137 ) -> Result<Self::Ok, Self::Error> {
138 Err(SerializationStrategy::Value)
139 }
140
141 fn serialize_newtype_struct<T>(
142 self,
143 _name: &'static str,
144 v: &T,
145 ) -> Result<Self::Ok, Self::Error>
146 where
147 T: serde_core::ser::Serialize + ?Sized,
148 {
149 v.serialize(self)
150 }
151
152 fn serialize_newtype_variant<T>(
153 self,
154 _name: &'static str,
155 _variant_index: u32,
156 _variant: &'static str,
157 _value: &T,
158 ) -> Result<Self::Ok, Self::Error>
159 where
160 T: serde_core::ser::Serialize + ?Sized,
161 {
162 Err(SerializationStrategy::Table)
163 }
164
165 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
166 Ok(ArrayWalkValue::new())
167 }
168
169 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
170 self.serialize_seq(Some(len))
171 }
172
173 fn serialize_tuple_struct(
174 self,
175 _name: &'static str,
176 len: usize,
177 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
178 self.serialize_seq(Some(len))
179 }
180
181 fn serialize_tuple_variant(
182 self,
183 _name: &'static str,
184 _variant_index: u32,
185 _variant: &'static str,
186 _len: usize,
187 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
188 Err(SerializationStrategy::Table)
189 }
190
191 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
192 Err(SerializationStrategy::Table)
193 }
194
195 fn serialize_struct(
196 self,
197 name: &'static str,
198 _len: usize,
199 ) -> Result<Self::SerializeStruct, Self::Error> {
200 if toml_datetime::ser::is_datetime(name) {
201 Ok(StructWalkValue)
202 } else {
203 Err(SerializationStrategy::Table)
204 }
205 }
206
207 fn serialize_struct_variant(
208 self,
209 _name: &'static str,
210 _variant_index: u32,
211 _variant: &'static str,
212 _len: usize,
213 ) -> Result<Self::SerializeStructVariant, Self::Error> {
214 Err(SerializationStrategy::Table)
215 }
216}
217
218#[doc(hidden)]
219pub(crate) struct ArrayWalkValue {
220 is_empty: bool,
221}
222
223impl ArrayWalkValue {
224 fn new() -> Self {
225 Self { is_empty: true }
226 }
227
228 fn serialize_element<T>(&mut self, value: &T) -> Result<(), SerializationStrategy>
229 where
230 T: serde_core::ser::Serialize + ?Sized,
231 {
232 self.is_empty = false;
233 match SerializationStrategy::from(value) {
234 SerializationStrategy::Value
235 | SerializationStrategy::ArrayOfTables
236 | SerializationStrategy::Unknown
237 | SerializationStrategy::Skip => Err(SerializationStrategy::Value),
238 SerializationStrategy::Table => Ok(()),
239 }
240 }
241
242 fn end(self) -> Result<core::convert::Infallible, SerializationStrategy> {
243 if self.is_empty {
244 Err(SerializationStrategy::Value)
245 } else {
246 Err(SerializationStrategy::ArrayOfTables)
247 }
248 }
249}
250
251impl serde_core::ser::SerializeSeq for ArrayWalkValue {
252 type Ok = core::convert::Infallible;
253 type Error = SerializationStrategy;
254
255 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
256 where
257 T: serde_core::ser::Serialize + ?Sized,
258 {
259 self.serialize_element(value)
260 }
261
262 fn end(self) -> Result<Self::Ok, Self::Error> {
263 self.end()
264 }
265}
266
267impl serde_core::ser::SerializeTuple for ArrayWalkValue {
268 type Ok = core::convert::Infallible;
269 type Error = SerializationStrategy;
270
271 fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
272 where
273 T: serde_core::ser::Serialize + ?Sized,
274 {
275 self.serialize_element(value)
276 }
277
278 fn end(self) -> Result<Self::Ok, Self::Error> {
279 self.end()
280 }
281}
282
283impl serde_core::ser::SerializeTupleStruct for ArrayWalkValue {
284 type Ok = core::convert::Infallible;
285 type Error = SerializationStrategy;
286
287 fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
288 where
289 T: serde_core::ser::Serialize + ?Sized,
290 {
291 self.serialize_element(value)
292 }
293
294 fn end(self) -> Result<Self::Ok, Self::Error> {
295 self.end()
296 }
297}
298
299pub(crate) struct StructWalkValue;
300
301impl serde_core::ser::SerializeMap for StructWalkValue {
302 type Ok = core::convert::Infallible;
303 type Error = SerializationStrategy;
304
305 fn serialize_key<T>(&mut self, _input: &T) -> Result<(), Self::Error>
306 where
307 T: serde_core::ser::Serialize + ?Sized,
308 {
309 Ok(())
310 }
311
312 fn serialize_value<T>(&mut self, _value: &T) -> Result<(), Self::Error>
313 where
314 T: serde_core::ser::Serialize + ?Sized,
315 {
316 Ok(())
317 }
318
319 fn end(self) -> Result<Self::Ok, Self::Error> {
320 Err(SerializationStrategy::Value)
322 }
323}
324
325impl serde_core::ser::SerializeStruct for StructWalkValue {
326 type Ok = core::convert::Infallible;
327 type Error = SerializationStrategy;
328
329 fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<(), Self::Error>
330 where
331 T: serde_core::ser::Serialize + ?Sized,
332 {
333 Ok(())
334 }
335
336 fn end(self) -> Result<Self::Ok, Self::Error> {
337 Err(SerializationStrategy::Value)
339 }
340}