1use std::fmt;
2use std::io;
3use std::mem::{self, MaybeUninit};
4
5use serde::{Serialize, Serializer};
6#[cfg(feature = "json")]
7use serde_json::Serializer as JsonSerializer;
8
9use crate::{Argument, FormatType};
10
11#[cfg(feature = "json")]
12type CompactJsonSerializer<W> = JsonSerializer<W, serde_json::ser::CompactFormatter>;
13#[cfg(feature = "json")]
14type PrettyJsonSerializer<W> = JsonSerializer<W, serde_json::ser::PrettyFormatter<'static>>;
15
16pub type FormatFn<T> = fn(&T, fmt: &mut fmt::Formatter) -> fmt::Result;
17
18struct FmtProxy<'a> {
19 data: &'a (),
20 func: FormatFn<()>,
21}
22
23impl<'a> FmtProxy<'a> {
24 pub fn new<T>(data: &'a T, func: FormatFn<T>) -> Self {
25 unsafe {
26 FmtProxy {
27 data: &*(data as *const T as *const ()),
28 func: std::mem::transmute(func),
29 }
30 }
31 }
32}
33
34impl fmt::Display for FmtProxy<'_> {
35 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
36 (self.func)(self.data, fmt)
37 }
38}
39
40#[derive(Debug)]
41pub enum FormatError {
42 Type(FormatType),
43 Serde(String),
44 Io(io::Error),
45}
46
47impl fmt::Display for FormatError {
48 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49 match self {
50 FormatError::Type(format) => write!(f, "cannot format as {}", format),
51 FormatError::Serde(error) => write!(f, "{}", error),
52 FormatError::Io(error) => write!(f, "{}", error),
53 }
54 }
55}
56
57impl std::error::Error for FormatError {}
58
59impl serde::ser::Error for FormatError {
60 fn custom<T>(msg: T) -> Self
61 where
62 T: fmt::Display,
63 {
64 FormatError::Serde(msg.to_string())
65 }
66}
67
68#[cfg(feature = "json")]
69impl From<serde_json::Error> for FormatError {
70 fn from(error: serde_json::Error) -> Self {
71 FormatError::Serde(error.to_string())
72 }
73}
74
75enum FormatterTarget<W> {
76 Write(W),
77 #[cfg(feature = "json")]
78 Compact(CompactJsonSerializer<W>),
79 #[cfg(feature = "json")]
80 Pretty(PrettyJsonSerializer<W>),
81}
82
83impl<W> FormatterTarget<W>
84where
85 W: io::Write,
86{
87 pub fn new(write: W) -> Self {
88 FormatterTarget::Write(write)
89 }
90
91 #[cfg(feature = "json")]
92 pub fn compact(write: W) -> Self {
93 FormatterTarget::Compact(JsonSerializer::new(write))
94 }
95
96 #[cfg(feature = "json")]
97 pub fn pretty(write: W) -> Self {
98 FormatterTarget::Pretty(JsonSerializer::pretty(write))
99 }
100
101 pub fn into_inner(self) -> W {
102 match self {
103 FormatterTarget::Write(write) => write,
104 #[cfg(feature = "json")]
105 FormatterTarget::Compact(write) => write.into_inner(),
106 #[cfg(feature = "json")]
107 FormatterTarget::Pretty(write) => write.into_inner(),
108 }
109 }
110
111 pub fn as_write(&mut self) -> &mut W {
112 self.convert(FormatterTarget::new);
113 #[cfg_attr(not(feature = "json"), allow(unreachable_patterns))]
114 match self {
115 FormatterTarget::Write(inner) => inner,
116 _ => unreachable!(),
117 }
118 }
119
120 #[cfg(feature = "json")]
121 pub fn as_compact(&mut self) -> &mut CompactJsonSerializer<W> {
122 self.convert(FormatterTarget::compact);
123 #[cfg_attr(not(feature = "json"), allow(unreachable_patterns))]
124 match self {
125 FormatterTarget::Compact(inner) => inner,
126 _ => unreachable!(),
127 }
128 }
129
130 #[cfg(feature = "json")]
131 pub fn as_pretty(&mut self) -> &mut PrettyJsonSerializer<W> {
132 self.convert(FormatterTarget::pretty);
133 match self {
134 FormatterTarget::Pretty(inner) => inner,
135 _ => unreachable!(),
136 }
137 }
138
139 fn convert<F>(&mut self, f: F)
140 where
141 F: FnOnce(W) -> Self,
142 {
143 unsafe {
144 let mut placeholder = MaybeUninit::uninit();
145 mem::swap(self, &mut *placeholder.as_mut_ptr());
146 let converted = f(placeholder.assume_init().into_inner());
147 mem::forget(mem::replace(self, converted));
148 }
149 }
150}
151
152pub struct Formatter<W> {
153 target: FormatterTarget<W>,
154 ty: FormatType,
155 alternate: bool,
156}
157
158impl<W> Formatter<W>
159where
160 W: io::Write,
161{
162 pub fn new(write: W) -> Self {
163 Formatter {
164 target: FormatterTarget::new(write),
165 ty: FormatType::Display,
166 alternate: false,
167 }
168 }
169
170 pub fn with_type(mut self, ty: FormatType) -> Self {
171 self.ty = ty;
172 self
173 }
174
175 pub fn with_alternate(mut self, alternate: bool) -> Self {
176 self.alternate = alternate;
177 self
178 }
179
180 pub fn format(&mut self, value: Argument<'_>) -> Result<(), FormatError> {
181 value.serialize(self)
184 }
185
186 #[cfg(feature = "json")]
187 fn serialize<D: Serialize>(&mut self, value: &D) -> Result<(), FormatError> {
188 if self.alternate {
189 value.serialize(self.target.as_pretty()).map_err(Into::into)
190 } else {
191 value
192 .serialize(self.target.as_compact())
193 .map_err(Into::into)
194 }
195 }
196
197 #[cfg(not(feature = "json"))]
198 fn serialize<D: Serialize>(&mut self, _value: &D) -> Result<(), FormatError> {
199 Err(FormatError::Type(FormatType::Object))
200 }
201
202 fn fmt_internal<T>(&mut self, value: &T, fmt: FormatFn<T>) -> Result<(), FormatError> {
203 let proxy = FmtProxy::new(value, fmt);
204
205 if self.alternate {
206 write!(self.target.as_write(), "{:#}", proxy).map_err(FormatError::Io)
207 } else {
208 write!(self.target.as_write(), "{}", proxy).map_err(FormatError::Io)
209 }
210 }
211
212 #[allow(unused)]
214 fn debug<D: fmt::Debug>(&mut self, value: &D) -> Result<(), FormatError> {
215 self.fmt_internal(value, fmt::Debug::fmt)
216 }
217
218 fn display<D: fmt::Display>(&mut self, value: &D) -> Result<(), FormatError> {
219 self.fmt_internal(value, fmt::Display::fmt)
220 }
221
222 fn octal<D: fmt::Octal>(&mut self, value: &D) -> Result<(), FormatError> {
223 self.fmt_internal(value, fmt::Octal::fmt)
224 }
225
226 fn lower_hex<D: fmt::LowerHex>(&mut self, value: &D) -> Result<(), FormatError> {
227 self.fmt_internal(value, fmt::LowerHex::fmt)
228 }
229
230 fn upper_hex<D: fmt::UpperHex>(&mut self, value: &D) -> Result<(), FormatError> {
231 self.fmt_internal(value, fmt::UpperHex::fmt)
232 }
233
234 fn pointer<D: fmt::Pointer>(&mut self, value: &D) -> Result<(), FormatError> {
235 self.fmt_internal(value, fmt::Pointer::fmt)
236 }
237
238 fn binary<D: fmt::Binary>(&mut self, value: &D) -> Result<(), FormatError> {
239 self.fmt_internal(value, fmt::Binary::fmt)
240 }
241
242 fn lower_exp<D: fmt::LowerExp>(&mut self, value: &D) -> Result<(), FormatError> {
243 self.fmt_internal(value, fmt::LowerExp::fmt)
244 }
245
246 fn upper_exp<D: fmt::UpperExp>(&mut self, value: &D) -> Result<(), FormatError> {
247 self.fmt_internal(value, fmt::UpperExp::fmt)
248 }
249}
250
251#[cfg(feature = "json")]
252pub enum SerializeSeq<'a, W: io::Write> {
253 Compact(<&'a mut CompactJsonSerializer<W> as Serializer>::SerializeSeq),
254 Pretty(<&'a mut PrettyJsonSerializer<W> as Serializer>::SerializeSeq),
255}
256
257#[cfg(feature = "json")]
258impl<'a, W: io::Write> serde::ser::SerializeSeq for SerializeSeq<'a, W> {
259 type Ok = ();
260 type Error = FormatError;
261
262 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
263 where
264 T: Serialize,
265 {
266 match self {
267 SerializeSeq::Compact(compound) => compound.serialize_element(value),
268 SerializeSeq::Pretty(compound) => compound.serialize_element(value),
269 }
270 .map_err(Into::into)
271 }
272
273 fn end(self) -> Result<Self::Ok, Self::Error> {
274 match self {
275 SerializeSeq::Compact(compound) => compound.end(),
276 SerializeSeq::Pretty(compound) => compound.end(),
277 }
278 .map_err(Into::into)
279 }
280}
281
282#[cfg(feature = "json")]
283pub enum SerializeTuple<'a, W: io::Write> {
284 Compact(<&'a mut CompactJsonSerializer<W> as Serializer>::SerializeTuple),
285 Pretty(<&'a mut PrettyJsonSerializer<W> as Serializer>::SerializeTuple),
286}
287
288#[cfg(feature = "json")]
289impl<'a, W: io::Write> serde::ser::SerializeTuple for SerializeTuple<'a, W> {
290 type Ok = ();
291 type Error = FormatError;
292
293 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
294 where
295 T: Serialize,
296 {
297 match self {
298 SerializeTuple::Compact(compound) => compound.serialize_element(value),
299 SerializeTuple::Pretty(compound) => compound.serialize_element(value),
300 }
301 .map_err(Into::into)
302 }
303
304 fn end(self) -> Result<Self::Ok, Self::Error> {
305 match self {
306 SerializeTuple::Compact(compound) => compound.end(),
307 SerializeTuple::Pretty(compound) => compound.end(),
308 }
309 .map_err(Into::into)
310 }
311}
312
313#[cfg(feature = "json")]
314pub enum SerializeTupleStruct<'a, W: io::Write> {
315 Compact(<&'a mut CompactJsonSerializer<W> as Serializer>::SerializeTupleStruct),
316 Pretty(<&'a mut PrettyJsonSerializer<W> as Serializer>::SerializeTupleStruct),
317}
318
319#[cfg(feature = "json")]
320impl<'a, W: io::Write> serde::ser::SerializeTupleStruct for SerializeTupleStruct<'a, W> {
321 type Ok = ();
322 type Error = FormatError;
323
324 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
325 where
326 T: Serialize,
327 {
328 match self {
329 SerializeTupleStruct::Compact(compound) => compound.serialize_field(value),
330 SerializeTupleStruct::Pretty(compound) => compound.serialize_field(value),
331 }
332 .map_err(Into::into)
333 }
334
335 fn end(self) -> Result<Self::Ok, Self::Error> {
336 match self {
337 SerializeTupleStruct::Compact(compound) => compound.end(),
338 SerializeTupleStruct::Pretty(compound) => compound.end(),
339 }
340 .map_err(Into::into)
341 }
342}
343
344#[cfg(feature = "json")]
345pub enum SerializeTupleVariant<'a, W: io::Write> {
346 Compact(<&'a mut CompactJsonSerializer<W> as Serializer>::SerializeTupleVariant),
347 Pretty(<&'a mut PrettyJsonSerializer<W> as Serializer>::SerializeTupleVariant),
348}
349
350#[cfg(feature = "json")]
351impl<'a, W: io::Write> serde::ser::SerializeTupleVariant for SerializeTupleVariant<'a, W> {
352 type Ok = ();
353 type Error = FormatError;
354
355 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
356 where
357 T: Serialize,
358 {
359 match self {
360 SerializeTupleVariant::Compact(compound) => compound.serialize_field(value),
361 SerializeTupleVariant::Pretty(compound) => compound.serialize_field(value),
362 }
363 .map_err(Into::into)
364 }
365
366 fn end(self) -> Result<Self::Ok, Self::Error> {
367 match self {
368 SerializeTupleVariant::Compact(compound) => compound.end(),
369 SerializeTupleVariant::Pretty(compound) => compound.end(),
370 }
371 .map_err(Into::into)
372 }
373}
374
375#[cfg(feature = "json")]
376pub enum SerializeMap<'a, W: io::Write> {
377 Compact(<&'a mut CompactJsonSerializer<W> as Serializer>::SerializeMap),
378 Pretty(<&'a mut PrettyJsonSerializer<W> as Serializer>::SerializeMap),
379}
380
381#[cfg(feature = "json")]
382impl<'a, W: io::Write> serde::ser::SerializeMap for SerializeMap<'a, W> {
383 type Ok = ();
384 type Error = FormatError;
385
386 fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
387 where
388 T: Serialize,
389 {
390 match self {
391 SerializeMap::Compact(compound) => compound.serialize_key(key),
392 SerializeMap::Pretty(compound) => compound.serialize_key(key),
393 }
394 .map_err(Into::into)
395 }
396
397 fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
398 where
399 T: Serialize,
400 {
401 match self {
402 SerializeMap::Compact(compound) => compound.serialize_value(value),
403 SerializeMap::Pretty(compound) => compound.serialize_value(value),
404 }
405 .map_err(Into::into)
406 }
407
408 fn end(self) -> Result<Self::Ok, Self::Error> {
409 match self {
410 SerializeMap::Compact(compound) => compound.end(),
411 SerializeMap::Pretty(compound) => compound.end(),
412 }
413 .map_err(Into::into)
414 }
415
416 fn serialize_entry<K: ?Sized, V: ?Sized>(
417 &mut self,
418 key: &K,
419 value: &V,
420 ) -> Result<(), Self::Error>
421 where
422 K: Serialize,
423 V: Serialize,
424 {
425 match self {
426 SerializeMap::Compact(compound) => compound.serialize_entry(key, value),
427 SerializeMap::Pretty(compound) => compound.serialize_entry(key, value),
428 }
429 .map_err(Into::into)
430 }
431}
432
433#[cfg(feature = "json")]
434pub enum SerializeStruct<'a, W: io::Write> {
435 Compact(<&'a mut CompactJsonSerializer<W> as Serializer>::SerializeStruct),
436 Pretty(<&'a mut PrettyJsonSerializer<W> as Serializer>::SerializeStruct),
437}
438
439#[cfg(feature = "json")]
440impl<'a, W: io::Write> serde::ser::SerializeStruct for SerializeStruct<'a, W> {
441 type Ok = ();
442 type Error = FormatError;
443
444 fn serialize_field<T: ?Sized>(
445 &mut self,
446 key: &'static str,
447 value: &T,
448 ) -> Result<(), Self::Error>
449 where
450 T: Serialize,
451 {
452 match self {
453 SerializeStruct::Compact(compound) => compound.serialize_field(key, value),
454 SerializeStruct::Pretty(compound) => compound.serialize_field(key, value),
455 }
456 .map_err(Into::into)
457 }
458
459 fn end(self) -> Result<Self::Ok, Self::Error> {
460 match self {
461 SerializeStruct::Compact(compound) => compound.end(),
462 SerializeStruct::Pretty(compound) => compound.end(),
463 }
464 .map_err(Into::into)
465 }
466
467 fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
468 match self {
469 SerializeStruct::Compact(compound) => compound.skip_field(key),
470 SerializeStruct::Pretty(compound) => compound.skip_field(key),
471 }
472 .map_err(Into::into)
473 }
474}
475
476#[cfg(feature = "json")]
477pub enum SerializeStructVariant<'a, W: io::Write> {
478 Compact(<&'a mut CompactJsonSerializer<W> as Serializer>::SerializeStructVariant),
479 Pretty(<&'a mut PrettyJsonSerializer<W> as Serializer>::SerializeStructVariant),
480}
481
482#[cfg(feature = "json")]
483impl<'a, W: io::Write> serde::ser::SerializeStructVariant for SerializeStructVariant<'a, W> {
484 type Ok = ();
485 type Error = FormatError;
486
487 fn serialize_field<T: ?Sized>(
488 &mut self,
489 key: &'static str,
490 value: &T,
491 ) -> Result<(), Self::Error>
492 where
493 T: Serialize,
494 {
495 match self {
496 SerializeStructVariant::Compact(compound) => compound.serialize_field(key, value),
497 SerializeStructVariant::Pretty(compound) => compound.serialize_field(key, value),
498 }
499 .map_err(Into::into)
500 }
501
502 fn end(self) -> Result<Self::Ok, Self::Error> {
503 match self {
504 SerializeStructVariant::Compact(compound) => compound.end(),
505 SerializeStructVariant::Pretty(compound) => compound.end(),
506 }
507 .map_err(Into::into)
508 }
509
510 fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
511 match self {
512 SerializeStructVariant::Compact(compound) => compound.skip_field(key),
513 SerializeStructVariant::Pretty(compound) => compound.skip_field(key),
514 }
515 .map_err(Into::into)
516 }
517}
518
519impl<'a, W> Serializer for &'a mut Formatter<W>
520where
521 W: io::Write,
522{
523 type Ok = ();
524 type Error = FormatError;
525
526 #[cfg(feature = "json")]
527 type SerializeSeq = SerializeSeq<'a, W>;
528 #[cfg(feature = "json")]
529 type SerializeTuple = SerializeTuple<'a, W>;
530 #[cfg(feature = "json")]
531 type SerializeTupleStruct = SerializeTupleStruct<'a, W>;
532 #[cfg(feature = "json")]
533 type SerializeTupleVariant = SerializeTupleVariant<'a, W>;
534 #[cfg(feature = "json")]
535 type SerializeMap = SerializeMap<'a, W>;
536 #[cfg(feature = "json")]
537 type SerializeStruct = SerializeStruct<'a, W>;
538 #[cfg(feature = "json")]
539 type SerializeStructVariant = SerializeStructVariant<'a, W>;
540
541 #[cfg(not(feature = "json"))]
542 type SerializeSeq = serde::ser::Impossible<Self::Ok, Self::Error>;
543 #[cfg(not(feature = "json"))]
544 type SerializeTuple = serde::ser::Impossible<Self::Ok, Self::Error>;
545 #[cfg(not(feature = "json"))]
546 type SerializeTupleStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
547 #[cfg(not(feature = "json"))]
548 type SerializeTupleVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
549 #[cfg(not(feature = "json"))]
550 type SerializeMap = serde::ser::Impossible<Self::Ok, Self::Error>;
551 #[cfg(not(feature = "json"))]
552 type SerializeStruct = serde::ser::Impossible<Self::Ok, Self::Error>;
553 #[cfg(not(feature = "json"))]
554 type SerializeStructVariant = serde::ser::Impossible<Self::Ok, Self::Error>;
555
556 fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
557 match self.ty {
558 FormatType::Display => self.display(&v),
559 FormatType::Object => self.serialize(&v),
560 other => Err(FormatError::Type(other)),
561 }
562 }
563
564 fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
565 match self.ty {
566 FormatType::Display => self.display(&v),
567 FormatType::Object => self.serialize(&v),
568 FormatType::Octal => self.octal(&v),
569 FormatType::LowerHex => self.lower_hex(&v),
570 FormatType::UpperHex => self.upper_hex(&v),
571 FormatType::Binary => self.binary(&v),
572 other => Err(FormatError::Type(other)),
573 }
574 }
575
576 fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
577 match self.ty {
578 FormatType::Display => self.display(&v),
579 FormatType::Object => self.serialize(&v),
580 FormatType::Octal => self.octal(&v),
581 FormatType::LowerHex => self.lower_hex(&v),
582 FormatType::UpperHex => self.upper_hex(&v),
583 FormatType::Binary => self.binary(&v),
584 other => Err(FormatError::Type(other)),
585 }
586 }
587
588 fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
589 match self.ty {
590 FormatType::Display => self.display(&v),
591 FormatType::Object => self.serialize(&v),
592 FormatType::Octal => self.octal(&v),
593 FormatType::LowerHex => self.lower_hex(&v),
594 FormatType::UpperHex => self.upper_hex(&v),
595 FormatType::Binary => self.binary(&v),
596 other => Err(FormatError::Type(other)),
597 }
598 }
599
600 fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
601 match self.ty {
602 FormatType::Display => self.display(&v),
603 FormatType::Object => self.serialize(&v),
604 FormatType::Octal => self.octal(&v),
605 FormatType::LowerHex => self.lower_hex(&v),
606 FormatType::UpperHex => self.upper_hex(&v),
607 FormatType::Binary => self.binary(&v),
608 other => Err(FormatError::Type(other)),
609 }
610 }
611
612 fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
613 match self.ty {
614 FormatType::Display => self.display(&v),
615 FormatType::Object => self.serialize(&v),
616 FormatType::Octal => self.octal(&v),
617 FormatType::LowerHex => self.lower_hex(&v),
618 FormatType::UpperHex => self.upper_hex(&v),
619 FormatType::Binary => self.binary(&v),
620 other => Err(FormatError::Type(other)),
621 }
622 }
623
624 fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
625 match self.ty {
626 FormatType::Display => self.display(&v),
627 FormatType::Object => self.serialize(&v),
628 FormatType::Octal => self.octal(&v),
629 FormatType::LowerHex => self.lower_hex(&v),
630 FormatType::UpperHex => self.upper_hex(&v),
631 FormatType::Binary => self.binary(&v),
632 other => Err(FormatError::Type(other)),
633 }
634 }
635
636 fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
637 match self.ty {
638 FormatType::Display => self.display(&v),
639 FormatType::Object => self.serialize(&v),
640 FormatType::Octal => self.octal(&v),
641 FormatType::LowerHex => self.lower_hex(&v),
642 FormatType::UpperHex => self.upper_hex(&v),
643 FormatType::Binary => self.binary(&v),
644 other => Err(FormatError::Type(other)),
645 }
646 }
647
648 fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
649 match self.ty {
650 FormatType::Display => self.display(&v),
651 FormatType::Object => self.serialize(&v),
652 FormatType::Octal => self.octal(&v),
653 FormatType::LowerHex => self.lower_hex(&v),
654 FormatType::UpperHex => self.upper_hex(&v),
655 FormatType::Binary => self.binary(&v),
656 other => Err(FormatError::Type(other)),
657 }
658 }
659
660 fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
661 match self.ty {
662 FormatType::Display => self.display(&v),
663 FormatType::Object => self.serialize(&v),
664 FormatType::LowerExp => self.lower_exp(&v),
665 FormatType::UpperExp => self.upper_exp(&v),
666 other => Err(FormatError::Type(other)),
667 }
668 }
669
670 fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
671 match self.ty {
672 FormatType::Display => self.display(&v),
673 FormatType::Object => self.serialize(&v),
674 FormatType::LowerExp => self.lower_exp(&v),
675 FormatType::UpperExp => self.upper_exp(&v),
676 other => Err(FormatError::Type(other)),
677 }
678 }
679
680 fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
681 match self.ty {
682 FormatType::Display => self.display(&v),
683 FormatType::Object => self.serialize(&v),
684 other => Err(FormatError::Type(other)),
685 }
686 }
687
688 fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
689 match self.ty {
690 FormatType::Display => self.display(&v),
691 FormatType::Object => self.serialize(&v),
692 FormatType::Pointer => self.pointer(&v),
693 other => Err(FormatError::Type(other)),
694 }
695 }
696
697 fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
698 match self.ty {
699 FormatType::Object => self.serialize(&v),
700 FormatType::Pointer => self.pointer(&v),
701 other => Err(FormatError::Type(other)),
702 }
703 }
704
705 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
706 self.serialize_unit()
707 }
708
709 fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
710 where
711 T: Serialize,
712 {
713 value.serialize(self)
714 }
715
716 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
717 match self.ty {
718 FormatType::Display => self.display(&"null"),
719 FormatType::Object => self.serialize(&()),
720 other => Err(FormatError::Type(other)),
721 }
722 }
723
724 fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
725 self.serialize_unit()
726 }
727
728 fn serialize_unit_variant(
729 self,
730 _name: &'static str,
731 _variant_index: u32,
732 variant: &'static str,
733 ) -> Result<Self::Ok, Self::Error> {
734 self.serialize_str(variant)
735 }
736
737 fn serialize_newtype_struct<T: ?Sized>(
738 self,
739 _name: &'static str,
740 value: &T,
741 ) -> Result<Self::Ok, Self::Error>
742 where
743 T: Serialize,
744 {
745 value.serialize(self)
746 }
747
748 fn serialize_newtype_variant<T: ?Sized>(
749 self,
750 _name: &'static str,
751 _variant_index: u32,
752 _variant: &'static str,
753 _value: &T,
754 ) -> Result<Self::Ok, Self::Error>
755 where
756 T: Serialize,
757 {
758 Err(FormatError::Type(self.ty))
759 }
760
761 #[cfg(feature = "json")]
762 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
763 if self.ty != FormatType::Object && self.ty != FormatType::Display {
764 return Err(FormatError::Type(self.ty));
765 }
766
767 if self.alternate {
768 self.target
769 .as_pretty()
770 .serialize_seq(len)
771 .map(SerializeSeq::Pretty)
772 .map_err(Into::into)
773 } else {
774 self.target
775 .as_compact()
776 .serialize_seq(len)
777 .map(SerializeSeq::Compact)
778 .map_err(Into::into)
779 }
780 }
781
782 #[cfg(feature = "json")]
783 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
784 if self.ty != FormatType::Object && self.ty != FormatType::Display {
785 return Err(FormatError::Type(self.ty));
786 }
787
788 if self.alternate {
789 self.target
790 .as_pretty()
791 .serialize_tuple(len)
792 .map(SerializeTuple::Pretty)
793 .map_err(Into::into)
794 } else {
795 self.target
796 .as_compact()
797 .serialize_tuple(len)
798 .map(SerializeTuple::Compact)
799 .map_err(Into::into)
800 }
801 }
802
803 #[cfg(feature = "json")]
804 fn serialize_tuple_struct(
805 self,
806 name: &'static str,
807 len: usize,
808 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
809 if self.ty != FormatType::Object && self.ty != FormatType::Display {
810 return Err(FormatError::Type(self.ty));
811 }
812
813 if self.alternate {
814 self.target
815 .as_pretty()
816 .serialize_tuple_struct(name, len)
817 .map(SerializeTupleStruct::Pretty)
818 .map_err(Into::into)
819 } else {
820 self.target
821 .as_compact()
822 .serialize_tuple_struct(name, len)
823 .map(SerializeTupleStruct::Compact)
824 .map_err(Into::into)
825 }
826 }
827
828 #[cfg(feature = "json")]
829 fn serialize_tuple_variant(
830 self,
831 name: &'static str,
832 variant_index: u32,
833 variant: &'static str,
834 len: usize,
835 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
836 if self.ty != FormatType::Object && self.ty != FormatType::Display {
837 return Err(FormatError::Type(self.ty));
838 }
839
840 if self.alternate {
841 self.target
842 .as_pretty()
843 .serialize_tuple_variant(name, variant_index, variant, len)
844 .map(SerializeTupleVariant::Pretty)
845 .map_err(Into::into)
846 } else {
847 self.target
848 .as_compact()
849 .serialize_tuple_variant(name, variant_index, variant, len)
850 .map(SerializeTupleVariant::Compact)
851 .map_err(Into::into)
852 }
853 }
854
855 #[cfg(feature = "json")]
856 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
857 if self.ty != FormatType::Object && self.ty != FormatType::Display {
858 return Err(FormatError::Type(self.ty));
859 }
860
861 if self.alternate {
862 self.target
863 .as_pretty()
864 .serialize_map(len)
865 .map(SerializeMap::Pretty)
866 .map_err(Into::into)
867 } else {
868 self.target
869 .as_compact()
870 .serialize_map(len)
871 .map(SerializeMap::Compact)
872 .map_err(Into::into)
873 }
874 }
875
876 #[cfg(feature = "json")]
877 fn serialize_struct(
878 self,
879 name: &'static str,
880 len: usize,
881 ) -> Result<Self::SerializeStruct, Self::Error> {
882 if self.ty != FormatType::Object && self.ty != FormatType::Display {
883 return Err(FormatError::Type(self.ty));
884 }
885
886 if self.alternate {
887 self.target
888 .as_pretty()
889 .serialize_struct(name, len)
890 .map(SerializeStruct::Pretty)
891 .map_err(Into::into)
892 } else {
893 self.target
894 .as_compact()
895 .serialize_struct(name, len)
896 .map(SerializeStruct::Compact)
897 .map_err(Into::into)
898 }
899 }
900
901 #[cfg(feature = "json")]
902 fn serialize_struct_variant(
903 self,
904 name: &'static str,
905 variant_index: u32,
906 variant: &'static str,
907 len: usize,
908 ) -> Result<Self::SerializeStructVariant, Self::Error> {
909 if self.ty != FormatType::Object && self.ty != FormatType::Display {
910 return Err(FormatError::Type(self.ty));
911 }
912
913 if self.alternate {
914 self.target
915 .as_pretty()
916 .serialize_struct_variant(name, variant_index, variant, len)
917 .map(SerializeStructVariant::Pretty)
918 .map_err(Into::into)
919 } else {
920 self.target
921 .as_compact()
922 .serialize_struct_variant(name, variant_index, variant, len)
923 .map(SerializeStructVariant::Compact)
924 .map_err(Into::into)
925 }
926 }
927
928 #[cfg(not(feature = "json"))]
929 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
930 Err(FormatError::Type(self.ty))
931 }
932
933 #[cfg(not(feature = "json"))]
934 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
935 Err(FormatError::Type(self.ty))
936 }
937
938 #[cfg(not(feature = "json"))]
939 fn serialize_tuple_struct(
940 self,
941 _name: &'static str,
942 _len: usize,
943 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
944 Err(FormatError::Type(self.ty))
945 }
946
947 #[cfg(not(feature = "json"))]
948 fn serialize_tuple_variant(
949 self,
950 _name: &'static str,
951 _variant_index: u32,
952 _variant: &'static str,
953 _len: usize,
954 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
955 Err(FormatError::Type(self.ty))
956 }
957
958 #[cfg(not(feature = "json"))]
959 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
960 Err(FormatError::Type(self.ty))
961 }
962
963 #[cfg(not(feature = "json"))]
964 fn serialize_struct(
965 self,
966 _name: &'static str,
967 _len: usize,
968 ) -> Result<Self::SerializeStruct, Self::Error> {
969 Err(FormatError::Type(self.ty))
970 }
971
972 #[cfg(not(feature = "json"))]
973 fn serialize_struct_variant(
974 self,
975 _name: &'static str,
976 _variant_index: u32,
977 _variant: &'static str,
978 _len: usize,
979 ) -> Result<Self::SerializeStructVariant, Self::Error> {
980 Err(FormatError::Type(self.ty))
981 }
982}