1use crate::lib::*;
2use crate::ser::{Error, Impossible, Serialize, Serializer};
3
4impl Error for fmt::Error {
5 fn custom<T: Display>(_msg: T) -> Self {
6 fmt::Error
7 }
8}
9
10macro_rules! fmt_primitives {
11 ($($f:ident: $t:ty,)*) => {
12 $(
13 fn $f(self, v: $t) -> fmt::Result {
14 Display::fmt(&v, self)
15 }
16 )*
17 };
18}
19
20/// ```edition2021
21/// use serde::ser::Serialize;
22/// use serde_derive::Serialize;
23/// use std::fmt::{self, Display};
24///
25/// #[derive(Serialize)]
26/// #[serde(rename_all = "kebab-case")]
27/// pub enum MessageType {
28/// StartRequest,
29/// EndRequest,
30/// }
31///
32/// impl Display for MessageType {
33/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34/// self.serialize(f)
35/// }
36/// }
37/// ```
38impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> {
39 type Ok = ();
40 type Error = fmt::Error;
41 type SerializeSeq = Impossible<(), fmt::Error>;
42 type SerializeTuple = Impossible<(), fmt::Error>;
43 type SerializeTupleStruct = Impossible<(), fmt::Error>;
44 type SerializeTupleVariant = Impossible<(), fmt::Error>;
45 type SerializeMap = Impossible<(), fmt::Error>;
46 type SerializeStruct = Impossible<(), fmt::Error>;
47 type SerializeStructVariant = Impossible<(), fmt::Error>;
48
49 fmt_primitives! {
50 serialize_bool: bool,
51 serialize_i8: i8,
52 serialize_i16: i16,
53 serialize_i32: i32,
54 serialize_i64: i64,
55 serialize_u8: u8,
56 serialize_u16: u16,
57 serialize_u32: u32,
58 serialize_u64: u64,
59 serialize_f32: f32,
60 serialize_f64: f64,
61 serialize_char: char,
62 serialize_str: &str,
63 serialize_unit_struct: &'static str,
64 }
65
66 serde_if_integer128! {
67 fmt_primitives! {
68 serialize_i128: i128,
69 serialize_u128: u128,
70 }
71 }
72
73 fn serialize_unit_variant(
74 self,
75 _name: &'static str,
76 _variant_index: u32,
77 variant: &'static str,
78 ) -> fmt::Result {
79 Display::fmt(variant, self)
80 }
81
82 fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> fmt::Result
83 where
84 T: Serialize,
85 {
86 Serialize::serialize(value, self)
87 }
88
89 fn serialize_bytes(self, _v: &[u8]) -> fmt::Result {
90 Err(fmt::Error)
91 }
92
93 fn serialize_none(self) -> fmt::Result {
94 Err(fmt::Error)
95 }
96
97 fn serialize_some<T: ?Sized>(self, _value: &T) -> fmt::Result
98 where
99 T: Serialize,
100 {
101 Err(fmt::Error)
102 }
103
104 fn serialize_unit(self) -> fmt::Result {
105 Err(fmt::Error)
106 }
107
108 fn serialize_newtype_variant<T: ?Sized>(
109 self,
110 _name: &'static str,
111 _variant_index: u32,
112 _variant: &'static str,
113 _value: &T,
114 ) -> fmt::Result
115 where
116 T: Serialize,
117 {
118 Err(fmt::Error)
119 }
120
121 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, fmt::Error> {
122 Err(fmt::Error)
123 }
124
125 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, fmt::Error> {
126 Err(fmt::Error)
127 }
128
129 fn serialize_tuple_struct(
130 self,
131 _name: &'static str,
132 _len: usize,
133 ) -> Result<Self::SerializeTupleStruct, fmt::Error> {
134 Err(fmt::Error)
135 }
136
137 fn serialize_tuple_variant(
138 self,
139 _name: &'static str,
140 _variant_index: u32,
141 _variant: &'static str,
142 _len: usize,
143 ) -> Result<Self::SerializeTupleVariant, fmt::Error> {
144 Err(fmt::Error)
145 }
146
147 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, fmt::Error> {
148 Err(fmt::Error)
149 }
150
151 fn serialize_struct(
152 self,
153 _name: &'static str,
154 _len: usize,
155 ) -> Result<Self::SerializeStruct, fmt::Error> {
156 Err(fmt::Error)
157 }
158
159 fn serialize_struct_variant(
160 self,
161 _name: &'static str,
162 _variant_index: u32,
163 _variant: &'static str,
164 _len: usize,
165 ) -> Result<Self::SerializeStructVariant, fmt::Error> {
166 Err(fmt::Error)
167 }
168
169 fn collect_str<T: ?Sized>(self, value: &T) -> fmt::Result
170 where
171 T: Display,
172 {
173 Display::fmt(value, self)
174 }
175}
176