/rust/registry/src/index.crates.io-1949cf8c6b5b557f/serde_fmt-1.1.0/src/lib.rs
Line | Count | Source |
1 | | /*! |
2 | | # `serde` -> `std::fmt` |
3 | | |
4 | | This library lets you take any `Serialize` and format it as if it's `Debug`. |
5 | | The format produced is the same as if the type derived `Debug`, and any |
6 | | formatting flags will be preserved. |
7 | | |
8 | | # Getting started |
9 | | |
10 | | Add `serde_fmt` to your `Cargo.toml`: |
11 | | |
12 | | ```toml,ignore |
13 | | [dependencies.serde_fmt] |
14 | | version = "1.1.0" |
15 | | ``` |
16 | | |
17 | | By default, this library doesn't depend on the standard library. |
18 | | You can enable support with the `std` Cargo feature: |
19 | | |
20 | | ```toml,ignore |
21 | | [dependencies.serde_fmt] |
22 | | version = "1.1.0" |
23 | | features = ["std"] |
24 | | ``` |
25 | | |
26 | | # Formatting a `Serialize` |
27 | | |
28 | | Use the [`to_debug`] function to treat a [`serde_core::Serialize`] like a [`std::fmt::Debug`]: |
29 | | |
30 | | ```rust |
31 | | # use serde_core::Serialize; |
32 | | fn takes_serialize(v: impl Serialize) { |
33 | | // You can dump any `Serialize` using the |
34 | | // standard `dbg!` macro |
35 | | dbg!(serde_fmt::to_debug(&v)); |
36 | | |
37 | | // do something with `v` |
38 | | } |
39 | | ``` |
40 | | */ |
41 | | |
42 | | #![doc(html_root_url = "https://docs.rs/serde_fmt/1.1.0")] |
43 | | #![cfg_attr(not(test), no_std)] |
44 | | |
45 | | #[cfg(all(not(test), not(feature = "std")))] |
46 | | extern crate core as std; |
47 | | |
48 | | #[cfg(any(test, feature = "std"))] |
49 | | extern crate std; |
50 | | |
51 | | use crate::std::fmt::{self, Debug, Display}; |
52 | | |
53 | | use serde_core::ser::{ |
54 | | self, Serialize, SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, |
55 | | SerializeTuple, SerializeTupleStruct, SerializeTupleVariant, Serializer, |
56 | | }; |
57 | | |
58 | | /** |
59 | | Format a [`serde_core::Serialize`] into a [`std::fmt::Write`]. |
60 | | */ |
61 | 0 | pub fn to_writer(v: impl Serialize, mut w: impl fmt::Write) -> fmt::Result { |
62 | 0 | w.write_fmt(format_args!("{:?}", to_debug(v))) |
63 | 0 | } |
64 | | |
65 | | /** |
66 | | Treat a type implementing [`serde_core::Serialize`] like a type implementing [`std::fmt::Debug`]. |
67 | | */ |
68 | 0 | pub fn to_debug<T>(v: T) -> ToDebug<T> |
69 | 0 | where |
70 | 0 | T: Serialize, |
71 | | { |
72 | 0 | ToDebug(v) |
73 | 0 | } Unexecuted instantiation: serde_fmt::to_debug::<&dyn erased_serde::ser::Serialize> Unexecuted instantiation: serde_fmt::to_debug::<_> |
74 | | |
75 | | /** |
76 | | The result of calling [`to_debug`] . |
77 | | */ |
78 | | #[derive(Clone, Copy)] |
79 | | pub struct ToDebug<T>(T); |
80 | | |
81 | | impl<T> Debug for ToDebug<T> |
82 | | where |
83 | | T: Serialize, |
84 | | { |
85 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
86 | 0 | fmt::Display::fmt(self, f) |
87 | 0 | } Unexecuted instantiation: <serde_fmt::ToDebug<&dyn erased_serde::ser::Serialize> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde_fmt::ToDebug<_> as core::fmt::Debug>::fmt |
88 | | } |
89 | | |
90 | | // Even though it's not specified, since we treat `fmt::Debug` |
91 | | // as the canonical format for `ToDebug` we can also think of |
92 | | // it as the human-readable `Display`able format in the same |
93 | | // way that `fmt::Arguments` does. |
94 | | impl<T> Display for ToDebug<T> |
95 | | where |
96 | | T: Serialize, |
97 | | { |
98 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
99 | | // If the `Serialize` impl fails then swallow the error rather than |
100 | | // propagate it; Traits like `ToString` expect formatting to be |
101 | | // infallible unless the writer itself fails |
102 | 0 | match self.0.serialize(Formatter::new(f)) { |
103 | 0 | Ok(()) => Ok(()), |
104 | 0 | Err(e) => write!(f, "<{}>", e), |
105 | | } |
106 | 0 | } Unexecuted instantiation: <serde_fmt::ToDebug<&dyn erased_serde::ser::Serialize> as core::fmt::Display>::fmt Unexecuted instantiation: <serde_fmt::ToDebug<_> as core::fmt::Display>::fmt |
107 | | } |
108 | | |
109 | | // Surface the original `Serialize` implementation. |
110 | | impl<T> Serialize for ToDebug<T> |
111 | | where |
112 | | T: Serialize, |
113 | | { |
114 | 0 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
115 | 0 | where |
116 | 0 | S: Serializer, |
117 | | { |
118 | 0 | self.0.serialize(serializer) |
119 | 0 | } |
120 | | } |
121 | | |
122 | | struct Formatter<'a, 'b: 'a>(&'a mut fmt::Formatter<'b>); |
123 | | |
124 | | impl<'a, 'b: 'a> Formatter<'a, 'b> { |
125 | 0 | fn new(fmt: &'a mut fmt::Formatter<'b>) -> Self { |
126 | 0 | Formatter(fmt) |
127 | 0 | } |
128 | | |
129 | 0 | fn fmt(self, v: impl Debug) -> Result<(), Error> { |
130 | 0 | v.fmt(self.0).map_err(Into::into) |
131 | 0 | } Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<&[u8]> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<&str> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<i8> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<bool> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<char> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<f64> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<f32> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<u8> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<i32> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<u32> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<i128> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<u128> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<i16> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<u16> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<i64> Unexecuted instantiation: <serde_fmt::Formatter>::fmt::<u64> |
132 | | } |
133 | | |
134 | | impl<'a, 'b: 'a> Serializer for Formatter<'a, 'b> { |
135 | | type Ok = (); |
136 | | type Error = Error; |
137 | | |
138 | | type SerializeSeq = DebugSeq<'a, 'b>; |
139 | | type SerializeTuple = DebugTuple<'a, 'b>; |
140 | | type SerializeTupleStruct = DebugTupleStruct<'a, 'b>; |
141 | | type SerializeTupleVariant = DebugTupleVariant<'a, 'b>; |
142 | | type SerializeMap = DebugMap<'a, 'b>; |
143 | | type SerializeStruct = DebugStruct<'a, 'b>; |
144 | | type SerializeStructVariant = DebugStructVariant<'a, 'b>; |
145 | | |
146 | 0 | fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> { |
147 | 0 | self.fmt(v) |
148 | 0 | } |
149 | | |
150 | 0 | fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> { |
151 | 0 | self.fmt(v) |
152 | 0 | } |
153 | | |
154 | 0 | fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> { |
155 | 0 | self.fmt(v) |
156 | 0 | } |
157 | | |
158 | 0 | fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> { |
159 | 0 | self.fmt(v) |
160 | 0 | } |
161 | | |
162 | 0 | fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> { |
163 | 0 | self.fmt(v) |
164 | 0 | } |
165 | | |
166 | 0 | fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> { |
167 | 0 | self.fmt(v) |
168 | 0 | } |
169 | | |
170 | 0 | fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> { |
171 | 0 | self.fmt(v) |
172 | 0 | } |
173 | | |
174 | 0 | fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> { |
175 | 0 | self.fmt(v) |
176 | 0 | } |
177 | | |
178 | 0 | fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> { |
179 | 0 | self.fmt(v) |
180 | 0 | } |
181 | | |
182 | 0 | fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> { |
183 | 0 | self.fmt(v) |
184 | 0 | } |
185 | | |
186 | 0 | fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> { |
187 | 0 | self.fmt(v) |
188 | 0 | } |
189 | | |
190 | 0 | fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> { |
191 | 0 | self.fmt(v) |
192 | 0 | } |
193 | | |
194 | 0 | fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> { |
195 | 0 | self.fmt(v) |
196 | 0 | } |
197 | | |
198 | 0 | fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> { |
199 | 0 | self.fmt(v) |
200 | 0 | } |
201 | | |
202 | 0 | fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> { |
203 | 0 | self.fmt(v) |
204 | 0 | } |
205 | | |
206 | 0 | fn collect_str<T: ?Sized>(self, v: &T) -> Result<Self::Ok, Self::Error> |
207 | 0 | where |
208 | 0 | T: Display, |
209 | | { |
210 | 0 | self.fmt(format_args!("{}", v)) |
211 | 0 | } |
212 | | |
213 | 0 | fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> { |
214 | 0 | self.fmt(v) |
215 | 0 | } |
216 | | |
217 | 0 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
218 | 0 | write!(self.0, "None")?; |
219 | 0 | Ok(()) |
220 | 0 | } |
221 | | |
222 | 0 | fn serialize_some<T>(self, v: &T) -> Result<Self::Ok, Self::Error> |
223 | 0 | where |
224 | 0 | T: ?Sized + Serialize, |
225 | | { |
226 | 0 | self.serialize_newtype_struct("Some", v) |
227 | 0 | } Unexecuted instantiation: <serde_fmt::Formatter as serde_core::ser::Serializer>::serialize_some::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::Formatter as serde_core::ser::Serializer>::serialize_some::<_> |
228 | | |
229 | 0 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
230 | 0 | write!(self.0, "()")?; |
231 | 0 | Ok(()) |
232 | 0 | } |
233 | | |
234 | 0 | fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> { |
235 | 0 | self.serialize_tuple_struct(name, 0)?.end() |
236 | 0 | } |
237 | | |
238 | 0 | fn serialize_unit_variant( |
239 | 0 | self, |
240 | 0 | _name: &'static str, |
241 | 0 | _variant_index: u32, |
242 | 0 | variant: &'static str, |
243 | 0 | ) -> Result<Self::Ok, Self::Error> { |
244 | 0 | self.serialize_tuple_struct(variant, 0)?.end() |
245 | 0 | } |
246 | | |
247 | 0 | fn serialize_newtype_struct<T>(self, name: &'static str, v: &T) -> Result<Self::Ok, Self::Error> |
248 | 0 | where |
249 | 0 | T: ?Sized + Serialize, |
250 | | { |
251 | 0 | let mut tuple = self.serialize_tuple_struct(name, 1)?; |
252 | 0 | tuple.serialize_field(v)?; |
253 | 0 | tuple.end() |
254 | 0 | } Unexecuted instantiation: <serde_fmt::Formatter as serde_core::ser::Serializer>::serialize_newtype_struct::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::Formatter as serde_core::ser::Serializer>::serialize_newtype_struct::<_> |
255 | | |
256 | 0 | fn serialize_newtype_variant<T>( |
257 | 0 | self, |
258 | 0 | _name: &'static str, |
259 | 0 | _variant_index: u32, |
260 | 0 | variant: &'static str, |
261 | 0 | v: &T, |
262 | 0 | ) -> Result<Self::Ok, Self::Error> |
263 | 0 | where |
264 | 0 | T: ?Sized + Serialize, |
265 | | { |
266 | 0 | let mut tuple = self.serialize_tuple_struct(variant, 1)?; |
267 | 0 | tuple.serialize_field(v)?; |
268 | 0 | tuple.end() |
269 | 0 | } Unexecuted instantiation: <serde_fmt::Formatter as serde_core::ser::Serializer>::serialize_newtype_variant::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::Formatter as serde_core::ser::Serializer>::serialize_newtype_variant::<_> |
270 | | |
271 | 0 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
272 | 0 | Ok(DebugSeq(self.0.debug_list())) |
273 | 0 | } |
274 | | |
275 | 0 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> { |
276 | 0 | Ok(DebugTuple(self.0.debug_tuple(""))) |
277 | 0 | } |
278 | | |
279 | 0 | fn serialize_tuple_struct( |
280 | 0 | self, |
281 | 0 | name: &'static str, |
282 | 0 | _len: usize, |
283 | 0 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
284 | 0 | Ok(DebugTupleStruct(self.0.debug_tuple(name))) |
285 | 0 | } |
286 | | |
287 | 0 | fn serialize_tuple_variant( |
288 | 0 | self, |
289 | 0 | _name: &'static str, |
290 | 0 | _variant_index: u32, |
291 | 0 | variant: &'static str, |
292 | 0 | _len: usize, |
293 | 0 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
294 | 0 | Ok(DebugTupleVariant(self.0.debug_tuple(variant))) |
295 | 0 | } |
296 | | |
297 | 0 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
298 | 0 | Ok(DebugMap(self.0.debug_map())) |
299 | 0 | } |
300 | | |
301 | 0 | fn serialize_struct( |
302 | 0 | self, |
303 | 0 | name: &'static str, |
304 | 0 | _len: usize, |
305 | 0 | ) -> Result<Self::SerializeStruct, Self::Error> { |
306 | 0 | Ok(DebugStruct(self.0.debug_struct(name))) |
307 | 0 | } |
308 | | |
309 | 0 | fn serialize_struct_variant( |
310 | 0 | self, |
311 | 0 | _name: &'static str, |
312 | 0 | _variant_index: u32, |
313 | 0 | variant: &'static str, |
314 | 0 | _len: usize, |
315 | 0 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
316 | 0 | Ok(DebugStructVariant(self.0.debug_struct(variant))) |
317 | 0 | } |
318 | | } |
319 | | |
320 | | struct DebugSeq<'a, 'b: 'a>(fmt::DebugList<'a, 'b>); |
321 | | |
322 | | impl<'a, 'b: 'a> SerializeSeq for DebugSeq<'a, 'b> { |
323 | | type Ok = (); |
324 | | type Error = Error; |
325 | | |
326 | 0 | fn serialize_element<T>(&mut self, v: &T) -> Result<Self::Ok, Self::Error> |
327 | 0 | where |
328 | 0 | T: ?Sized + Serialize, |
329 | | { |
330 | 0 | self.0.entry(&to_debug(v)); |
331 | 0 | Ok(()) |
332 | 0 | } Unexecuted instantiation: <serde_fmt::DebugSeq as serde_core::ser::SerializeSeq>::serialize_element::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugSeq as serde_core::ser::SerializeSeq>::serialize_element::<_> |
333 | | |
334 | 0 | fn end(mut self) -> Result<Self::Ok, Self::Error> { |
335 | 0 | self.0.finish().map_err(Into::into) |
336 | 0 | } |
337 | | } |
338 | | |
339 | | struct DebugTuple<'a, 'b: 'a>(fmt::DebugTuple<'a, 'b>); |
340 | | |
341 | | impl<'a, 'b: 'a> SerializeTuple for DebugTuple<'a, 'b> { |
342 | | type Ok = (); |
343 | | type Error = Error; |
344 | | |
345 | 0 | fn serialize_element<T>(&mut self, v: &T) -> Result<Self::Ok, Self::Error> |
346 | 0 | where |
347 | 0 | T: ?Sized + Serialize, |
348 | | { |
349 | 0 | self.0.field(&to_debug(v)); |
350 | 0 | Ok(()) |
351 | 0 | } Unexecuted instantiation: <serde_fmt::DebugTuple as serde_core::ser::SerializeTuple>::serialize_element::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugTuple as serde_core::ser::SerializeTuple>::serialize_element::<_> |
352 | | |
353 | 0 | fn end(mut self) -> Result<Self::Ok, Self::Error> { |
354 | 0 | self.0.finish().map_err(Into::into) |
355 | 0 | } |
356 | | } |
357 | | |
358 | | struct DebugTupleStruct<'a, 'b: 'a>(fmt::DebugTuple<'a, 'b>); |
359 | | |
360 | | impl<'a, 'b: 'a> SerializeTupleStruct for DebugTupleStruct<'a, 'b> { |
361 | | type Ok = (); |
362 | | type Error = Error; |
363 | | |
364 | 0 | fn serialize_field<T>(&mut self, v: &T) -> Result<Self::Ok, Self::Error> |
365 | 0 | where |
366 | 0 | T: ?Sized + Serialize, |
367 | | { |
368 | 0 | self.0.field(&to_debug(v)); |
369 | 0 | Ok(()) |
370 | 0 | } Unexecuted instantiation: <serde_fmt::DebugTupleStruct as serde_core::ser::SerializeTupleStruct>::serialize_field::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugTupleStruct as serde_core::ser::SerializeTupleStruct>::serialize_field::<_> |
371 | | |
372 | 0 | fn end(mut self) -> Result<Self::Ok, Self::Error> { |
373 | 0 | self.0.finish().map_err(Into::into) |
374 | 0 | } |
375 | | } |
376 | | |
377 | | struct DebugTupleVariant<'a, 'b: 'a>(fmt::DebugTuple<'a, 'b>); |
378 | | |
379 | | impl<'a, 'b: 'a> SerializeTupleVariant for DebugTupleVariant<'a, 'b> { |
380 | | type Ok = (); |
381 | | type Error = Error; |
382 | | |
383 | 0 | fn serialize_field<T>(&mut self, v: &T) -> Result<Self::Ok, Self::Error> |
384 | 0 | where |
385 | 0 | T: ?Sized + Serialize, |
386 | | { |
387 | 0 | self.0.field(&to_debug(v)); |
388 | 0 | Ok(()) |
389 | 0 | } Unexecuted instantiation: <serde_fmt::DebugTupleVariant as serde_core::ser::SerializeTupleVariant>::serialize_field::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugTupleVariant as serde_core::ser::SerializeTupleVariant>::serialize_field::<_> |
390 | | |
391 | 0 | fn end(mut self) -> Result<Self::Ok, Self::Error> { |
392 | 0 | self.0.finish().map_err(Into::into) |
393 | 0 | } |
394 | | } |
395 | | |
396 | | struct DebugStruct<'a, 'b: 'a>(fmt::DebugStruct<'a, 'b>); |
397 | | |
398 | | impl<'a, 'b: 'a> SerializeStruct for DebugStruct<'a, 'b> { |
399 | | type Ok = (); |
400 | | type Error = Error; |
401 | | |
402 | 0 | fn serialize_field<T>(&mut self, k: &'static str, v: &T) -> Result<Self::Ok, Self::Error> |
403 | 0 | where |
404 | 0 | T: ?Sized + Serialize, |
405 | | { |
406 | 0 | self.0.field(k, &to_debug(v)); |
407 | 0 | Ok(()) |
408 | 0 | } Unexecuted instantiation: <serde_fmt::DebugStruct as serde_core::ser::SerializeStruct>::serialize_field::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugStruct as serde_core::ser::SerializeStruct>::serialize_field::<_> |
409 | | |
410 | 0 | fn end(mut self) -> Result<Self::Ok, Self::Error> { |
411 | 0 | self.0.finish().map_err(Into::into) |
412 | 0 | } |
413 | | } |
414 | | |
415 | | struct DebugStructVariant<'a, 'b: 'a>(fmt::DebugStruct<'a, 'b>); |
416 | | |
417 | | impl<'a, 'b: 'a> SerializeStructVariant for DebugStructVariant<'a, 'b> { |
418 | | type Ok = (); |
419 | | type Error = Error; |
420 | | |
421 | 0 | fn serialize_field<T>(&mut self, k: &'static str, v: &T) -> Result<Self::Ok, Self::Error> |
422 | 0 | where |
423 | 0 | T: ?Sized + Serialize, |
424 | | { |
425 | 0 | self.0.field(k, &to_debug(v)); |
426 | 0 | Ok(()) |
427 | 0 | } Unexecuted instantiation: <serde_fmt::DebugStructVariant as serde_core::ser::SerializeStructVariant>::serialize_field::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugStructVariant as serde_core::ser::SerializeStructVariant>::serialize_field::<_> |
428 | | |
429 | 0 | fn end(mut self) -> Result<Self::Ok, Self::Error> { |
430 | 0 | self.0.finish().map_err(Into::into) |
431 | 0 | } |
432 | | } |
433 | | |
434 | | struct DebugMap<'a, 'b: 'a>(fmt::DebugMap<'a, 'b>); |
435 | | |
436 | | impl<'a, 'b: 'a> SerializeMap for DebugMap<'a, 'b> { |
437 | | type Ok = (); |
438 | | type Error = Error; |
439 | | |
440 | 0 | fn serialize_entry<K, V>(&mut self, k: &K, v: &V) -> Result<Self::Ok, Self::Error> |
441 | 0 | where |
442 | 0 | K: ?Sized + Serialize, |
443 | 0 | V: ?Sized + Serialize, |
444 | | { |
445 | 0 | self.0.entry(&to_debug(k), &to_debug(v)); |
446 | 0 | Ok(()) |
447 | 0 | } Unexecuted instantiation: <serde_fmt::DebugMap as serde_core::ser::SerializeMap>::serialize_entry::<dyn erased_serde::ser::Serialize, dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugMap as serde_core::ser::SerializeMap>::serialize_entry::<_, _> |
448 | | |
449 | 0 | fn serialize_key<T>(&mut self, k: &T) -> Result<Self::Ok, Self::Error> |
450 | 0 | where |
451 | 0 | T: ?Sized + Serialize, |
452 | | { |
453 | 0 | self.0.key(&to_debug(k)); |
454 | 0 | Ok(()) |
455 | 0 | } Unexecuted instantiation: <serde_fmt::DebugMap as serde_core::ser::SerializeMap>::serialize_key::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugMap as serde_core::ser::SerializeMap>::serialize_key::<_> |
456 | | |
457 | 0 | fn serialize_value<T>(&mut self, v: &T) -> Result<Self::Ok, Self::Error> |
458 | 0 | where |
459 | 0 | T: ?Sized + Serialize, |
460 | | { |
461 | 0 | self.0.value(&to_debug(v)); |
462 | 0 | Ok(()) |
463 | 0 | } Unexecuted instantiation: <serde_fmt::DebugMap as serde_core::ser::SerializeMap>::serialize_value::<dyn erased_serde::ser::Serialize> Unexecuted instantiation: <serde_fmt::DebugMap as serde_core::ser::SerializeMap>::serialize_value::<_> |
464 | | |
465 | 0 | fn end(mut self) -> Result<Self::Ok, Self::Error> { |
466 | 0 | self.0.finish().map_err(Into::into) |
467 | 0 | } |
468 | | } |
469 | | |
470 | | #[derive(Debug)] |
471 | | struct Error; |
472 | | |
473 | | impl Display for Error { |
474 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
475 | 0 | write!(f, "failed to serialize to a standard formatter") |
476 | 0 | } |
477 | | } |
478 | | |
479 | | impl From<Error> for fmt::Error { |
480 | 0 | fn from(_: Error) -> fmt::Error { |
481 | 0 | fmt::Error |
482 | 0 | } |
483 | | } |
484 | | |
485 | | impl From<fmt::Error> for Error { |
486 | 0 | fn from(_: fmt::Error) -> Error { |
487 | 0 | Error |
488 | 0 | } |
489 | | } |
490 | | |
491 | | impl ser::StdError for Error {} |
492 | | |
493 | | impl ser::Error for Error { |
494 | 0 | fn custom<T>(_: T) -> Self |
495 | 0 | where |
496 | 0 | T: Display, |
497 | | { |
498 | 0 | Error |
499 | 0 | } Unexecuted instantiation: <serde_fmt::Error as serde_core::ser::Error>::custom::<alloc::boxed::Box<alloc::string::String>> Unexecuted instantiation: <serde_fmt::Error as serde_core::ser::Error>::custom::<_> |
500 | | } |
501 | | |
502 | | #[cfg(test)] |
503 | | extern crate serde_derive; |
504 | | |
505 | | #[cfg(test)] |
506 | | mod tests { |
507 | | use super::*; |
508 | | use serde_core::ser::Error as _; |
509 | | use serde_derive::*; |
510 | | |
511 | | fn check_fmt(v: impl fmt::Debug + Serialize) { |
512 | | assert_eq!(format!("{:?}", v), format!("{:?}", to_debug(v))); |
513 | | } |
514 | | |
515 | | #[test] |
516 | | fn failing_serialize_does_not_panic_to_string() { |
517 | | struct Kaboom; |
518 | | |
519 | | impl Serialize for Kaboom { |
520 | | fn serialize<S: Serializer>(&self, _: S) -> Result<S::Ok, S::Error> { |
521 | | Err(S::Error::custom("kaboom!")) |
522 | | } |
523 | | } |
524 | | |
525 | | #[derive(Serialize)] |
526 | | struct NestedKaboom { |
527 | | a: i32, |
528 | | b: Kaboom, |
529 | | c: i32, |
530 | | } |
531 | | |
532 | | assert_eq!("<failed to serialize to a standard formatter>", to_debug(Kaboom).to_string()); |
533 | | assert_eq!("NestedKaboom { a: 1, b: <failed to serialize to a standard formatter>, c: 2 }", to_debug(NestedKaboom { a: 1, b: Kaboom, c: 2 }).to_string()); |
534 | | } |
535 | | |
536 | | #[test] |
537 | | fn struct_fmt_is_consitent() { |
538 | | #[derive(Serialize, Debug)] |
539 | | struct Struct { |
540 | | a: Signed, |
541 | | b: Unsigned, |
542 | | c: char, |
543 | | d: &'static str, |
544 | | e: &'static [u8], |
545 | | f: (), |
546 | | } |
547 | | |
548 | | #[derive(Serialize, Debug)] |
549 | | struct Signed { |
550 | | a: i8, |
551 | | b: i16, |
552 | | c: i32, |
553 | | d: i64, |
554 | | } |
555 | | |
556 | | #[derive(Serialize, Debug)] |
557 | | struct Unsigned { |
558 | | a: u8, |
559 | | b: u16, |
560 | | c: u32, |
561 | | d: u64, |
562 | | } |
563 | | |
564 | | check_fmt(Struct { |
565 | | a: Signed { |
566 | | a: -1, |
567 | | b: 42, |
568 | | c: -42, |
569 | | d: 42, |
570 | | }, |
571 | | b: Unsigned { |
572 | | a: 1, |
573 | | b: 42, |
574 | | c: 1, |
575 | | d: 42, |
576 | | }, |
577 | | c: 'a', |
578 | | d: "a string", |
579 | | e: &[1, 2, 3], |
580 | | f: (), |
581 | | }); |
582 | | } |
583 | | |
584 | | #[test] |
585 | | fn fmt_flags_are_consistent() { |
586 | | use crate::std::format; |
587 | | |
588 | | #[derive(Serialize, Debug)] |
589 | | struct Struct { |
590 | | a: i32, |
591 | | b: i32, |
592 | | } |
593 | | |
594 | | assert_eq!(format!("{:03?}", 42), format!("{:03?}", to_debug(42))); |
595 | | assert_eq!(format!("{:x?}", 42), format!("{:x?}", to_debug(42))); |
596 | | assert_eq!(format!("{:X?}", 42), format!("{:X?}", to_debug(42))); |
597 | | assert_eq!( |
598 | | format!("{:#?}", Struct { a: 42, b: 17 }), |
599 | | format!("{:#?}", to_debug(Struct { a: 42, b: 17 })) |
600 | | ); |
601 | | } |
602 | | |
603 | | #[test] |
604 | | fn option_fmt_is_consistent() { |
605 | | check_fmt(Option::Some::<i32>(42)); |
606 | | check_fmt(Option::None::<i32>); |
607 | | } |
608 | | |
609 | | #[test] |
610 | | fn result_fmt_is_consistent() { |
611 | | check_fmt(Result::Ok::<i32, i32>(42)); |
612 | | check_fmt(Result::Err::<i32, i32>(42)); |
613 | | } |
614 | | |
615 | | #[test] |
616 | | fn tuple_fmt_is_consistent() { |
617 | | check_fmt((42, 17)); |
618 | | } |
619 | | |
620 | | #[test] |
621 | | fn tagged_fmt_is_consistent() { |
622 | | #[derive(Serialize, Debug)] |
623 | | enum Tagged { |
624 | | Unit, |
625 | | NewType(i32), |
626 | | Tuple(i32, i32), |
627 | | Struct { a: i32, b: i32 }, |
628 | | } |
629 | | |
630 | | check_fmt(Tagged::Unit); |
631 | | check_fmt(Tagged::NewType(42)); |
632 | | check_fmt(Tagged::Tuple(42, 17)); |
633 | | check_fmt(Tagged::Struct { a: 42, b: 17 }); |
634 | | } |
635 | | } |