1use super::{Error, KeySerializer, SerializeValueArray, ValueSerializer};
2
3#[doc(hidden)]
4pub enum SerializeMap {
5 Datetime(SerializeDatetime),
6 Table(SerializeInlineTable),
7}
8
9impl SerializeMap {
10 pub(crate) fn table() -> Self {
11 Self::Table(SerializeInlineTable::new())
12 }
13
14 pub(crate) fn table_with_capacity(len: usize) -> Self {
15 Self::Table(SerializeInlineTable::with_capacity(len))
16 }
17
18 pub(crate) fn datetime() -> Self {
19 Self::Datetime(SerializeDatetime::new())
20 }
21}
22
23impl serde::ser::SerializeMap for SerializeMap {
24 type Ok = crate::Value;
25 type Error = Error;
26
27 fn serialize_key<T: ?Sized>(&mut self, input: &T) -> Result<(), Self::Error>
28 where
29 T: serde::ser::Serialize,
30 {
31 match self {
32 Self::Datetime(s) => s.serialize_key(input),
33 Self::Table(s) => s.serialize_key(input),
34 }
35 }
36
37 fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
38 where
39 T: serde::ser::Serialize,
40 {
41 match self {
42 Self::Datetime(s) => s.serialize_value(value),
43 Self::Table(s) => s.serialize_value(value),
44 }
45 }
46
47 fn end(self) -> Result<Self::Ok, Self::Error> {
48 match self {
49 Self::Datetime(s) => s.end().map(|items| items.into()),
50 Self::Table(s) => s.end().map(|items| items.into()),
51 }
52 }
53}
54
55impl serde::ser::SerializeStruct for SerializeMap {
56 type Ok = crate::Value;
57 type Error = Error;
58
59 fn serialize_field<T: ?Sized>(
60 &mut self,
61 key: &'static str,
62 value: &T,
63 ) -> Result<(), Self::Error>
64 where
65 T: serde::ser::Serialize,
66 {
67 match self {
68 Self::Datetime(s) => s.serialize_field(key, value),
69 Self::Table(s) => s.serialize_field(key, value),
70 }
71 }
72
73 fn end(self) -> Result<Self::Ok, Self::Error> {
74 match self {
75 Self::Datetime(s) => s.end().map(|items| items.into()),
76 Self::Table(s) => s.end().map(|items| items.into()),
77 }
78 }
79}
80
81#[doc(hidden)]
82pub struct SerializeDatetime {
83 value: Option<crate::Datetime>,
84}
85
86impl SerializeDatetime {
87 pub(crate) fn new() -> Self {
88 Self { value: None }
89 }
90}
91
92impl serde::ser::SerializeMap for SerializeDatetime {
93 type Ok = crate::Datetime;
94 type Error = Error;
95
96 fn serialize_key<T: ?Sized>(&mut self, _input: &T) -> Result<(), Self::Error>
97 where
98 T: serde::ser::Serialize,
99 {
100 unreachable!("datetimes should only be serialized as structs, not maps")
101 }
102
103 fn serialize_value<T: ?Sized>(&mut self, _value: &T) -> Result<(), Self::Error>
104 where
105 T: serde::ser::Serialize,
106 {
107 unreachable!("datetimes should only be serialized as structs, not maps")
108 }
109
110 fn end(self) -> Result<Self::Ok, Self::Error> {
111 unreachable!("datetimes should only be serialized as structs, not maps")
112 }
113}
114
115impl serde::ser::SerializeStruct for SerializeDatetime {
116 type Ok = crate::Datetime;
117 type Error = Error;
118
119 fn serialize_field<T: ?Sized>(
120 &mut self,
121 key: &'static str,
122 value: &T,
123 ) -> Result<(), Self::Error>
124 where
125 T: serde::ser::Serialize,
126 {
127 if key == toml_datetime::__unstable::FIELD {
128 self.value = Some(value.serialize(serializer:DatetimeFieldSerializer::default())?);
129 }
130
131 Ok(())
132 }
133
134 fn end(self) -> Result<Self::Ok, Self::Error> {
135 self.value.ok_or(err:Error::UnsupportedNone)
136 }
137}
138
139#[doc(hidden)]
140pub struct SerializeInlineTable {
141 items: crate::table::KeyValuePairs,
142 key: Option<crate::InternalString>,
143}
144
145impl SerializeInlineTable {
146 pub(crate) fn new() -> Self {
147 Self {
148 items: Default::default(),
149 key: Default::default(),
150 }
151 }
152
153 pub(crate) fn with_capacity(len: usize) -> Self {
154 let mut s: SerializeInlineTable = Self::new();
155 s.items.reserve(additional:len);
156 s
157 }
158}
159
160impl serde::ser::SerializeMap for SerializeInlineTable {
161 type Ok = crate::InlineTable;
162 type Error = Error;
163
164 fn serialize_key<T: ?Sized>(&mut self, input: &T) -> Result<(), Self::Error>
165 where
166 T: serde::ser::Serialize,
167 {
168 self.key = Some(input.serialize(KeySerializer)?);
169 Ok(())
170 }
171
172 fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
173 where
174 T: serde::ser::Serialize,
175 {
176 let mut value_serializer = MapValueSerializer::new();
177 let res = value.serialize(&mut value_serializer);
178 match res {
179 Ok(item) => {
180 let key = self.key.take().unwrap();
181 let kv = crate::table::TableKeyValue::new(
182 crate::Key::new(&key),
183 crate::Item::Value(item),
184 );
185 self.items.insert(key, kv);
186 }
187 Err(e) => {
188 if !(e == Error::UnsupportedNone && value_serializer.is_none) {
189 return Err(e);
190 }
191 }
192 }
193 Ok(())
194 }
195
196 fn end(self) -> Result<Self::Ok, Self::Error> {
197 Ok(crate::InlineTable::with_pairs(self.items))
198 }
199}
200
201impl serde::ser::SerializeStruct for SerializeInlineTable {
202 type Ok = crate::InlineTable;
203 type Error = Error;
204
205 fn serialize_field<T: ?Sized>(
206 &mut self,
207 key: &'static str,
208 value: &T,
209 ) -> Result<(), Self::Error>
210 where
211 T: serde::ser::Serialize,
212 {
213 let mut value_serializer = MapValueSerializer::new();
214 let res = value.serialize(&mut value_serializer);
215 match res {
216 Ok(item) => {
217 let kv = crate::table::TableKeyValue::new(
218 crate::Key::new(key),
219 crate::Item::Value(item),
220 );
221 self.items.insert(crate::InternalString::from(key), kv);
222 }
223 Err(e) => {
224 if !(e == Error::UnsupportedNone && value_serializer.is_none) {
225 return Err(e);
226 }
227 }
228 };
229 Ok(())
230 }
231
232 fn end(self) -> Result<Self::Ok, Self::Error> {
233 Ok(crate::InlineTable::with_pairs(self.items))
234 }
235}
236
237#[derive(Default)]
238struct DatetimeFieldSerializer {}
239
240impl serde::ser::Serializer for DatetimeFieldSerializer {
241 type Ok = toml_datetime::Datetime;
242 type Error = Error;
243 type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>;
244 type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>;
245 type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
246 type SerializeTupleVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
247 type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>;
248 type SerializeStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
249 type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
250
251 fn serialize_bool(self, _value: bool) -> Result<Self::Ok, Self::Error> {
252 Err(Error::DateInvalid)
253 }
254
255 fn serialize_i8(self, _value: i8) -> Result<Self::Ok, Self::Error> {
256 Err(Error::DateInvalid)
257 }
258
259 fn serialize_i16(self, _value: i16) -> Result<Self::Ok, Self::Error> {
260 Err(Error::DateInvalid)
261 }
262
263 fn serialize_i32(self, _value: i32) -> Result<Self::Ok, Self::Error> {
264 Err(Error::DateInvalid)
265 }
266
267 fn serialize_i64(self, _value: i64) -> Result<Self::Ok, Self::Error> {
268 Err(Error::DateInvalid)
269 }
270
271 fn serialize_u8(self, _value: u8) -> Result<Self::Ok, Self::Error> {
272 Err(Error::DateInvalid)
273 }
274
275 fn serialize_u16(self, _value: u16) -> Result<Self::Ok, Self::Error> {
276 Err(Error::DateInvalid)
277 }
278
279 fn serialize_u32(self, _value: u32) -> Result<Self::Ok, Self::Error> {
280 Err(Error::DateInvalid)
281 }
282
283 fn serialize_u64(self, _value: u64) -> Result<Self::Ok, Self::Error> {
284 Err(Error::DateInvalid)
285 }
286
287 fn serialize_f32(self, _value: f32) -> Result<Self::Ok, Self::Error> {
288 Err(Error::DateInvalid)
289 }
290
291 fn serialize_f64(self, _value: f64) -> Result<Self::Ok, Self::Error> {
292 Err(Error::DateInvalid)
293 }
294
295 fn serialize_char(self, _value: char) -> Result<Self::Ok, Self::Error> {
296 Err(Error::DateInvalid)
297 }
298
299 fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
300 v.parse::<toml_datetime::Datetime>().map_err(Error::custom)
301 }
302
303 fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok, Self::Error> {
304 Err(Error::DateInvalid)
305 }
306
307 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
308 Err(Error::DateInvalid)
309 }
310
311 fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
312 where
313 T: serde::ser::Serialize,
314 {
315 Err(Error::DateInvalid)
316 }
317
318 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
319 Err(Error::DateInvalid)
320 }
321
322 fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
323 Err(Error::DateInvalid)
324 }
325
326 fn serialize_unit_variant(
327 self,
328 _name: &'static str,
329 _variant_index: u32,
330 _variant: &'static str,
331 ) -> Result<Self::Ok, Self::Error> {
332 Err(Error::DateInvalid)
333 }
334
335 fn serialize_newtype_struct<T: ?Sized>(
336 self,
337 _name: &'static str,
338 _value: &T,
339 ) -> Result<Self::Ok, Self::Error>
340 where
341 T: serde::ser::Serialize,
342 {
343 Err(Error::DateInvalid)
344 }
345
346 fn serialize_newtype_variant<T: ?Sized>(
347 self,
348 _name: &'static str,
349 _variant_index: u32,
350 _variant: &'static str,
351 _value: &T,
352 ) -> Result<Self::Ok, Self::Error>
353 where
354 T: serde::ser::Serialize,
355 {
356 Err(Error::DateInvalid)
357 }
358
359 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
360 Err(Error::DateInvalid)
361 }
362
363 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
364 Err(Error::DateInvalid)
365 }
366
367 fn serialize_tuple_struct(
368 self,
369 _name: &'static str,
370 _len: usize,
371 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
372 Err(Error::DateInvalid)
373 }
374
375 fn serialize_tuple_variant(
376 self,
377 _name: &'static str,
378 _variant_index: u32,
379 _variant: &'static str,
380 _len: usize,
381 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
382 Err(Error::DateInvalid)
383 }
384
385 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
386 Err(Error::DateInvalid)
387 }
388
389 fn serialize_struct(
390 self,
391 _name: &'static str,
392 _len: usize,
393 ) -> Result<Self::SerializeStruct, Self::Error> {
394 Err(Error::DateInvalid)
395 }
396
397 fn serialize_struct_variant(
398 self,
399 _name: &'static str,
400 _variant_index: u32,
401 _variant: &'static str,
402 _len: usize,
403 ) -> Result<Self::SerializeStructVariant, Self::Error> {
404 Err(Error::DateInvalid)
405 }
406}
407
408#[derive(Default)]
409struct MapValueSerializer {
410 is_none: bool,
411}
412
413impl MapValueSerializer {
414 fn new() -> Self {
415 Self { is_none: false }
416 }
417}
418
419impl serde::ser::Serializer for &mut MapValueSerializer {
420 type Ok = crate::Value;
421 type Error = Error;
422 type SerializeSeq = super::SerializeValueArray;
423 type SerializeTuple = super::SerializeValueArray;
424 type SerializeTupleStruct = super::SerializeValueArray;
425 type SerializeTupleVariant = super::SerializeTupleVariant;
426 type SerializeMap = super::SerializeMap;
427 type SerializeStruct = super::SerializeMap;
428 type SerializeStructVariant = super::SerializeStructVariant;
429
430 fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
431 ValueSerializer::new().serialize_bool(v)
432 }
433
434 fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
435 ValueSerializer::new().serialize_i8(v)
436 }
437
438 fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
439 ValueSerializer::new().serialize_i16(v)
440 }
441
442 fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
443 ValueSerializer::new().serialize_i32(v)
444 }
445
446 fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
447 ValueSerializer::new().serialize_i64(v)
448 }
449
450 fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
451 ValueSerializer::new().serialize_u8(v)
452 }
453
454 fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
455 ValueSerializer::new().serialize_u16(v)
456 }
457
458 fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
459 ValueSerializer::new().serialize_u32(v)
460 }
461
462 fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
463 ValueSerializer::new().serialize_u64(v)
464 }
465
466 fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
467 ValueSerializer::new().serialize_f32(v)
468 }
469
470 fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
471 ValueSerializer::new().serialize_f64(v)
472 }
473
474 fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
475 ValueSerializer::new().serialize_char(v)
476 }
477
478 fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
479 ValueSerializer::new().serialize_str(v)
480 }
481
482 fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> {
483 ValueSerializer::new().serialize_bytes(value)
484 }
485
486 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
487 self.is_none = true;
488 Err(Error::UnsupportedNone)
489 }
490
491 fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
492 where
493 T: serde::ser::Serialize,
494 {
495 ValueSerializer::new().serialize_some(value)
496 }
497
498 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
499 ValueSerializer::new().serialize_unit()
500 }
501
502 fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
503 ValueSerializer::new().serialize_unit_struct(name)
504 }
505
506 fn serialize_unit_variant(
507 self,
508 name: &'static str,
509 variant_index: u32,
510 variant: &'static str,
511 ) -> Result<Self::Ok, Self::Error> {
512 ValueSerializer::new().serialize_unit_variant(name, variant_index, variant)
513 }
514
515 fn serialize_newtype_struct<T: ?Sized>(
516 self,
517 name: &'static str,
518 value: &T,
519 ) -> Result<Self::Ok, Self::Error>
520 where
521 T: serde::ser::Serialize,
522 {
523 ValueSerializer::new().serialize_newtype_struct(name, value)
524 }
525
526 fn serialize_newtype_variant<T: ?Sized>(
527 self,
528 name: &'static str,
529 variant_index: u32,
530 variant: &'static str,
531 value: &T,
532 ) -> Result<Self::Ok, Self::Error>
533 where
534 T: serde::ser::Serialize,
535 {
536 ValueSerializer::new().serialize_newtype_variant(name, variant_index, variant, value)
537 }
538
539 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
540 ValueSerializer::new().serialize_seq(len)
541 }
542
543 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
544 ValueSerializer::new().serialize_tuple(len)
545 }
546
547 fn serialize_tuple_struct(
548 self,
549 name: &'static str,
550 len: usize,
551 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
552 ValueSerializer::new().serialize_tuple_struct(name, len)
553 }
554
555 fn serialize_tuple_variant(
556 self,
557 name: &'static str,
558 variant_index: u32,
559 variant: &'static str,
560 len: usize,
561 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
562 ValueSerializer::new().serialize_tuple_variant(name, variant_index, variant, len)
563 }
564
565 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
566 ValueSerializer::new().serialize_map(len)
567 }
568
569 fn serialize_struct(
570 self,
571 name: &'static str,
572 len: usize,
573 ) -> Result<Self::SerializeStruct, Self::Error> {
574 ValueSerializer::new().serialize_struct(name, len)
575 }
576
577 fn serialize_struct_variant(
578 self,
579 name: &'static str,
580 variant_index: u32,
581 variant: &'static str,
582 len: usize,
583 ) -> Result<Self::SerializeStructVariant, Self::Error> {
584 ValueSerializer::new().serialize_struct_variant(name, variant_index, variant, len)
585 }
586}
587
588pub type SerializeTupleVariant = SerializeVariant<SerializeValueArray>;
589pub type SerializeStructVariant = SerializeVariant<SerializeMap>;
590
591pub struct SerializeVariant<T> {
592 variant: &'static str,
593 inner: T,
594}
595
596impl SerializeVariant<SerializeValueArray> {
597 pub(crate) fn tuple(variant: &'static str, len: usize) -> Self {
598 Self {
599 variant,
600 inner: SerializeValueArray::with_capacity(len),
601 }
602 }
603}
604
605impl SerializeVariant<SerializeMap> {
606 pub(crate) fn struct_(variant: &'static str, len: usize) -> Self {
607 Self {
608 variant,
609 inner: SerializeMap::table_with_capacity(len),
610 }
611 }
612}
613
614impl serde::ser::SerializeTupleVariant for SerializeVariant<SerializeValueArray> {
615 type Ok = crate::Value;
616 type Error = Error;
617
618 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
619 where
620 T: serde::ser::Serialize,
621 {
622 serde::ser::SerializeSeq::serialize_element(&mut self.inner, value)
623 }
624
625 fn end(self) -> Result<Self::Ok, Self::Error> {
626 let inner: Value = serde::ser::SerializeSeq::end(self.inner)?;
627 let mut items: IndexMap = crate::table::KeyValuePairs::new();
628 let kv: TableKeyValue = crate::table::TableKeyValue::new(
629 crate::Key::new(self.variant),
630 crate::Item::Value(inner),
631 );
632 items.insert(key:crate::InternalString::from(self.variant), value:kv);
633 Ok(crate::Value::InlineTable(crate::InlineTable::with_pairs(
634 items,
635 )))
636 }
637}
638
639impl serde::ser::SerializeStructVariant for SerializeVariant<SerializeMap> {
640 type Ok = crate::Value;
641 type Error = Error;
642
643 #[inline]
644 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
645 where
646 T: serde::ser::Serialize + ?Sized,
647 {
648 serde::ser::SerializeStruct::serialize_field(&mut self.inner, key, value)
649 }
650
651 #[inline]
652 fn end(self) -> Result<Self::Ok, Self::Error> {
653 let inner = serde::ser::SerializeStruct::end(self.inner)?;
654 let mut items = crate::table::KeyValuePairs::new();
655 let kv = crate::table::TableKeyValue::new(
656 crate::Key::new(self.variant),
657 crate::Item::Value(inner),
658 );
659 items.insert(crate::InternalString::from(self.variant), kv);
660 Ok(crate::Value::InlineTable(crate::InlineTable::with_pairs(
661 items,
662 )))
663 }
664}
665