1 | use super::{Error, KeySerializer, ValueSerializer}; |
2 | |
3 | #[doc (hidden)] |
4 | pub enum SerializeMap { |
5 | Datetime(SerializeDatetime), |
6 | Table(SerializeInlineTable), |
7 | } |
8 | |
9 | impl 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 | |
23 | impl 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 | |
55 | impl 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)] |
82 | pub struct SerializeDatetime { |
83 | value: Option<crate::Datetime>, |
84 | } |
85 | |
86 | impl SerializeDatetime { |
87 | pub(crate) fn new() -> Self { |
88 | Self { value: None } |
89 | } |
90 | } |
91 | |
92 | impl 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 | |
115 | impl 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)] |
140 | pub struct SerializeInlineTable { |
141 | items: crate::table::KeyValuePairs, |
142 | key: Option<crate::InternalString>, |
143 | } |
144 | |
145 | impl 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 | |
160 | impl 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 = None; |
169 | self.key = Some(input.serialize(KeySerializer)?); |
170 | Ok(()) |
171 | } |
172 | |
173 | fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> |
174 | where |
175 | T: serde::ser::Serialize, |
176 | { |
177 | let mut value_serializer = MapValueSerializer::new(); |
178 | let res = value.serialize(&mut value_serializer); |
179 | match res { |
180 | Ok(item) => { |
181 | let key = self.key.take().unwrap(); |
182 | let kv = crate::table::TableKeyValue::new( |
183 | crate::Key::new(&key), |
184 | crate::Item::Value(item), |
185 | ); |
186 | self.items.insert(key, kv); |
187 | } |
188 | Err(e) => { |
189 | if !(e == Error::UnsupportedNone && value_serializer.is_none) { |
190 | return Err(e); |
191 | } |
192 | } |
193 | } |
194 | Ok(()) |
195 | } |
196 | |
197 | fn end(self) -> Result<Self::Ok, Self::Error> { |
198 | Ok(crate::InlineTable::with_pairs(self.items)) |
199 | } |
200 | } |
201 | |
202 | impl serde::ser::SerializeStruct for SerializeInlineTable { |
203 | type Ok = crate::InlineTable; |
204 | type Error = Error; |
205 | |
206 | fn serialize_field<T: ?Sized>( |
207 | &mut self, |
208 | key: &'static str, |
209 | value: &T, |
210 | ) -> Result<(), Self::Error> |
211 | where |
212 | T: serde::ser::Serialize, |
213 | { |
214 | let mut value_serializer = MapValueSerializer::new(); |
215 | let res = value.serialize(&mut value_serializer); |
216 | match res { |
217 | Ok(item) => { |
218 | let kv = crate::table::TableKeyValue::new( |
219 | crate::Key::new(key), |
220 | crate::Item::Value(item), |
221 | ); |
222 | self.items.insert(crate::InternalString::from(key), kv); |
223 | } |
224 | Err(e) => { |
225 | if !(e == Error::UnsupportedNone && value_serializer.is_none) { |
226 | return Err(e); |
227 | } |
228 | } |
229 | }; |
230 | Ok(()) |
231 | } |
232 | |
233 | fn end(self) -> Result<Self::Ok, Self::Error> { |
234 | Ok(crate::InlineTable::with_pairs(self.items)) |
235 | } |
236 | } |
237 | |
238 | #[derive (Default)] |
239 | struct DatetimeFieldSerializer {} |
240 | |
241 | impl serde::ser::Serializer for DatetimeFieldSerializer { |
242 | type Ok = toml_datetime::Datetime; |
243 | type Error = Error; |
244 | type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>; |
245 | type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>; |
246 | type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>; |
247 | type SerializeTupleVariant = serde::ser::Impossible<Self::Ok, Self::Error>; |
248 | type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>; |
249 | type SerializeStruct = serde::ser::Impossible<Self::Ok, Self::Error>; |
250 | type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>; |
251 | |
252 | fn serialize_bool(self, _value: bool) -> Result<Self::Ok, Self::Error> { |
253 | Err(Error::DateInvalid) |
254 | } |
255 | |
256 | fn serialize_i8(self, _value: i8) -> Result<Self::Ok, Self::Error> { |
257 | Err(Error::DateInvalid) |
258 | } |
259 | |
260 | fn serialize_i16(self, _value: i16) -> Result<Self::Ok, Self::Error> { |
261 | Err(Error::DateInvalid) |
262 | } |
263 | |
264 | fn serialize_i32(self, _value: i32) -> Result<Self::Ok, Self::Error> { |
265 | Err(Error::DateInvalid) |
266 | } |
267 | |
268 | fn serialize_i64(self, _value: i64) -> Result<Self::Ok, Self::Error> { |
269 | Err(Error::DateInvalid) |
270 | } |
271 | |
272 | fn serialize_u8(self, _value: u8) -> Result<Self::Ok, Self::Error> { |
273 | Err(Error::DateInvalid) |
274 | } |
275 | |
276 | fn serialize_u16(self, _value: u16) -> Result<Self::Ok, Self::Error> { |
277 | Err(Error::DateInvalid) |
278 | } |
279 | |
280 | fn serialize_u32(self, _value: u32) -> Result<Self::Ok, Self::Error> { |
281 | Err(Error::DateInvalid) |
282 | } |
283 | |
284 | fn serialize_u64(self, _value: u64) -> Result<Self::Ok, Self::Error> { |
285 | Err(Error::DateInvalid) |
286 | } |
287 | |
288 | fn serialize_f32(self, _value: f32) -> Result<Self::Ok, Self::Error> { |
289 | Err(Error::DateInvalid) |
290 | } |
291 | |
292 | fn serialize_f64(self, _value: f64) -> Result<Self::Ok, Self::Error> { |
293 | Err(Error::DateInvalid) |
294 | } |
295 | |
296 | fn serialize_char(self, _value: char) -> Result<Self::Ok, Self::Error> { |
297 | Err(Error::DateInvalid) |
298 | } |
299 | |
300 | fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
301 | v.parse::<toml_datetime::Datetime>().map_err(Error::custom) |
302 | } |
303 | |
304 | fn serialize_bytes(self, _value: &[u8]) -> Result<Self::Ok, Self::Error> { |
305 | Err(Error::DateInvalid) |
306 | } |
307 | |
308 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
309 | Err(Error::DateInvalid) |
310 | } |
311 | |
312 | fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error> |
313 | where |
314 | T: serde::ser::Serialize, |
315 | { |
316 | Err(Error::DateInvalid) |
317 | } |
318 | |
319 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
320 | Err(Error::DateInvalid) |
321 | } |
322 | |
323 | fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> { |
324 | Err(Error::DateInvalid) |
325 | } |
326 | |
327 | fn serialize_unit_variant( |
328 | self, |
329 | _name: &'static str, |
330 | _variant_index: u32, |
331 | _variant: &'static str, |
332 | ) -> Result<Self::Ok, Self::Error> { |
333 | Err(Error::DateInvalid) |
334 | } |
335 | |
336 | fn serialize_newtype_struct<T: ?Sized>( |
337 | self, |
338 | _name: &'static str, |
339 | _value: &T, |
340 | ) -> Result<Self::Ok, Self::Error> |
341 | where |
342 | T: serde::ser::Serialize, |
343 | { |
344 | Err(Error::DateInvalid) |
345 | } |
346 | |
347 | fn serialize_newtype_variant<T: ?Sized>( |
348 | self, |
349 | _name: &'static str, |
350 | _variant_index: u32, |
351 | _variant: &'static str, |
352 | _value: &T, |
353 | ) -> Result<Self::Ok, Self::Error> |
354 | where |
355 | T: serde::ser::Serialize, |
356 | { |
357 | Err(Error::DateInvalid) |
358 | } |
359 | |
360 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
361 | Err(Error::DateInvalid) |
362 | } |
363 | |
364 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
365 | Err(Error::DateInvalid) |
366 | } |
367 | |
368 | fn serialize_tuple_struct( |
369 | self, |
370 | _name: &'static str, |
371 | _len: usize, |
372 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
373 | Err(Error::DateInvalid) |
374 | } |
375 | |
376 | fn serialize_tuple_variant( |
377 | self, |
378 | _name: &'static str, |
379 | _variant_index: u32, |
380 | _variant: &'static str, |
381 | _len: usize, |
382 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
383 | Err(Error::DateInvalid) |
384 | } |
385 | |
386 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
387 | Err(Error::DateInvalid) |
388 | } |
389 | |
390 | fn serialize_struct( |
391 | self, |
392 | _name: &'static str, |
393 | _len: usize, |
394 | ) -> Result<Self::SerializeStruct, Self::Error> { |
395 | Err(Error::DateInvalid) |
396 | } |
397 | |
398 | fn serialize_struct_variant( |
399 | self, |
400 | _name: &'static str, |
401 | _variant_index: u32, |
402 | _variant: &'static str, |
403 | _len: usize, |
404 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
405 | Err(Error::DateInvalid) |
406 | } |
407 | } |
408 | |
409 | #[derive (Default)] |
410 | struct MapValueSerializer { |
411 | is_none: bool, |
412 | } |
413 | |
414 | impl MapValueSerializer { |
415 | fn new() -> Self { |
416 | Self { is_none: false } |
417 | } |
418 | } |
419 | |
420 | impl serde::ser::Serializer for &mut MapValueSerializer { |
421 | type Ok = crate::Value; |
422 | type Error = Error; |
423 | type SerializeSeq = super::SerializeValueArray; |
424 | type SerializeTuple = super::SerializeValueArray; |
425 | type SerializeTupleStruct = super::SerializeValueArray; |
426 | type SerializeTupleVariant = super::SerializeValueArray; |
427 | type SerializeMap = super::SerializeMap; |
428 | type SerializeStruct = super::SerializeMap; |
429 | type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>; |
430 | |
431 | fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { |
432 | ValueSerializer::new().serialize_bool(v) |
433 | } |
434 | |
435 | fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { |
436 | ValueSerializer::new().serialize_i8(v) |
437 | } |
438 | |
439 | fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { |
440 | ValueSerializer::new().serialize_i16(v) |
441 | } |
442 | |
443 | fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { |
444 | ValueSerializer::new().serialize_i32(v) |
445 | } |
446 | |
447 | fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { |
448 | ValueSerializer::new().serialize_i64(v) |
449 | } |
450 | |
451 | fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { |
452 | ValueSerializer::new().serialize_u8(v) |
453 | } |
454 | |
455 | fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { |
456 | ValueSerializer::new().serialize_u16(v) |
457 | } |
458 | |
459 | fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { |
460 | ValueSerializer::new().serialize_u32(v) |
461 | } |
462 | |
463 | fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { |
464 | ValueSerializer::new().serialize_u64(v) |
465 | } |
466 | |
467 | fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { |
468 | ValueSerializer::new().serialize_f32(v) |
469 | } |
470 | |
471 | fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> { |
472 | ValueSerializer::new().serialize_f64(v) |
473 | } |
474 | |
475 | fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { |
476 | ValueSerializer::new().serialize_char(v) |
477 | } |
478 | |
479 | fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
480 | ValueSerializer::new().serialize_str(v) |
481 | } |
482 | |
483 | fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> { |
484 | ValueSerializer::new().serialize_bytes(value) |
485 | } |
486 | |
487 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
488 | self.is_none = true; |
489 | Err(Error::UnsupportedNone) |
490 | } |
491 | |
492 | fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error> |
493 | where |
494 | T: serde::ser::Serialize, |
495 | { |
496 | ValueSerializer::new().serialize_some(value) |
497 | } |
498 | |
499 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
500 | ValueSerializer::new().serialize_unit() |
501 | } |
502 | |
503 | fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { |
504 | ValueSerializer::new().serialize_unit_struct(name) |
505 | } |
506 | |
507 | fn serialize_unit_variant( |
508 | self, |
509 | name: &'static str, |
510 | variant_index: u32, |
511 | variant: &'static str, |
512 | ) -> Result<Self::Ok, Self::Error> { |
513 | ValueSerializer::new().serialize_unit_variant(name, variant_index, variant) |
514 | } |
515 | |
516 | fn serialize_newtype_struct<T: ?Sized>( |
517 | self, |
518 | name: &'static str, |
519 | value: &T, |
520 | ) -> Result<Self::Ok, Self::Error> |
521 | where |
522 | T: serde::ser::Serialize, |
523 | { |
524 | ValueSerializer::new().serialize_newtype_struct(name, value) |
525 | } |
526 | |
527 | fn serialize_newtype_variant<T: ?Sized>( |
528 | self, |
529 | name: &'static str, |
530 | variant_index: u32, |
531 | variant: &'static str, |
532 | value: &T, |
533 | ) -> Result<Self::Ok, Self::Error> |
534 | where |
535 | T: serde::ser::Serialize, |
536 | { |
537 | ValueSerializer::new().serialize_newtype_variant(name, variant_index, variant, value) |
538 | } |
539 | |
540 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
541 | ValueSerializer::new().serialize_seq(len) |
542 | } |
543 | |
544 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
545 | ValueSerializer::new().serialize_tuple(len) |
546 | } |
547 | |
548 | fn serialize_tuple_struct( |
549 | self, |
550 | name: &'static str, |
551 | len: usize, |
552 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
553 | ValueSerializer::new().serialize_tuple_struct(name, len) |
554 | } |
555 | |
556 | fn serialize_tuple_variant( |
557 | self, |
558 | name: &'static str, |
559 | variant_index: u32, |
560 | variant: &'static str, |
561 | len: usize, |
562 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
563 | ValueSerializer::new().serialize_tuple_variant(name, variant_index, variant, len) |
564 | } |
565 | |
566 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
567 | ValueSerializer::new().serialize_map(len) |
568 | } |
569 | |
570 | fn serialize_struct( |
571 | self, |
572 | name: &'static str, |
573 | len: usize, |
574 | ) -> Result<Self::SerializeStruct, Self::Error> { |
575 | ValueSerializer::new().serialize_struct(name, len) |
576 | } |
577 | |
578 | fn serialize_struct_variant( |
579 | self, |
580 | name: &'static str, |
581 | variant_index: u32, |
582 | variant: &'static str, |
583 | len: usize, |
584 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
585 | ValueSerializer::new().serialize_struct_variant(name, variant_index, variant, len) |
586 | } |
587 | } |
588 | |