1use serde::{ser::SerializeStruct, Serialize};
2
3pub fn to_string_pretty<T>(value: &T) -> serde_json::Result<String>
4where
5 T: ?Sized + Serialize,
6{
7 let vec = to_vec_pretty(value)?;
8 let string = unsafe {
9 String::from_utf8_unchecked(vec)
11 };
12 Ok(string)
13}
14
15pub fn to_vec_pretty<T>(value: &T) -> serde_json::Result<Vec<u8>>
16where
17 T: ?Sized + Serialize,
18{
19 let mut writer = Vec::with_capacity(128);
20 to_writer_pretty(&mut writer, value)?;
21 Ok(writer)
22}
23
24pub fn to_writer_pretty<W, T>(writer: W, value: &T) -> serde_json::Result<()>
25where
26 W: std::io::Write,
27 T: ?Sized + Serialize,
28{
29 let mut ser = serde_json::Serializer::pretty(writer);
30 let ser = HexSerializer::new(&mut ser);
31 value.serialize(ser)
32}
33
34pub struct HexSerializer<S>
35where
36 S: serde::ser::Serializer,
37{
38 inner: S,
39}
40
41impl<S> HexSerializer<S>
42where
43 S: serde::ser::Serializer,
44{
45 pub fn new(inner: S) -> Self {
46 Self { inner }
47 }
48}
49
50impl<S> serde::ser::Serializer for HexSerializer<S>
51where
52 S: serde::ser::Serializer,
53{
54 type Ok = S::Ok;
55 type Error = S::Error;
56
57 type SerializeSeq = S::SerializeSeq;
58 type SerializeTuple = S::SerializeTuple;
59 type SerializeTupleStruct = S::SerializeTupleStruct;
60 type SerializeTupleVariant = S::SerializeTupleVariant;
61 type SerializeMap = S::SerializeMap;
62 type SerializeStruct = HexSerializeStruct<S::SerializeStruct>;
63 type SerializeStructVariant = S::SerializeStructVariant;
64
65 fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
66 self.inner.serialize_bool(v)
67 }
68
69 fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
70 self.inner.serialize_i8(v)
71 }
72
73 fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
74 self.inner.serialize_i16(v)
75 }
76
77 fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
78 self.inner.serialize_i32(v)
79 }
80
81 fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
82 self.inner.serialize_i64(v)
83 }
84
85 fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
86 self.inner.serialize_u8(v)
87 }
88
89 fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
90 self.inner.serialize_u16(v)
91 }
92
93 fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
94 self.inner.serialize_u32(v)
95 }
96
97 fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
98 self.inner.serialize_u64(v)
99 }
100
101 fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
102 self.inner.serialize_f32(v)
103 }
104
105 fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
106 self.inner.serialize_f64(v)
107 }
108
109 fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
110 self.inner.serialize_char(v)
111 }
112
113 fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
114 self.inner.serialize_str(v)
115 }
116
117 fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
118 self.inner.serialize_bytes(v)
119 }
120
121 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
122 self.inner.serialize_none()
123 }
124
125 fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
126 where
127 T: ?Sized + Serialize,
128 {
129 self.inner.serialize_some(value)
130 }
131
132 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
133 self.inner.serialize_unit()
134 }
135
136 fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
137 self.inner.serialize_unit_struct(name)
138 }
139
140 fn serialize_unit_variant(
141 self,
142 name: &'static str,
143 variant_index: u32,
144 variant: &'static str,
145 ) -> Result<Self::Ok, Self::Error> {
146 self.inner
147 .serialize_unit_variant(name, variant_index, variant)
148 }
149
150 fn serialize_newtype_struct<T>(
151 self,
152 name: &'static str,
153 value: &T,
154 ) -> Result<Self::Ok, Self::Error>
155 where
156 T: ?Sized + Serialize,
157 {
158 self.inner.serialize_newtype_struct(name, value)
159 }
160
161 fn serialize_newtype_variant<T>(
162 self,
163 name: &'static str,
164 variant_index: u32,
165 variant: &'static str,
166 value: &T,
167 ) -> Result<Self::Ok, Self::Error>
168 where
169 T: ?Sized + Serialize,
170 {
171 self.inner
172 .serialize_newtype_variant(name, variant_index, variant, value)
173 }
174
175 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
176 self.inner.serialize_seq(len)
177 }
178
179 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
180 self.inner.serialize_tuple(len)
181 }
182
183 fn serialize_tuple_struct(
184 self,
185 name: &'static str,
186 len: usize,
187 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
188 self.inner.serialize_tuple_struct(name, len)
189 }
190
191 fn serialize_tuple_variant(
192 self,
193 name: &'static str,
194 variant_index: u32,
195 variant: &'static str,
196 len: usize,
197 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
198 self.inner
199 .serialize_tuple_variant(name, variant_index, variant, len)
200 }
201
202 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
203 self.inner.serialize_map(len)
204 }
205
206 fn serialize_struct(
207 self,
208 name: &'static str,
209 len: usize,
210 ) -> Result<Self::SerializeStruct, Self::Error> {
211 match self.inner.serialize_struct(name, len) {
212 Ok(s) => Ok(Self::SerializeStruct::new(s)),
213 Err(e) => Err(e),
214 }
215 }
216
217 fn serialize_struct_variant(
218 self,
219 name: &'static str,
220 variant_index: u32,
221 variant: &'static str,
222 len: usize,
223 ) -> Result<Self::SerializeStructVariant, Self::Error> {
224 self.inner
225 .serialize_struct_variant(name, variant_index, variant, len)
226 }
227}
228
229pub struct HexSerializeStruct<S>
230where
231 S: SerializeStruct,
232{
233 inner: S,
234}
235
236impl<S> HexSerializeStruct<S>
237where
238 S: SerializeStruct,
239{
240 pub fn new(inner: S) -> Self {
241 Self { inner }
242 }
243}
244
245impl<S> SerializeStruct for HexSerializeStruct<S>
246where
247 S: SerializeStruct,
248{
249 type Ok = S::Ok;
250 type Error = S::Error;
251
252 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
253 where
254 T: ?Sized + Serialize,
255 {
256 let as_vec = match serde_json::to_vec(value) {
263 Ok(as_vec) => as_vec,
264 Err(_) => return self.inner.serialize_field(key, value),
265 };
266 let val: Vec<u8> = match serde_json::from_slice(&as_vec) {
267 Ok(val) => val,
268 Err(_) => return self.inner.serialize_field(key, value),
269 };
270 self.inner.serialize_field(key, &hex::encode(&val))
271 }
272
273 fn end(self) -> Result<Self::Ok, Self::Error> {
274 self.inner.end()
275 }
276}
277
278pub mod value {
279 use std::fmt::Display;
280
281 use serde::{ser::Impossible, Serialize};
282 use serde_json::{Error, Map, Result, Value};
283
284 pub fn to_value<T>(value: T) -> Result<Value>
285 where
286 T: Serialize,
287 {
288 value.serialize(Serializer)
289 }
290
291 pub struct Serializer;
292
293 impl serde::Serializer for Serializer {
294 type Ok = Value;
295 type Error = Error;
296
297 type SerializeSeq = SerializeVec;
298 type SerializeTuple = SerializeVec;
299 type SerializeTupleStruct = SerializeVec;
300 type SerializeTupleVariant = SerializeTupleVariant;
301 type SerializeMap = SerializeMap;
302 type SerializeStruct = SerializeMap;
303 type SerializeStructVariant = SerializeStructVariant;
304
305 #[inline]
306 fn serialize_bool(self, value: bool) -> Result<Value> {
307 Ok(Value::Bool(value))
308 }
309
310 #[inline]
311 fn serialize_i8(self, value: i8) -> Result<Value> {
312 self.serialize_i64(value as i64)
313 }
314
315 #[inline]
316 fn serialize_i16(self, value: i16) -> Result<Value> {
317 self.serialize_i64(value as i64)
318 }
319
320 #[inline]
321 fn serialize_i32(self, value: i32) -> Result<Value> {
322 self.serialize_i64(value as i64)
323 }
324
325 fn serialize_i64(self, value: i64) -> Result<Value> {
326 Ok(Value::Number(value.into()))
327 }
328
329 fn serialize_i128(self, value: i128) -> Result<Value> {
330 if let Ok(value) = i64::try_from(value) {
331 Ok(Value::Number(value.into()))
332 } else {
333 Err(serde::ser::Error::custom("number out of range"))
334 }
335 }
336
337 #[inline]
338 fn serialize_u8(self, value: u8) -> Result<Value> {
339 self.serialize_u64(value as u64)
340 }
341
342 #[inline]
343 fn serialize_u16(self, value: u16) -> Result<Value> {
344 self.serialize_u64(value as u64)
345 }
346
347 #[inline]
348 fn serialize_u32(self, value: u32) -> Result<Value> {
349 self.serialize_u64(value as u64)
350 }
351
352 #[inline]
353 fn serialize_u64(self, value: u64) -> Result<Value> {
354 Ok(Value::Number(value.into()))
355 }
356
357 fn serialize_u128(self, value: u128) -> Result<Value> {
358 if let Ok(value) = u64::try_from(value) {
359 Ok(Value::Number(value.into()))
360 } else {
361 Err(serde::ser::Error::custom("number out of range"))
362 }
363 }
364
365 #[inline]
366 fn serialize_f32(self, float: f32) -> Result<Value> {
367 Ok(Value::from(float))
368 }
369
370 #[inline]
371 fn serialize_f64(self, float: f64) -> Result<Value> {
372 Ok(Value::from(float))
373 }
374
375 #[inline]
376 fn serialize_char(self, value: char) -> Result<Value> {
377 let mut s = String::new();
378 s.push(value);
379 Ok(Value::String(s))
380 }
381
382 #[inline]
383 fn serialize_str(self, value: &str) -> Result<Value> {
384 Ok(Value::String(value.to_owned()))
385 }
386
387 fn serialize_bytes(self, value: &[u8]) -> Result<Value> {
388 Ok(Value::String(hex::encode(value)))
389 }
390
391 #[inline]
392 fn serialize_unit(self) -> Result<Value> {
393 Ok(Value::Null)
394 }
395
396 #[inline]
397 fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> {
398 self.serialize_unit()
399 }
400
401 #[inline]
402 fn serialize_unit_variant(
403 self,
404 _name: &'static str,
405 _variant_index: u32,
406 variant: &'static str,
407 ) -> Result<Value> {
408 self.serialize_str(variant)
409 }
410
411 #[inline]
412 fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value>
413 where
414 T: ?Sized + Serialize,
415 {
416 value.serialize(self)
417 }
418
419 fn serialize_newtype_variant<T>(
420 self,
421 _name: &'static str,
422 _variant_index: u32,
423 variant: &'static str,
424 value: &T,
425 ) -> Result<Value>
426 where
427 T: ?Sized + Serialize,
428 {
429 let mut values = Map::new();
430 values.insert(String::from(variant), to_value(value)?);
431 Ok(Value::Object(values))
432 }
433
434 #[inline]
435 fn serialize_none(self) -> Result<Value> {
436 self.serialize_unit()
437 }
438
439 #[inline]
440 fn serialize_some<T>(self, value: &T) -> Result<Value>
441 where
442 T: ?Sized + Serialize,
443 {
444 value.serialize(self)
445 }
446
447 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
448 Ok(SerializeVec {
449 vec: Vec::with_capacity(len.unwrap_or(0)),
450 })
451 }
452
453 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
454 self.serialize_seq(Some(len))
455 }
456
457 fn serialize_tuple_struct(
458 self,
459 _name: &'static str,
460 len: usize,
461 ) -> Result<Self::SerializeTupleStruct> {
462 self.serialize_seq(Some(len))
463 }
464
465 fn serialize_tuple_variant(
466 self,
467 _name: &'static str,
468 _variant_index: u32,
469 variant: &'static str,
470 len: usize,
471 ) -> Result<Self::SerializeTupleVariant> {
472 Ok(SerializeTupleVariant {
473 name: String::from(variant),
474 vec: Vec::with_capacity(len),
475 })
476 }
477
478 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
479 Ok(SerializeMap {
480 map: Map::new(),
481 next_key: None,
482 })
483 }
484
485 fn serialize_struct(
486 self,
487 _name: &'static str,
488 len: usize,
489 ) -> Result<Self::SerializeStruct> {
490 self.serialize_map(Some(len))
491 }
492
493 fn serialize_struct_variant(
494 self,
495 _name: &'static str,
496 _variant_index: u32,
497 variant: &'static str,
498 _len: usize,
499 ) -> Result<Self::SerializeStructVariant> {
500 Ok(SerializeStructVariant {
501 name: String::from(variant),
502 map: Map::new(),
503 })
504 }
505
506 fn collect_str<T>(self, value: &T) -> Result<Value>
507 where
508 T: ?Sized + Display,
509 {
510 Ok(Value::String(value.to_string()))
511 }
512 }
513
514 pub struct SerializeVec {
515 vec: Vec<Value>,
516 }
517
518 pub struct SerializeTupleVariant {
519 name: String,
520 vec: Vec<Value>,
521 }
522
523 pub struct SerializeMap {
524 map: Map<String, Value>,
525 next_key: Option<String>,
526 }
527
528 pub struct SerializeStructVariant {
529 name: String,
530 map: Map<String, Value>,
531 }
532
533 impl serde::ser::SerializeSeq for SerializeVec {
534 type Ok = Value;
535 type Error = Error;
536
537 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
538 where
539 T: ?Sized + Serialize,
540 {
541 self.vec.push(to_value(value)?);
542 Ok(())
543 }
544
545 fn end(self) -> Result<Value> {
546 Ok(Value::Array(self.vec))
547 }
548 }
549
550 impl serde::ser::SerializeTuple for SerializeVec {
551 type Ok = Value;
552 type Error = Error;
553
554 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
555 where
556 T: ?Sized + Serialize,
557 {
558 serde::ser::SerializeSeq::serialize_element(self, value)
559 }
560
561 fn end(self) -> Result<Value> {
562 serde::ser::SerializeSeq::end(self)
563 }
564 }
565
566 impl serde::ser::SerializeTupleStruct for SerializeVec {
567 type Ok = Value;
568 type Error = Error;
569
570 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
571 where
572 T: ?Sized + Serialize,
573 {
574 serde::ser::SerializeSeq::serialize_element(self, value)
575 }
576
577 fn end(self) -> Result<Value> {
578 serde::ser::SerializeSeq::end(self)
579 }
580 }
581
582 impl serde::ser::SerializeTupleVariant for SerializeTupleVariant {
583 type Ok = Value;
584 type Error = Error;
585
586 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
587 where
588 T: ?Sized + Serialize,
589 {
590 self.vec.push(to_value(value)?);
591 Ok(())
592 }
593
594 fn end(self) -> Result<Value> {
595 let mut object = Map::new();
596
597 object.insert(self.name, Value::Array(self.vec));
598
599 Ok(Value::Object(object))
600 }
601 }
602
603 impl serde::ser::SerializeMap for SerializeMap {
604 type Ok = Value;
605 type Error = Error;
606
607 fn serialize_key<T>(&mut self, key: &T) -> Result<()>
608 where
609 T: ?Sized + Serialize,
610 {
611 self.next_key = Some(key.serialize(MapKeySerializer)?);
612 Ok(())
613 }
614
615 fn serialize_value<T>(&mut self, value: &T) -> Result<()>
616 where
617 T: ?Sized + Serialize,
618 {
619 let key = self.next_key.take();
620 let key = key.expect("serialize_value called before serialize_key");
623
624 let as_vec = match serde_json::to_vec(value) {
631 Ok(as_vec) => as_vec,
632 Err(_) => {
633 self.map.insert(key, to_value(value)?);
634 return Ok(());
635 }
636 };
637 let val: Vec<u8> = match serde_json::from_slice(&as_vec) {
638 Ok(val) => val,
639 Err(_) => {
640 self.map.insert(key, to_value(value)?);
641 return Ok(());
642 }
643 };
644
645 self.map.insert(key, to_value(hex::encode(&val))?);
646 Ok(())
647 }
648
649 fn end(self) -> Result<Value> {
650 Ok(Value::Object(self.map))
651 }
652 }
653
654 impl serde::ser::SerializeStruct for SerializeMap {
655 type Ok = Value;
656 type Error = Error;
657
658 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
659 where
660 T: ?Sized + Serialize,
661 {
662 serde::ser::SerializeMap::serialize_entry(self, key, value)
663 }
664
665 fn end(self) -> Result<Value> {
666 serde::ser::SerializeMap::end(self)
667 }
668 }
669
670 impl serde::ser::SerializeStructVariant for SerializeStructVariant {
671 type Ok = Value;
672 type Error = Error;
673
674 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
675 where
676 T: ?Sized + Serialize,
677 {
678 self.map.insert(String::from(key), to_value(value)?);
679 Ok(())
680 }
681
682 fn end(self) -> Result<Value> {
683 let mut object = Map::new();
684
685 object.insert(self.name, Value::Object(self.map));
686
687 Ok(Value::Object(object))
688 }
689 }
690
691 struct MapKeySerializer;
692
693 fn key_must_be_a_string() -> Error {
694 serde::ser::Error::custom("key must be a string")
695 }
696
697 fn float_key_must_be_finite() -> Error {
698 serde::ser::Error::custom("float key must be finite")
699 }
700
701 impl serde::Serializer for MapKeySerializer {
702 type Ok = String;
703 type Error = Error;
704
705 type SerializeSeq = Impossible<String, Error>;
706 type SerializeTuple = Impossible<String, Error>;
707 type SerializeTupleStruct = Impossible<String, Error>;
708 type SerializeTupleVariant = Impossible<String, Error>;
709 type SerializeMap = Impossible<String, Error>;
710 type SerializeStruct = Impossible<String, Error>;
711 type SerializeStructVariant = Impossible<String, Error>;
712
713 #[inline]
714 fn serialize_unit_variant(
715 self,
716 _name: &'static str,
717 _variant_index: u32,
718 variant: &'static str,
719 ) -> Result<String> {
720 Ok(variant.to_owned())
721 }
722
723 #[inline]
724 fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<String>
725 where
726 T: ?Sized + Serialize,
727 {
728 value.serialize(self)
729 }
730
731 fn serialize_bool(self, value: bool) -> Result<String> {
732 Ok(value.to_string())
733 }
734
735 fn serialize_i8(self, value: i8) -> Result<String> {
736 Ok(value.to_string())
737 }
738
739 fn serialize_i16(self, value: i16) -> Result<String> {
740 Ok(value.to_string())
741 }
742
743 fn serialize_i32(self, value: i32) -> Result<String> {
744 Ok(value.to_string())
745 }
746
747 fn serialize_i64(self, value: i64) -> Result<String> {
748 Ok(value.to_string())
749 }
750
751 fn serialize_i128(self, value: i128) -> Result<String> {
752 Ok(value.to_string())
753 }
754
755 fn serialize_u8(self, value: u8) -> Result<String> {
756 Ok(value.to_string())
757 }
758
759 fn serialize_u16(self, value: u16) -> Result<String> {
760 Ok(value.to_string())
761 }
762
763 fn serialize_u32(self, value: u32) -> Result<String> {
764 Ok(value.to_string())
765 }
766
767 fn serialize_u64(self, value: u64) -> Result<String> {
768 Ok(value.to_string())
769 }
770
771 fn serialize_u128(self, value: u128) -> Result<String> {
772 Ok(value.to_string())
773 }
774
775 fn serialize_f32(self, value: f32) -> Result<String> {
776 if value.is_finite() {
777 Ok(ryu::Buffer::new().format_finite(value).to_owned())
778 } else {
779 Err(float_key_must_be_finite())
780 }
781 }
782
783 fn serialize_f64(self, value: f64) -> Result<String> {
784 if value.is_finite() {
785 Ok(ryu::Buffer::new().format_finite(value).to_owned())
786 } else {
787 Err(float_key_must_be_finite())
788 }
789 }
790
791 #[inline]
792 fn serialize_char(self, value: char) -> Result<String> {
793 Ok({
794 let mut s = String::new();
795 s.push(value);
796 s
797 })
798 }
799
800 #[inline]
801 fn serialize_str(self, value: &str) -> Result<String> {
802 Ok(value.to_owned())
803 }
804
805 fn serialize_bytes(self, _value: &[u8]) -> Result<String> {
806 Err(key_must_be_a_string())
807 }
808
809 fn serialize_unit(self) -> Result<String> {
810 Err(key_must_be_a_string())
811 }
812
813 fn serialize_unit_struct(self, _name: &'static str) -> Result<String> {
814 Err(key_must_be_a_string())
815 }
816
817 fn serialize_newtype_variant<T>(
818 self,
819 _name: &'static str,
820 _variant_index: u32,
821 _variant: &'static str,
822 _value: &T,
823 ) -> Result<String>
824 where
825 T: ?Sized + Serialize,
826 {
827 Err(key_must_be_a_string())
828 }
829
830 fn serialize_none(self) -> Result<String> {
831 Err(key_must_be_a_string())
832 }
833
834 fn serialize_some<T>(self, _value: &T) -> Result<String>
835 where
836 T: ?Sized + Serialize,
837 {
838 Err(key_must_be_a_string())
839 }
840
841 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
842 Err(key_must_be_a_string())
843 }
844
845 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
846 Err(key_must_be_a_string())
847 }
848
849 fn serialize_tuple_struct(
850 self,
851 _name: &'static str,
852 _len: usize,
853 ) -> Result<Self::SerializeTupleStruct> {
854 Err(key_must_be_a_string())
855 }
856
857 fn serialize_tuple_variant(
858 self,
859 _name: &'static str,
860 _variant_index: u32,
861 _variant: &'static str,
862 _len: usize,
863 ) -> Result<Self::SerializeTupleVariant> {
864 Err(key_must_be_a_string())
865 }
866
867 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
868 Err(key_must_be_a_string())
869 }
870
871 fn serialize_struct(
872 self,
873 _name: &'static str,
874 _len: usize,
875 ) -> Result<Self::SerializeStruct> {
876 Err(key_must_be_a_string())
877 }
878
879 fn serialize_struct_variant(
880 self,
881 _name: &'static str,
882 _variant_index: u32,
883 _variant: &'static str,
884 _len: usize,
885 ) -> Result<Self::SerializeStructVariant> {
886 Err(key_must_be_a_string())
887 }
888
889 fn collect_str<T>(self, value: &T) -> Result<String>
890 where
891 T: ?Sized + Display,
892 {
893 Ok(value.to_string())
894 }
895 }
896}