/rust/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.216/src/ser/fmt.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::lib::*; |
2 | | use crate::ser::{Error, Impossible, Serialize, Serializer}; |
3 | | |
4 | | impl Error for fmt::Error { |
5 | 0 | fn custom<T: Display>(_msg: T) -> Self { |
6 | 0 | fmt::Error |
7 | 0 | } |
8 | | } |
9 | | |
10 | | macro_rules! fmt_primitives { |
11 | | ($($f:ident: $t:ty,)*) => { |
12 | | $( |
13 | 0 | fn $f(self, v: $t) -> fmt::Result { |
14 | 0 | Display::fmt(&v, self) |
15 | 0 | } Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_bool Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_i8 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_i16 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_i32 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_i64 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_i128 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_u8 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_u16 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_u32 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_u64 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_u128 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_f32 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_f64 Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_char Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_str Unexecuted instantiation: <&mut core::fmt::Formatter as serde::ser::Serializer>::serialize_unit_struct |
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 | | /// ``` |
38 | | impl<'a> Serializer for &mut fmt::Formatter<'a> { |
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_i128: i128, |
56 | | serialize_u8: u8, |
57 | | serialize_u16: u16, |
58 | | serialize_u32: u32, |
59 | | serialize_u64: u64, |
60 | | serialize_u128: u128, |
61 | | serialize_f32: f32, |
62 | | serialize_f64: f64, |
63 | | serialize_char: char, |
64 | | serialize_str: &str, |
65 | | serialize_unit_struct: &'static str, |
66 | | } |
67 | | |
68 | 0 | fn serialize_unit_variant( |
69 | 0 | self, |
70 | 0 | _name: &'static str, |
71 | 0 | _variant_index: u32, |
72 | 0 | variant: &'static str, |
73 | 0 | ) -> fmt::Result { |
74 | 0 | Display::fmt(variant, self) |
75 | 0 | } |
76 | | |
77 | 0 | fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> fmt::Result |
78 | 0 | where |
79 | 0 | T: ?Sized + Serialize, |
80 | 0 | { |
81 | 0 | Serialize::serialize(value, self) |
82 | 0 | } |
83 | | |
84 | 0 | fn serialize_bytes(self, _v: &[u8]) -> fmt::Result { |
85 | 0 | Err(fmt::Error) |
86 | 0 | } |
87 | | |
88 | 0 | fn serialize_none(self) -> fmt::Result { |
89 | 0 | Err(fmt::Error) |
90 | 0 | } |
91 | | |
92 | 0 | fn serialize_some<T>(self, _value: &T) -> fmt::Result |
93 | 0 | where |
94 | 0 | T: ?Sized + Serialize, |
95 | 0 | { |
96 | 0 | Err(fmt::Error) |
97 | 0 | } |
98 | | |
99 | 0 | fn serialize_unit(self) -> fmt::Result { |
100 | 0 | Err(fmt::Error) |
101 | 0 | } |
102 | | |
103 | 0 | fn serialize_newtype_variant<T>( |
104 | 0 | self, |
105 | 0 | _name: &'static str, |
106 | 0 | _variant_index: u32, |
107 | 0 | _variant: &'static str, |
108 | 0 | _value: &T, |
109 | 0 | ) -> fmt::Result |
110 | 0 | where |
111 | 0 | T: ?Sized + Serialize, |
112 | 0 | { |
113 | 0 | Err(fmt::Error) |
114 | 0 | } |
115 | | |
116 | 0 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, fmt::Error> { |
117 | 0 | Err(fmt::Error) |
118 | 0 | } |
119 | | |
120 | 0 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, fmt::Error> { |
121 | 0 | Err(fmt::Error) |
122 | 0 | } |
123 | | |
124 | 0 | fn serialize_tuple_struct( |
125 | 0 | self, |
126 | 0 | _name: &'static str, |
127 | 0 | _len: usize, |
128 | 0 | ) -> Result<Self::SerializeTupleStruct, fmt::Error> { |
129 | 0 | Err(fmt::Error) |
130 | 0 | } |
131 | | |
132 | 0 | fn serialize_tuple_variant( |
133 | 0 | self, |
134 | 0 | _name: &'static str, |
135 | 0 | _variant_index: u32, |
136 | 0 | _variant: &'static str, |
137 | 0 | _len: usize, |
138 | 0 | ) -> Result<Self::SerializeTupleVariant, fmt::Error> { |
139 | 0 | Err(fmt::Error) |
140 | 0 | } |
141 | | |
142 | 0 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, fmt::Error> { |
143 | 0 | Err(fmt::Error) |
144 | 0 | } |
145 | | |
146 | 0 | fn serialize_struct( |
147 | 0 | self, |
148 | 0 | _name: &'static str, |
149 | 0 | _len: usize, |
150 | 0 | ) -> Result<Self::SerializeStruct, fmt::Error> { |
151 | 0 | Err(fmt::Error) |
152 | 0 | } |
153 | | |
154 | 0 | fn serialize_struct_variant( |
155 | 0 | self, |
156 | 0 | _name: &'static str, |
157 | 0 | _variant_index: u32, |
158 | 0 | _variant: &'static str, |
159 | 0 | _len: usize, |
160 | 0 | ) -> Result<Self::SerializeStructVariant, fmt::Error> { |
161 | 0 | Err(fmt::Error) |
162 | 0 | } |
163 | | |
164 | 0 | fn collect_str<T>(self, value: &T) -> fmt::Result |
165 | 0 | where |
166 | 0 | T: ?Sized + Display, |
167 | 0 | { |
168 | 0 | Display::fmt(value, self) |
169 | 0 | } |
170 | | } |