serde_request_envelope/
support.rs1use serde::ser::{
2 self, Impossible, Serialize, SerializeStruct, SerializeStructVariant, SerializeTupleStruct,
3 Serializer,
4};
5use std::fmt::{self, Display};
6
7#[derive(Debug)]
8pub struct NotStruct;
9
10type Result<T> = std::result::Result<T, NotStruct>;
11
12impl std::error::Error for NotStruct {
13 fn description(&self) -> &str {
14 "not struct"
15 }
16}
17
18impl Display for NotStruct {
19 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20 write!(f, "{}", "not struct")
21 }
22}
23
24impl ser::Error for NotStruct {
25 fn custom<T: Display>(_msg: T) -> Self {
26 NotStruct
27 }
28}
29
30pub fn type_name<T: Serialize>(t: &T) -> Result<&'static str> {
32 struct TypeName;
33 impl Serializer for TypeName {
34 type Ok = &'static str;
35 type Error = NotStruct;
36 type SerializeSeq = Impossible<Self::Ok, Self::Error>;
37 type SerializeTuple = Impossible<Self::Ok, Self::Error>;
38 type SerializeTupleStruct = Struct;
39 type SerializeTupleVariant = Impossible<Self::Ok, Self::Error>;
40 type SerializeMap = Impossible<Self::Ok, Self::Error>;
41 type SerializeStruct = Struct;
42 type SerializeStructVariant = Struct;
43 fn serialize_bool(self, _v: bool) -> Result<Self::Ok> {
44 Err(NotStruct)
45 }
46 fn serialize_i8(self, _v: i8) -> Result<Self::Ok> {
47 Err(NotStruct)
48 }
49 fn serialize_i16(self, _v: i16) -> Result<Self::Ok> {
50 Err(NotStruct)
51 }
52 fn serialize_i32(self, _v: i32) -> Result<Self::Ok> {
53 Err(NotStruct)
54 }
55 fn serialize_i64(self, _v: i64) -> Result<Self::Ok> {
56 Err(NotStruct)
57 }
58 fn serialize_u8(self, _v: u8) -> Result<Self::Ok> {
59 Err(NotStruct)
60 }
61 fn serialize_u16(self, _v: u16) -> Result<Self::Ok> {
62 Err(NotStruct)
63 }
64 fn serialize_u32(self, _v: u32) -> Result<Self::Ok> {
65 Err(NotStruct)
66 }
67 fn serialize_u64(self, _v: u64) -> Result<Self::Ok> {
68 Err(NotStruct)
69 }
70 fn serialize_f32(self, _v: f32) -> Result<Self::Ok> {
71 Err(NotStruct)
72 }
73 fn serialize_f64(self, _v: f64) -> Result<Self::Ok> {
74 Err(NotStruct)
75 }
76 fn serialize_char(self, _v: char) -> Result<Self::Ok> {
77 Err(NotStruct)
78 }
79 fn serialize_str(self, _v: &str) -> Result<Self::Ok> {
80 Err(NotStruct)
81 }
82 fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok> {
83 Err(NotStruct)
84 }
85 fn serialize_none(self) -> Result<Self::Ok> {
86 Err(NotStruct)
87 }
88 fn serialize_some<T: ?Sized + Serialize>(self, _value: &T) -> Result<Self::Ok> {
89 Err(NotStruct)
90 }
91 fn serialize_unit(self) -> Result<Self::Ok> {
92 Err(NotStruct)
93 }
94 fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok> {
95 Ok(name)
96 }
97 fn serialize_unit_variant(
98 self,
99 name: &'static str,
100 _variant_index: u32,
101 _variant: &'static str,
102 ) -> Result<Self::Ok> {
103 Ok(name)
104 }
105 fn serialize_newtype_struct<T: ?Sized + Serialize>(
106 self,
107 name: &'static str,
108 _value: &T,
109 ) -> Result<Self::Ok> {
110 Ok(name)
111 }
112 fn serialize_newtype_variant<T: ?Sized + Serialize>(
113 self,
114 name: &'static str,
115 _variant_index: u32,
116 _variant: &'static str,
117 _value: &T,
118 ) -> Result<Self::Ok> {
119 Ok(name)
120 }
121 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
122 Err(NotStruct)
123 }
124 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
125 Err(NotStruct)
126 }
127 fn serialize_tuple_struct(
128 self,
129 name: &'static str,
130 _len: usize,
131 ) -> Result<Self::SerializeTupleStruct> {
132 Ok(Struct(name))
133 }
134 fn serialize_tuple_variant(
135 self,
136 _name: &'static str,
137 _variant_index: u32,
138 _variant: &'static str,
139 _len: usize,
140 ) -> Result<Self::SerializeTupleVariant> {
141 Err(NotStruct)
142 }
143 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
144 Err(NotStruct)
145 }
146 fn serialize_struct(
147 self,
148 name: &'static str,
149 _len: usize,
150 ) -> Result<Self::SerializeStruct> {
151 Ok(Struct(name))
152 }
153 fn serialize_struct_variant(
154 self,
155 name: &'static str,
156 _variant_index: u32,
157 _variant: &'static str,
158 _len: usize,
159 ) -> Result<Self::SerializeStructVariant> {
160 Ok(Struct(name))
161 }
162 }
163
164 struct Struct(&'static str);
165 impl SerializeStruct for Struct {
166 type Ok = &'static str;
167 type Error = NotStruct;
168 fn serialize_field<T: ?Sized + Serialize>(
169 &mut self,
170 _key: &'static str,
171 _value: &T,
172 ) -> Result<()> {
173 Ok(())
174 }
175 fn end(self) -> Result<Self::Ok> {
176 Ok(self.0)
177 }
178 }
179 impl SerializeTupleStruct for Struct {
180 type Ok = &'static str;
181 type Error = NotStruct;
182 fn serialize_field<T: ?Sized + Serialize>(&mut self, _value: &T) -> Result<()> {
183 Ok(())
184 }
185 fn end(self) -> Result<Self::Ok> {
186 Ok(self.0)
187 }
188 }
189
190 impl SerializeStructVariant for Struct {
191 type Ok = &'static str;
192 type Error = NotStruct;
193
194 fn serialize_field<T: ?Sized + Serialize>(
195 &mut self,
196 _key: &'static str,
197 _value: &T,
198 ) -> Result<()> {
199 Ok(())
200 }
201 fn end(self) -> Result<Self::Ok> {
202 Ok(self.0)
203 }
204 }
205
206 t.serialize(TypeName)
207}