/rust/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.152/src/private/ser.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use lib::*; |
2 | | |
3 | | use ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}; |
4 | | |
5 | | #[cfg(any(feature = "std", feature = "alloc"))] |
6 | | use self::content::{ |
7 | | Content, ContentSerializer, SerializeStructVariantAsMapValue, SerializeTupleVariantAsMapValue, |
8 | | }; |
9 | | |
10 | | /// Used to check that serde(getter) attributes return the expected type. |
11 | | /// Not public API. |
12 | 0 | pub fn constrain<T: ?Sized>(t: &T) -> &T { |
13 | 0 | t |
14 | 0 | } |
15 | | |
16 | | /// Not public API. |
17 | 0 | pub fn serialize_tagged_newtype<S, T>( |
18 | 0 | serializer: S, |
19 | 0 | type_ident: &'static str, |
20 | 0 | variant_ident: &'static str, |
21 | 0 | tag: &'static str, |
22 | 0 | variant_name: &'static str, |
23 | 0 | value: &T, |
24 | 0 | ) -> Result<S::Ok, S::Error> |
25 | 0 | where |
26 | 0 | S: Serializer, |
27 | 0 | T: Serialize, |
28 | 0 | { |
29 | 0 | value.serialize(TaggedSerializer { |
30 | 0 | type_ident: type_ident, |
31 | 0 | variant_ident: variant_ident, |
32 | 0 | tag: tag, |
33 | 0 | variant_name: variant_name, |
34 | 0 | delegate: serializer, |
35 | 0 | }) |
36 | 0 | } |
37 | | |
38 | | struct TaggedSerializer<S> { |
39 | | type_ident: &'static str, |
40 | | variant_ident: &'static str, |
41 | | tag: &'static str, |
42 | | variant_name: &'static str, |
43 | | delegate: S, |
44 | | } |
45 | | |
46 | | enum Unsupported { |
47 | | Boolean, |
48 | | Integer, |
49 | | Float, |
50 | | Char, |
51 | | String, |
52 | | ByteArray, |
53 | | Optional, |
54 | | #[cfg(any(feature = "std", feature = "alloc"))] |
55 | | UnitStruct, |
56 | | Sequence, |
57 | | Tuple, |
58 | | TupleStruct, |
59 | | Enum, |
60 | | } |
61 | | |
62 | | impl Display for Unsupported { |
63 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
64 | 0 | match *self { |
65 | 0 | Unsupported::Boolean => formatter.write_str("a boolean"), |
66 | 0 | Unsupported::Integer => formatter.write_str("an integer"), |
67 | 0 | Unsupported::Float => formatter.write_str("a float"), |
68 | 0 | Unsupported::Char => formatter.write_str("a char"), |
69 | 0 | Unsupported::String => formatter.write_str("a string"), |
70 | 0 | Unsupported::ByteArray => formatter.write_str("a byte array"), |
71 | 0 | Unsupported::Optional => formatter.write_str("an optional"), |
72 | | #[cfg(any(feature = "std", feature = "alloc"))] |
73 | 0 | Unsupported::UnitStruct => formatter.write_str("unit struct"), |
74 | 0 | Unsupported::Sequence => formatter.write_str("a sequence"), |
75 | 0 | Unsupported::Tuple => formatter.write_str("a tuple"), |
76 | 0 | Unsupported::TupleStruct => formatter.write_str("a tuple struct"), |
77 | 0 | Unsupported::Enum => formatter.write_str("an enum"), |
78 | | } |
79 | 0 | } |
80 | | } |
81 | | |
82 | | impl<S> TaggedSerializer<S> |
83 | | where |
84 | | S: Serializer, |
85 | | { |
86 | 0 | fn bad_type(self, what: Unsupported) -> S::Error { |
87 | 0 | ser::Error::custom(format_args!( |
88 | 0 | "cannot serialize tagged newtype variant {}::{} containing {}", |
89 | 0 | self.type_ident, self.variant_ident, what |
90 | 0 | )) |
91 | 0 | } |
92 | | } |
93 | | |
94 | | impl<S> Serializer for TaggedSerializer<S> |
95 | | where |
96 | | S: Serializer, |
97 | | { |
98 | | type Ok = S::Ok; |
99 | | type Error = S::Error; |
100 | | |
101 | | type SerializeSeq = Impossible<S::Ok, S::Error>; |
102 | | type SerializeTuple = Impossible<S::Ok, S::Error>; |
103 | | type SerializeTupleStruct = Impossible<S::Ok, S::Error>; |
104 | | type SerializeMap = S::SerializeMap; |
105 | | type SerializeStruct = S::SerializeStruct; |
106 | | |
107 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
108 | | type SerializeTupleVariant = Impossible<S::Ok, S::Error>; |
109 | | #[cfg(any(feature = "std", feature = "alloc"))] |
110 | | type SerializeTupleVariant = SerializeTupleVariantAsMapValue<S::SerializeMap>; |
111 | | |
112 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
113 | | type SerializeStructVariant = Impossible<S::Ok, S::Error>; |
114 | | #[cfg(any(feature = "std", feature = "alloc"))] |
115 | | type SerializeStructVariant = SerializeStructVariantAsMapValue<S::SerializeMap>; |
116 | | |
117 | 0 | fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> { |
118 | 0 | Err(self.bad_type(Unsupported::Boolean)) |
119 | 0 | } |
120 | | |
121 | 0 | fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> { |
122 | 0 | Err(self.bad_type(Unsupported::Integer)) |
123 | 0 | } |
124 | | |
125 | 0 | fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> { |
126 | 0 | Err(self.bad_type(Unsupported::Integer)) |
127 | 0 | } |
128 | | |
129 | 0 | fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> { |
130 | 0 | Err(self.bad_type(Unsupported::Integer)) |
131 | 0 | } |
132 | | |
133 | 0 | fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> { |
134 | 0 | Err(self.bad_type(Unsupported::Integer)) |
135 | 0 | } |
136 | | |
137 | 0 | fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> { |
138 | 0 | Err(self.bad_type(Unsupported::Integer)) |
139 | 0 | } |
140 | | |
141 | 0 | fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> { |
142 | 0 | Err(self.bad_type(Unsupported::Integer)) |
143 | 0 | } |
144 | | |
145 | 0 | fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> { |
146 | 0 | Err(self.bad_type(Unsupported::Integer)) |
147 | 0 | } |
148 | | |
149 | 0 | fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> { |
150 | 0 | Err(self.bad_type(Unsupported::Integer)) |
151 | 0 | } |
152 | | |
153 | 0 | fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> { |
154 | 0 | Err(self.bad_type(Unsupported::Float)) |
155 | 0 | } |
156 | | |
157 | 0 | fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> { |
158 | 0 | Err(self.bad_type(Unsupported::Float)) |
159 | 0 | } |
160 | | |
161 | 0 | fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> { |
162 | 0 | Err(self.bad_type(Unsupported::Char)) |
163 | 0 | } |
164 | | |
165 | 0 | fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> { |
166 | 0 | Err(self.bad_type(Unsupported::String)) |
167 | 0 | } |
168 | | |
169 | 0 | fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> { |
170 | 0 | Err(self.bad_type(Unsupported::ByteArray)) |
171 | 0 | } |
172 | | |
173 | 0 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
174 | 0 | Err(self.bad_type(Unsupported::Optional)) |
175 | 0 | } |
176 | | |
177 | 0 | fn serialize_some<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error> |
178 | 0 | where |
179 | 0 | T: Serialize, |
180 | 0 | { |
181 | 0 | Err(self.bad_type(Unsupported::Optional)) |
182 | 0 | } |
183 | | |
184 | 0 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
185 | 0 | let mut map = try!(self.delegate.serialize_map(Some(1))); |
186 | 0 | try!(map.serialize_entry(self.tag, self.variant_name)); |
187 | 0 | map.end() |
188 | 0 | } |
189 | | |
190 | 0 | fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> { |
191 | 0 | let mut map = try!(self.delegate.serialize_map(Some(1))); |
192 | 0 | try!(map.serialize_entry(self.tag, self.variant_name)); |
193 | 0 | map.end() |
194 | 0 | } |
195 | | |
196 | 0 | fn serialize_unit_variant( |
197 | 0 | self, |
198 | 0 | _: &'static str, |
199 | 0 | _: u32, |
200 | 0 | inner_variant: &'static str, |
201 | 0 | ) -> Result<Self::Ok, Self::Error> { |
202 | 0 | let mut map = try!(self.delegate.serialize_map(Some(2))); |
203 | 0 | try!(map.serialize_entry(self.tag, self.variant_name)); |
204 | 0 | try!(map.serialize_entry(inner_variant, &())); |
205 | 0 | map.end() |
206 | 0 | } |
207 | | |
208 | 0 | fn serialize_newtype_struct<T: ?Sized>( |
209 | 0 | self, |
210 | 0 | _: &'static str, |
211 | 0 | value: &T, |
212 | 0 | ) -> Result<Self::Ok, Self::Error> |
213 | 0 | where |
214 | 0 | T: Serialize, |
215 | 0 | { |
216 | 0 | value.serialize(self) |
217 | 0 | } |
218 | | |
219 | 0 | fn serialize_newtype_variant<T: ?Sized>( |
220 | 0 | self, |
221 | 0 | _: &'static str, |
222 | 0 | _: u32, |
223 | 0 | inner_variant: &'static str, |
224 | 0 | inner_value: &T, |
225 | 0 | ) -> Result<Self::Ok, Self::Error> |
226 | 0 | where |
227 | 0 | T: Serialize, |
228 | 0 | { |
229 | 0 | let mut map = try!(self.delegate.serialize_map(Some(2))); |
230 | 0 | try!(map.serialize_entry(self.tag, self.variant_name)); |
231 | 0 | try!(map.serialize_entry(inner_variant, inner_value)); |
232 | 0 | map.end() |
233 | 0 | } |
234 | | |
235 | 0 | fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
236 | 0 | Err(self.bad_type(Unsupported::Sequence)) |
237 | 0 | } |
238 | | |
239 | 0 | fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> { |
240 | 0 | Err(self.bad_type(Unsupported::Tuple)) |
241 | 0 | } |
242 | | |
243 | 0 | fn serialize_tuple_struct( |
244 | 0 | self, |
245 | 0 | _: &'static str, |
246 | 0 | _: usize, |
247 | 0 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
248 | 0 | Err(self.bad_type(Unsupported::TupleStruct)) |
249 | 0 | } |
250 | | |
251 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
252 | | fn serialize_tuple_variant( |
253 | | self, |
254 | | _: &'static str, |
255 | | _: u32, |
256 | | _: &'static str, |
257 | | _: usize, |
258 | | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
259 | | // Lack of push-based serialization means we need to buffer the content |
260 | | // of the tuple variant, so it requires std. |
261 | | Err(self.bad_type(Unsupported::Enum)) |
262 | | } |
263 | | |
264 | | #[cfg(any(feature = "std", feature = "alloc"))] |
265 | 0 | fn serialize_tuple_variant( |
266 | 0 | self, |
267 | 0 | _: &'static str, |
268 | 0 | _: u32, |
269 | 0 | inner_variant: &'static str, |
270 | 0 | len: usize, |
271 | 0 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
272 | 0 | let mut map = try!(self.delegate.serialize_map(Some(2))); |
273 | 0 | try!(map.serialize_entry(self.tag, self.variant_name)); |
274 | 0 | try!(map.serialize_key(inner_variant)); |
275 | 0 | Ok(SerializeTupleVariantAsMapValue::new( |
276 | 0 | map, |
277 | 0 | inner_variant, |
278 | 0 | len, |
279 | 0 | )) |
280 | 0 | } |
281 | | |
282 | 0 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
283 | 0 | let mut map = try!(self.delegate.serialize_map(len.map(|len| len + 1))); |
284 | 0 | try!(map.serialize_entry(self.tag, self.variant_name)); |
285 | 0 | Ok(map) |
286 | 0 | } |
287 | | |
288 | 0 | fn serialize_struct( |
289 | 0 | self, |
290 | 0 | name: &'static str, |
291 | 0 | len: usize, |
292 | 0 | ) -> Result<Self::SerializeStruct, Self::Error> { |
293 | 0 | let mut state = try!(self.delegate.serialize_struct(name, len + 1)); |
294 | 0 | try!(state.serialize_field(self.tag, self.variant_name)); |
295 | 0 | Ok(state) |
296 | 0 | } |
297 | | |
298 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
299 | | fn serialize_struct_variant( |
300 | | self, |
301 | | _: &'static str, |
302 | | _: u32, |
303 | | _: &'static str, |
304 | | _: usize, |
305 | | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
306 | | // Lack of push-based serialization means we need to buffer the content |
307 | | // of the struct variant, so it requires std. |
308 | | Err(self.bad_type(Unsupported::Enum)) |
309 | | } |
310 | | |
311 | | #[cfg(any(feature = "std", feature = "alloc"))] |
312 | 0 | fn serialize_struct_variant( |
313 | 0 | self, |
314 | 0 | _: &'static str, |
315 | 0 | _: u32, |
316 | 0 | inner_variant: &'static str, |
317 | 0 | len: usize, |
318 | 0 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
319 | 0 | let mut map = try!(self.delegate.serialize_map(Some(2))); |
320 | 0 | try!(map.serialize_entry(self.tag, self.variant_name)); |
321 | 0 | try!(map.serialize_key(inner_variant)); |
322 | 0 | Ok(SerializeStructVariantAsMapValue::new( |
323 | 0 | map, |
324 | 0 | inner_variant, |
325 | 0 | len, |
326 | 0 | )) |
327 | 0 | } |
328 | | |
329 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
330 | | fn collect_str<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error> |
331 | | where |
332 | | T: Display, |
333 | | { |
334 | | Err(self.bad_type(Unsupported::String)) |
335 | | } |
336 | | } |
337 | | |
338 | | #[cfg(any(feature = "std", feature = "alloc"))] |
339 | | mod content { |
340 | | use lib::*; |
341 | | |
342 | | use ser::{self, Serialize, Serializer}; |
343 | | |
344 | | pub struct SerializeTupleVariantAsMapValue<M> { |
345 | | map: M, |
346 | | name: &'static str, |
347 | | fields: Vec<Content>, |
348 | | } |
349 | | |
350 | | impl<M> SerializeTupleVariantAsMapValue<M> { |
351 | 0 | pub fn new(map: M, name: &'static str, len: usize) -> Self { |
352 | 0 | SerializeTupleVariantAsMapValue { |
353 | 0 | map: map, |
354 | 0 | name: name, |
355 | 0 | fields: Vec::with_capacity(len), |
356 | 0 | } |
357 | 0 | } |
358 | | } |
359 | | |
360 | | impl<M> ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue<M> |
361 | | where |
362 | | M: ser::SerializeMap, |
363 | | { |
364 | | type Ok = M::Ok; |
365 | | type Error = M::Error; |
366 | | |
367 | 0 | fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), M::Error> |
368 | 0 | where |
369 | 0 | T: Serialize, |
370 | 0 | { |
371 | 0 | let value = try!(value.serialize(ContentSerializer::<M::Error>::new())); |
372 | 0 | self.fields.push(value); |
373 | 0 | Ok(()) |
374 | 0 | } |
375 | | |
376 | 0 | fn end(mut self) -> Result<M::Ok, M::Error> { |
377 | 0 | try!(self |
378 | 0 | .map |
379 | 0 | .serialize_value(&Content::TupleStruct(self.name, self.fields))); |
380 | 0 | self.map.end() |
381 | 0 | } |
382 | | } |
383 | | |
384 | | pub struct SerializeStructVariantAsMapValue<M> { |
385 | | map: M, |
386 | | name: &'static str, |
387 | | fields: Vec<(&'static str, Content)>, |
388 | | } |
389 | | |
390 | | impl<M> SerializeStructVariantAsMapValue<M> { |
391 | 0 | pub fn new(map: M, name: &'static str, len: usize) -> Self { |
392 | 0 | SerializeStructVariantAsMapValue { |
393 | 0 | map: map, |
394 | 0 | name: name, |
395 | 0 | fields: Vec::with_capacity(len), |
396 | 0 | } |
397 | 0 | } |
398 | | } |
399 | | |
400 | | impl<M> ser::SerializeStructVariant for SerializeStructVariantAsMapValue<M> |
401 | | where |
402 | | M: ser::SerializeMap, |
403 | | { |
404 | | type Ok = M::Ok; |
405 | | type Error = M::Error; |
406 | | |
407 | 0 | fn serialize_field<T: ?Sized>( |
408 | 0 | &mut self, |
409 | 0 | key: &'static str, |
410 | 0 | value: &T, |
411 | 0 | ) -> Result<(), M::Error> |
412 | 0 | where |
413 | 0 | T: Serialize, |
414 | 0 | { |
415 | 0 | let value = try!(value.serialize(ContentSerializer::<M::Error>::new())); |
416 | 0 | self.fields.push((key, value)); |
417 | 0 | Ok(()) |
418 | 0 | } |
419 | | |
420 | 0 | fn end(mut self) -> Result<M::Ok, M::Error> { |
421 | 0 | try!(self |
422 | 0 | .map |
423 | 0 | .serialize_value(&Content::Struct(self.name, self.fields))); |
424 | 0 | self.map.end() |
425 | 0 | } |
426 | | } |
427 | | |
428 | | pub enum Content { |
429 | | Bool(bool), |
430 | | |
431 | | U8(u8), |
432 | | U16(u16), |
433 | | U32(u32), |
434 | | U64(u64), |
435 | | |
436 | | I8(i8), |
437 | | I16(i16), |
438 | | I32(i32), |
439 | | I64(i64), |
440 | | |
441 | | F32(f32), |
442 | | F64(f64), |
443 | | |
444 | | Char(char), |
445 | | String(String), |
446 | | Bytes(Vec<u8>), |
447 | | |
448 | | None, |
449 | | Some(Box<Content>), |
450 | | |
451 | | Unit, |
452 | | UnitStruct(&'static str), |
453 | | UnitVariant(&'static str, u32, &'static str), |
454 | | NewtypeStruct(&'static str, Box<Content>), |
455 | | NewtypeVariant(&'static str, u32, &'static str, Box<Content>), |
456 | | |
457 | | Seq(Vec<Content>), |
458 | | Tuple(Vec<Content>), |
459 | | TupleStruct(&'static str, Vec<Content>), |
460 | | TupleVariant(&'static str, u32, &'static str, Vec<Content>), |
461 | | Map(Vec<(Content, Content)>), |
462 | | Struct(&'static str, Vec<(&'static str, Content)>), |
463 | | StructVariant( |
464 | | &'static str, |
465 | | u32, |
466 | | &'static str, |
467 | | Vec<(&'static str, Content)>, |
468 | | ), |
469 | | } |
470 | | |
471 | | impl Serialize for Content { |
472 | 0 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
473 | 0 | where |
474 | 0 | S: Serializer, |
475 | 0 | { |
476 | 0 | match *self { |
477 | 0 | Content::Bool(b) => serializer.serialize_bool(b), |
478 | 0 | Content::U8(u) => serializer.serialize_u8(u), |
479 | 0 | Content::U16(u) => serializer.serialize_u16(u), |
480 | 0 | Content::U32(u) => serializer.serialize_u32(u), |
481 | 0 | Content::U64(u) => serializer.serialize_u64(u), |
482 | 0 | Content::I8(i) => serializer.serialize_i8(i), |
483 | 0 | Content::I16(i) => serializer.serialize_i16(i), |
484 | 0 | Content::I32(i) => serializer.serialize_i32(i), |
485 | 0 | Content::I64(i) => serializer.serialize_i64(i), |
486 | 0 | Content::F32(f) => serializer.serialize_f32(f), |
487 | 0 | Content::F64(f) => serializer.serialize_f64(f), |
488 | 0 | Content::Char(c) => serializer.serialize_char(c), |
489 | 0 | Content::String(ref s) => serializer.serialize_str(s), |
490 | 0 | Content::Bytes(ref b) => serializer.serialize_bytes(b), |
491 | 0 | Content::None => serializer.serialize_none(), |
492 | 0 | Content::Some(ref c) => serializer.serialize_some(&**c), |
493 | 0 | Content::Unit => serializer.serialize_unit(), |
494 | 0 | Content::UnitStruct(n) => serializer.serialize_unit_struct(n), |
495 | 0 | Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v), |
496 | 0 | Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c), |
497 | 0 | Content::NewtypeVariant(n, i, v, ref c) => { |
498 | 0 | serializer.serialize_newtype_variant(n, i, v, &**c) |
499 | | } |
500 | 0 | Content::Seq(ref elements) => elements.serialize(serializer), |
501 | 0 | Content::Tuple(ref elements) => { |
502 | | use ser::SerializeTuple; |
503 | 0 | let mut tuple = try!(serializer.serialize_tuple(elements.len())); |
504 | 0 | for e in elements { |
505 | 0 | try!(tuple.serialize_element(e)); |
506 | | } |
507 | 0 | tuple.end() |
508 | | } |
509 | 0 | Content::TupleStruct(n, ref fields) => { |
510 | | use ser::SerializeTupleStruct; |
511 | 0 | let mut ts = try!(serializer.serialize_tuple_struct(n, fields.len())); |
512 | 0 | for f in fields { |
513 | 0 | try!(ts.serialize_field(f)); |
514 | | } |
515 | 0 | ts.end() |
516 | | } |
517 | 0 | Content::TupleVariant(n, i, v, ref fields) => { |
518 | | use ser::SerializeTupleVariant; |
519 | 0 | let mut tv = try!(serializer.serialize_tuple_variant(n, i, v, fields.len())); |
520 | 0 | for f in fields { |
521 | 0 | try!(tv.serialize_field(f)); |
522 | | } |
523 | 0 | tv.end() |
524 | | } |
525 | 0 | Content::Map(ref entries) => { |
526 | | use ser::SerializeMap; |
527 | 0 | let mut map = try!(serializer.serialize_map(Some(entries.len()))); |
528 | 0 | for (k, v) in entries { |
529 | 0 | try!(map.serialize_entry(k, v)); |
530 | | } |
531 | 0 | map.end() |
532 | | } |
533 | 0 | Content::Struct(n, ref fields) => { |
534 | | use ser::SerializeStruct; |
535 | 0 | let mut s = try!(serializer.serialize_struct(n, fields.len())); |
536 | 0 | for &(k, ref v) in fields { |
537 | 0 | try!(s.serialize_field(k, v)); |
538 | | } |
539 | 0 | s.end() |
540 | | } |
541 | 0 | Content::StructVariant(n, i, v, ref fields) => { |
542 | | use ser::SerializeStructVariant; |
543 | 0 | let mut sv = try!(serializer.serialize_struct_variant(n, i, v, fields.len())); |
544 | 0 | for &(k, ref v) in fields { |
545 | 0 | try!(sv.serialize_field(k, v)); |
546 | | } |
547 | 0 | sv.end() |
548 | | } |
549 | | } |
550 | 0 | } |
551 | | } |
552 | | |
553 | | pub struct ContentSerializer<E> { |
554 | | error: PhantomData<E>, |
555 | | } |
556 | | |
557 | | impl<E> ContentSerializer<E> { |
558 | 0 | pub fn new() -> Self { |
559 | 0 | ContentSerializer { error: PhantomData } |
560 | 0 | } |
561 | | } |
562 | | |
563 | | impl<E> Serializer for ContentSerializer<E> |
564 | | where |
565 | | E: ser::Error, |
566 | | { |
567 | | type Ok = Content; |
568 | | type Error = E; |
569 | | |
570 | | type SerializeSeq = SerializeSeq<E>; |
571 | | type SerializeTuple = SerializeTuple<E>; |
572 | | type SerializeTupleStruct = SerializeTupleStruct<E>; |
573 | | type SerializeTupleVariant = SerializeTupleVariant<E>; |
574 | | type SerializeMap = SerializeMap<E>; |
575 | | type SerializeStruct = SerializeStruct<E>; |
576 | | type SerializeStructVariant = SerializeStructVariant<E>; |
577 | | |
578 | 0 | fn serialize_bool(self, v: bool) -> Result<Content, E> { |
579 | 0 | Ok(Content::Bool(v)) |
580 | 0 | } |
581 | | |
582 | 0 | fn serialize_i8(self, v: i8) -> Result<Content, E> { |
583 | 0 | Ok(Content::I8(v)) |
584 | 0 | } |
585 | | |
586 | 0 | fn serialize_i16(self, v: i16) -> Result<Content, E> { |
587 | 0 | Ok(Content::I16(v)) |
588 | 0 | } |
589 | | |
590 | 0 | fn serialize_i32(self, v: i32) -> Result<Content, E> { |
591 | 0 | Ok(Content::I32(v)) |
592 | 0 | } |
593 | | |
594 | 0 | fn serialize_i64(self, v: i64) -> Result<Content, E> { |
595 | 0 | Ok(Content::I64(v)) |
596 | 0 | } |
597 | | |
598 | 0 | fn serialize_u8(self, v: u8) -> Result<Content, E> { |
599 | 0 | Ok(Content::U8(v)) |
600 | 0 | } |
601 | | |
602 | 0 | fn serialize_u16(self, v: u16) -> Result<Content, E> { |
603 | 0 | Ok(Content::U16(v)) |
604 | 0 | } |
605 | | |
606 | 0 | fn serialize_u32(self, v: u32) -> Result<Content, E> { |
607 | 0 | Ok(Content::U32(v)) |
608 | 0 | } |
609 | | |
610 | 0 | fn serialize_u64(self, v: u64) -> Result<Content, E> { |
611 | 0 | Ok(Content::U64(v)) |
612 | 0 | } |
613 | | |
614 | 0 | fn serialize_f32(self, v: f32) -> Result<Content, E> { |
615 | 0 | Ok(Content::F32(v)) |
616 | 0 | } |
617 | | |
618 | 0 | fn serialize_f64(self, v: f64) -> Result<Content, E> { |
619 | 0 | Ok(Content::F64(v)) |
620 | 0 | } |
621 | | |
622 | 0 | fn serialize_char(self, v: char) -> Result<Content, E> { |
623 | 0 | Ok(Content::Char(v)) |
624 | 0 | } |
625 | | |
626 | 0 | fn serialize_str(self, value: &str) -> Result<Content, E> { |
627 | 0 | Ok(Content::String(value.to_owned())) |
628 | 0 | } |
629 | | |
630 | 0 | fn serialize_bytes(self, value: &[u8]) -> Result<Content, E> { |
631 | 0 | Ok(Content::Bytes(value.to_owned())) |
632 | 0 | } |
633 | | |
634 | 0 | fn serialize_none(self) -> Result<Content, E> { |
635 | 0 | Ok(Content::None) |
636 | 0 | } |
637 | | |
638 | 0 | fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Content, E> |
639 | 0 | where |
640 | 0 | T: Serialize, |
641 | 0 | { |
642 | 0 | Ok(Content::Some(Box::new(try!(value.serialize(self))))) |
643 | 0 | } |
644 | | |
645 | 0 | fn serialize_unit(self) -> Result<Content, E> { |
646 | 0 | Ok(Content::Unit) |
647 | 0 | } |
648 | | |
649 | 0 | fn serialize_unit_struct(self, name: &'static str) -> Result<Content, E> { |
650 | 0 | Ok(Content::UnitStruct(name)) |
651 | 0 | } |
652 | | |
653 | 0 | fn serialize_unit_variant( |
654 | 0 | self, |
655 | 0 | name: &'static str, |
656 | 0 | variant_index: u32, |
657 | 0 | variant: &'static str, |
658 | 0 | ) -> Result<Content, E> { |
659 | 0 | Ok(Content::UnitVariant(name, variant_index, variant)) |
660 | 0 | } |
661 | | |
662 | 0 | fn serialize_newtype_struct<T: ?Sized>( |
663 | 0 | self, |
664 | 0 | name: &'static str, |
665 | 0 | value: &T, |
666 | 0 | ) -> Result<Content, E> |
667 | 0 | where |
668 | 0 | T: Serialize, |
669 | 0 | { |
670 | 0 | Ok(Content::NewtypeStruct( |
671 | 0 | name, |
672 | 0 | Box::new(try!(value.serialize(self))), |
673 | | )) |
674 | 0 | } |
675 | | |
676 | 0 | fn serialize_newtype_variant<T: ?Sized>( |
677 | 0 | self, |
678 | 0 | name: &'static str, |
679 | 0 | variant_index: u32, |
680 | 0 | variant: &'static str, |
681 | 0 | value: &T, |
682 | 0 | ) -> Result<Content, E> |
683 | 0 | where |
684 | 0 | T: Serialize, |
685 | 0 | { |
686 | 0 | Ok(Content::NewtypeVariant( |
687 | 0 | name, |
688 | 0 | variant_index, |
689 | 0 | variant, |
690 | 0 | Box::new(try!(value.serialize(self))), |
691 | | )) |
692 | 0 | } |
693 | | |
694 | 0 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> { |
695 | 0 | Ok(SerializeSeq { |
696 | 0 | elements: Vec::with_capacity(len.unwrap_or(0)), |
697 | 0 | error: PhantomData, |
698 | 0 | }) |
699 | 0 | } |
700 | | |
701 | 0 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> { |
702 | 0 | Ok(SerializeTuple { |
703 | 0 | elements: Vec::with_capacity(len), |
704 | 0 | error: PhantomData, |
705 | 0 | }) |
706 | 0 | } |
707 | | |
708 | 0 | fn serialize_tuple_struct( |
709 | 0 | self, |
710 | 0 | name: &'static str, |
711 | 0 | len: usize, |
712 | 0 | ) -> Result<Self::SerializeTupleStruct, E> { |
713 | 0 | Ok(SerializeTupleStruct { |
714 | 0 | name: name, |
715 | 0 | fields: Vec::with_capacity(len), |
716 | 0 | error: PhantomData, |
717 | 0 | }) |
718 | 0 | } |
719 | | |
720 | 0 | fn serialize_tuple_variant( |
721 | 0 | self, |
722 | 0 | name: &'static str, |
723 | 0 | variant_index: u32, |
724 | 0 | variant: &'static str, |
725 | 0 | len: usize, |
726 | 0 | ) -> Result<Self::SerializeTupleVariant, E> { |
727 | 0 | Ok(SerializeTupleVariant { |
728 | 0 | name: name, |
729 | 0 | variant_index: variant_index, |
730 | 0 | variant: variant, |
731 | 0 | fields: Vec::with_capacity(len), |
732 | 0 | error: PhantomData, |
733 | 0 | }) |
734 | 0 | } |
735 | | |
736 | 0 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, E> { |
737 | 0 | Ok(SerializeMap { |
738 | 0 | entries: Vec::with_capacity(len.unwrap_or(0)), |
739 | 0 | key: None, |
740 | 0 | error: PhantomData, |
741 | 0 | }) |
742 | 0 | } |
743 | | |
744 | 0 | fn serialize_struct( |
745 | 0 | self, |
746 | 0 | name: &'static str, |
747 | 0 | len: usize, |
748 | 0 | ) -> Result<Self::SerializeStruct, E> { |
749 | 0 | Ok(SerializeStruct { |
750 | 0 | name: name, |
751 | 0 | fields: Vec::with_capacity(len), |
752 | 0 | error: PhantomData, |
753 | 0 | }) |
754 | 0 | } |
755 | | |
756 | 0 | fn serialize_struct_variant( |
757 | 0 | self, |
758 | 0 | name: &'static str, |
759 | 0 | variant_index: u32, |
760 | 0 | variant: &'static str, |
761 | 0 | len: usize, |
762 | 0 | ) -> Result<Self::SerializeStructVariant, E> { |
763 | 0 | Ok(SerializeStructVariant { |
764 | 0 | name: name, |
765 | 0 | variant_index: variant_index, |
766 | 0 | variant: variant, |
767 | 0 | fields: Vec::with_capacity(len), |
768 | 0 | error: PhantomData, |
769 | 0 | }) |
770 | 0 | } |
771 | | } |
772 | | |
773 | | pub struct SerializeSeq<E> { |
774 | | elements: Vec<Content>, |
775 | | error: PhantomData<E>, |
776 | | } |
777 | | |
778 | | impl<E> ser::SerializeSeq for SerializeSeq<E> |
779 | | where |
780 | | E: ser::Error, |
781 | | { |
782 | | type Ok = Content; |
783 | | type Error = E; |
784 | | |
785 | 0 | fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E> |
786 | 0 | where |
787 | 0 | T: Serialize, |
788 | 0 | { |
789 | 0 | let value = try!(value.serialize(ContentSerializer::<E>::new())); |
790 | 0 | self.elements.push(value); |
791 | 0 | Ok(()) |
792 | 0 | } |
793 | | |
794 | 0 | fn end(self) -> Result<Content, E> { |
795 | 0 | Ok(Content::Seq(self.elements)) |
796 | 0 | } |
797 | | } |
798 | | |
799 | | pub struct SerializeTuple<E> { |
800 | | elements: Vec<Content>, |
801 | | error: PhantomData<E>, |
802 | | } |
803 | | |
804 | | impl<E> ser::SerializeTuple for SerializeTuple<E> |
805 | | where |
806 | | E: ser::Error, |
807 | | { |
808 | | type Ok = Content; |
809 | | type Error = E; |
810 | | |
811 | 0 | fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E> |
812 | 0 | where |
813 | 0 | T: Serialize, |
814 | 0 | { |
815 | 0 | let value = try!(value.serialize(ContentSerializer::<E>::new())); |
816 | 0 | self.elements.push(value); |
817 | 0 | Ok(()) |
818 | 0 | } |
819 | | |
820 | 0 | fn end(self) -> Result<Content, E> { |
821 | 0 | Ok(Content::Tuple(self.elements)) |
822 | 0 | } |
823 | | } |
824 | | |
825 | | pub struct SerializeTupleStruct<E> { |
826 | | name: &'static str, |
827 | | fields: Vec<Content>, |
828 | | error: PhantomData<E>, |
829 | | } |
830 | | |
831 | | impl<E> ser::SerializeTupleStruct for SerializeTupleStruct<E> |
832 | | where |
833 | | E: ser::Error, |
834 | | { |
835 | | type Ok = Content; |
836 | | type Error = E; |
837 | | |
838 | 0 | fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E> |
839 | 0 | where |
840 | 0 | T: Serialize, |
841 | 0 | { |
842 | 0 | let value = try!(value.serialize(ContentSerializer::<E>::new())); |
843 | 0 | self.fields.push(value); |
844 | 0 | Ok(()) |
845 | 0 | } |
846 | | |
847 | 0 | fn end(self) -> Result<Content, E> { |
848 | 0 | Ok(Content::TupleStruct(self.name, self.fields)) |
849 | 0 | } |
850 | | } |
851 | | |
852 | | pub struct SerializeTupleVariant<E> { |
853 | | name: &'static str, |
854 | | variant_index: u32, |
855 | | variant: &'static str, |
856 | | fields: Vec<Content>, |
857 | | error: PhantomData<E>, |
858 | | } |
859 | | |
860 | | impl<E> ser::SerializeTupleVariant for SerializeTupleVariant<E> |
861 | | where |
862 | | E: ser::Error, |
863 | | { |
864 | | type Ok = Content; |
865 | | type Error = E; |
866 | | |
867 | 0 | fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E> |
868 | 0 | where |
869 | 0 | T: Serialize, |
870 | 0 | { |
871 | 0 | let value = try!(value.serialize(ContentSerializer::<E>::new())); |
872 | 0 | self.fields.push(value); |
873 | 0 | Ok(()) |
874 | 0 | } |
875 | | |
876 | 0 | fn end(self) -> Result<Content, E> { |
877 | 0 | Ok(Content::TupleVariant( |
878 | 0 | self.name, |
879 | 0 | self.variant_index, |
880 | 0 | self.variant, |
881 | 0 | self.fields, |
882 | 0 | )) |
883 | 0 | } |
884 | | } |
885 | | |
886 | | pub struct SerializeMap<E> { |
887 | | entries: Vec<(Content, Content)>, |
888 | | key: Option<Content>, |
889 | | error: PhantomData<E>, |
890 | | } |
891 | | |
892 | | impl<E> ser::SerializeMap for SerializeMap<E> |
893 | | where |
894 | | E: ser::Error, |
895 | | { |
896 | | type Ok = Content; |
897 | | type Error = E; |
898 | | |
899 | 0 | fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), E> |
900 | 0 | where |
901 | 0 | T: Serialize, |
902 | 0 | { |
903 | 0 | let key = try!(key.serialize(ContentSerializer::<E>::new())); |
904 | 0 | self.key = Some(key); |
905 | 0 | Ok(()) |
906 | 0 | } |
907 | | |
908 | 0 | fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), E> |
909 | 0 | where |
910 | 0 | T: Serialize, |
911 | 0 | { |
912 | 0 | let key = self |
913 | 0 | .key |
914 | 0 | .take() |
915 | 0 | .expect("serialize_value called before serialize_key"); |
916 | 0 | let value = try!(value.serialize(ContentSerializer::<E>::new())); |
917 | 0 | self.entries.push((key, value)); |
918 | 0 | Ok(()) |
919 | 0 | } |
920 | | |
921 | 0 | fn end(self) -> Result<Content, E> { |
922 | 0 | Ok(Content::Map(self.entries)) |
923 | 0 | } |
924 | | |
925 | 0 | fn serialize_entry<K: ?Sized, V: ?Sized>(&mut self, key: &K, value: &V) -> Result<(), E> |
926 | 0 | where |
927 | 0 | K: Serialize, |
928 | 0 | V: Serialize, |
929 | 0 | { |
930 | 0 | let key = try!(key.serialize(ContentSerializer::<E>::new())); |
931 | 0 | let value = try!(value.serialize(ContentSerializer::<E>::new())); |
932 | 0 | self.entries.push((key, value)); |
933 | 0 | Ok(()) |
934 | 0 | } |
935 | | } |
936 | | |
937 | | pub struct SerializeStruct<E> { |
938 | | name: &'static str, |
939 | | fields: Vec<(&'static str, Content)>, |
940 | | error: PhantomData<E>, |
941 | | } |
942 | | |
943 | | impl<E> ser::SerializeStruct for SerializeStruct<E> |
944 | | where |
945 | | E: ser::Error, |
946 | | { |
947 | | type Ok = Content; |
948 | | type Error = E; |
949 | | |
950 | 0 | fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E> |
951 | 0 | where |
952 | 0 | T: Serialize, |
953 | 0 | { |
954 | 0 | let value = try!(value.serialize(ContentSerializer::<E>::new())); |
955 | 0 | self.fields.push((key, value)); |
956 | 0 | Ok(()) |
957 | 0 | } |
958 | | |
959 | 0 | fn end(self) -> Result<Content, E> { |
960 | 0 | Ok(Content::Struct(self.name, self.fields)) |
961 | 0 | } |
962 | | } |
963 | | |
964 | | pub struct SerializeStructVariant<E> { |
965 | | name: &'static str, |
966 | | variant_index: u32, |
967 | | variant: &'static str, |
968 | | fields: Vec<(&'static str, Content)>, |
969 | | error: PhantomData<E>, |
970 | | } |
971 | | |
972 | | impl<E> ser::SerializeStructVariant for SerializeStructVariant<E> |
973 | | where |
974 | | E: ser::Error, |
975 | | { |
976 | | type Ok = Content; |
977 | | type Error = E; |
978 | | |
979 | 0 | fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E> |
980 | 0 | where |
981 | 0 | T: Serialize, |
982 | 0 | { |
983 | 0 | let value = try!(value.serialize(ContentSerializer::<E>::new())); |
984 | 0 | self.fields.push((key, value)); |
985 | 0 | Ok(()) |
986 | 0 | } |
987 | | |
988 | 0 | fn end(self) -> Result<Content, E> { |
989 | 0 | Ok(Content::StructVariant( |
990 | 0 | self.name, |
991 | 0 | self.variant_index, |
992 | 0 | self.variant, |
993 | 0 | self.fields, |
994 | 0 | )) |
995 | 0 | } |
996 | | } |
997 | | } |
998 | | |
999 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1000 | | pub struct FlatMapSerializer<'a, M: 'a>(pub &'a mut M); |
1001 | | |
1002 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1003 | | impl<'a, M> FlatMapSerializer<'a, M> |
1004 | | where |
1005 | | M: SerializeMap + 'a, |
1006 | | { |
1007 | 0 | fn bad_type(what: Unsupported) -> M::Error { |
1008 | 0 | ser::Error::custom(format_args!( |
1009 | 0 | "can only flatten structs and maps (got {})", |
1010 | 0 | what |
1011 | 0 | )) |
1012 | 0 | } |
1013 | | } |
1014 | | |
1015 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1016 | | impl<'a, M> Serializer for FlatMapSerializer<'a, M> |
1017 | | where |
1018 | | M: SerializeMap + 'a, |
1019 | | { |
1020 | | type Ok = (); |
1021 | | type Error = M::Error; |
1022 | | |
1023 | | type SerializeSeq = Impossible<Self::Ok, M::Error>; |
1024 | | type SerializeTuple = Impossible<Self::Ok, M::Error>; |
1025 | | type SerializeTupleStruct = Impossible<Self::Ok, M::Error>; |
1026 | | type SerializeMap = FlatMapSerializeMap<'a, M>; |
1027 | | type SerializeStruct = FlatMapSerializeStruct<'a, M>; |
1028 | | type SerializeTupleVariant = Impossible<Self::Ok, M::Error>; |
1029 | | type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>; |
1030 | | |
1031 | 0 | fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> { |
1032 | 0 | Err(Self::bad_type(Unsupported::Boolean)) |
1033 | 0 | } |
1034 | | |
1035 | 0 | fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> { |
1036 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1037 | 0 | } |
1038 | | |
1039 | 0 | fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> { |
1040 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1041 | 0 | } |
1042 | | |
1043 | 0 | fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> { |
1044 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1045 | 0 | } |
1046 | | |
1047 | 0 | fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> { |
1048 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1049 | 0 | } |
1050 | | |
1051 | 0 | fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> { |
1052 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1053 | 0 | } |
1054 | | |
1055 | 0 | fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> { |
1056 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1057 | 0 | } |
1058 | | |
1059 | 0 | fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> { |
1060 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1061 | 0 | } |
1062 | | |
1063 | 0 | fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> { |
1064 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1065 | 0 | } |
1066 | | |
1067 | 0 | fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> { |
1068 | 0 | Err(Self::bad_type(Unsupported::Float)) |
1069 | 0 | } |
1070 | | |
1071 | 0 | fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> { |
1072 | 0 | Err(Self::bad_type(Unsupported::Float)) |
1073 | 0 | } |
1074 | | |
1075 | 0 | fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> { |
1076 | 0 | Err(Self::bad_type(Unsupported::Char)) |
1077 | 0 | } |
1078 | | |
1079 | 0 | fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> { |
1080 | 0 | Err(Self::bad_type(Unsupported::String)) |
1081 | 0 | } |
1082 | | |
1083 | 0 | fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> { |
1084 | 0 | Err(Self::bad_type(Unsupported::ByteArray)) |
1085 | 0 | } |
1086 | | |
1087 | 0 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
1088 | 0 | Ok(()) |
1089 | 0 | } |
1090 | | |
1091 | 0 | fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error> |
1092 | 0 | where |
1093 | 0 | T: Serialize, |
1094 | 0 | { |
1095 | 0 | value.serialize(self) |
1096 | 0 | } |
1097 | | |
1098 | 0 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
1099 | 0 | Ok(()) |
1100 | 0 | } |
1101 | | |
1102 | 0 | fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> { |
1103 | 0 | Err(Self::bad_type(Unsupported::UnitStruct)) |
1104 | 0 | } |
1105 | | |
1106 | 0 | fn serialize_unit_variant( |
1107 | 0 | self, |
1108 | 0 | _: &'static str, |
1109 | 0 | _: u32, |
1110 | 0 | _: &'static str, |
1111 | 0 | ) -> Result<Self::Ok, Self::Error> { |
1112 | 0 | Err(Self::bad_type(Unsupported::Enum)) |
1113 | 0 | } |
1114 | | |
1115 | 0 | fn serialize_newtype_struct<T: ?Sized>( |
1116 | 0 | self, |
1117 | 0 | _: &'static str, |
1118 | 0 | value: &T, |
1119 | 0 | ) -> Result<Self::Ok, Self::Error> |
1120 | 0 | where |
1121 | 0 | T: Serialize, |
1122 | 0 | { |
1123 | 0 | value.serialize(self) |
1124 | 0 | } |
1125 | | |
1126 | 0 | fn serialize_newtype_variant<T: ?Sized>( |
1127 | 0 | self, |
1128 | 0 | _: &'static str, |
1129 | 0 | _: u32, |
1130 | 0 | variant: &'static str, |
1131 | 0 | value: &T, |
1132 | 0 | ) -> Result<Self::Ok, Self::Error> |
1133 | 0 | where |
1134 | 0 | T: Serialize, |
1135 | 0 | { |
1136 | 0 | try!(self.0.serialize_key(variant)); |
1137 | 0 | self.0.serialize_value(value) |
1138 | 0 | } |
1139 | | |
1140 | 0 | fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
1141 | 0 | Err(Self::bad_type(Unsupported::Sequence)) |
1142 | 0 | } |
1143 | | |
1144 | 0 | fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> { |
1145 | 0 | Err(Self::bad_type(Unsupported::Tuple)) |
1146 | 0 | } |
1147 | | |
1148 | 0 | fn serialize_tuple_struct( |
1149 | 0 | self, |
1150 | 0 | _: &'static str, |
1151 | 0 | _: usize, |
1152 | 0 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
1153 | 0 | Err(Self::bad_type(Unsupported::TupleStruct)) |
1154 | 0 | } |
1155 | | |
1156 | 0 | fn serialize_tuple_variant( |
1157 | 0 | self, |
1158 | 0 | _: &'static str, |
1159 | 0 | _: u32, |
1160 | 0 | _: &'static str, |
1161 | 0 | _: usize, |
1162 | 0 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
1163 | 0 | Err(Self::bad_type(Unsupported::Enum)) |
1164 | 0 | } |
1165 | | |
1166 | 0 | fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
1167 | 0 | Ok(FlatMapSerializeMap(self.0)) |
1168 | 0 | } |
1169 | | |
1170 | 0 | fn serialize_struct( |
1171 | 0 | self, |
1172 | 0 | _: &'static str, |
1173 | 0 | _: usize, |
1174 | 0 | ) -> Result<Self::SerializeStruct, Self::Error> { |
1175 | 0 | Ok(FlatMapSerializeStruct(self.0)) |
1176 | 0 | } |
1177 | | |
1178 | 0 | fn serialize_struct_variant( |
1179 | 0 | self, |
1180 | 0 | _: &'static str, |
1181 | 0 | _: u32, |
1182 | 0 | inner_variant: &'static str, |
1183 | 0 | _: usize, |
1184 | 0 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
1185 | 0 | try!(self.0.serialize_key(inner_variant)); |
1186 | 0 | Ok(FlatMapSerializeStructVariantAsMapValue::new( |
1187 | 0 | self.0, |
1188 | 0 | inner_variant, |
1189 | 0 | )) |
1190 | 0 | } |
1191 | | } |
1192 | | |
1193 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1194 | | pub struct FlatMapSerializeMap<'a, M: 'a>(&'a mut M); |
1195 | | |
1196 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1197 | | impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M> |
1198 | | where |
1199 | | M: SerializeMap + 'a, |
1200 | | { |
1201 | | type Ok = (); |
1202 | | type Error = M::Error; |
1203 | | |
1204 | 0 | fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error> |
1205 | 0 | where |
1206 | 0 | T: Serialize, |
1207 | 0 | { |
1208 | 0 | self.0.serialize_key(key) |
1209 | 0 | } |
1210 | | |
1211 | 0 | fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> |
1212 | 0 | where |
1213 | 0 | T: Serialize, |
1214 | 0 | { |
1215 | 0 | self.0.serialize_value(value) |
1216 | 0 | } |
1217 | | |
1218 | 0 | fn serialize_entry<K: ?Sized, V: ?Sized>( |
1219 | 0 | &mut self, |
1220 | 0 | key: &K, |
1221 | 0 | value: &V, |
1222 | 0 | ) -> Result<(), Self::Error> |
1223 | 0 | where |
1224 | 0 | K: Serialize, |
1225 | 0 | V: Serialize, |
1226 | 0 | { |
1227 | 0 | self.0.serialize_entry(key, value) |
1228 | 0 | } |
1229 | | |
1230 | 0 | fn end(self) -> Result<(), Self::Error> { |
1231 | 0 | Ok(()) |
1232 | 0 | } |
1233 | | } |
1234 | | |
1235 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1236 | | pub struct FlatMapSerializeStruct<'a, M: 'a>(&'a mut M); |
1237 | | |
1238 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1239 | | impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M> |
1240 | | where |
1241 | | M: SerializeMap + 'a, |
1242 | | { |
1243 | | type Ok = (); |
1244 | | type Error = M::Error; |
1245 | | |
1246 | 0 | fn serialize_field<T: ?Sized>( |
1247 | 0 | &mut self, |
1248 | 0 | key: &'static str, |
1249 | 0 | value: &T, |
1250 | 0 | ) -> Result<(), Self::Error> |
1251 | 0 | where |
1252 | 0 | T: Serialize, |
1253 | 0 | { |
1254 | 0 | self.0.serialize_entry(key, value) |
1255 | 0 | } |
1256 | | |
1257 | 0 | fn end(self) -> Result<(), Self::Error> { |
1258 | 0 | Ok(()) |
1259 | 0 | } |
1260 | | } |
1261 | | |
1262 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1263 | | pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> { |
1264 | | map: &'a mut M, |
1265 | | name: &'static str, |
1266 | | fields: Vec<(&'static str, Content)>, |
1267 | | } |
1268 | | |
1269 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1270 | | impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M> |
1271 | | where |
1272 | | M: SerializeMap + 'a, |
1273 | | { |
1274 | 0 | fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> { |
1275 | 0 | FlatMapSerializeStructVariantAsMapValue { |
1276 | 0 | map: map, |
1277 | 0 | name: name, |
1278 | 0 | fields: Vec::new(), |
1279 | 0 | } |
1280 | 0 | } |
1281 | | } |
1282 | | |
1283 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1284 | | impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M> |
1285 | | where |
1286 | | M: SerializeMap + 'a, |
1287 | | { |
1288 | | type Ok = (); |
1289 | | type Error = M::Error; |
1290 | | |
1291 | 0 | fn serialize_field<T: ?Sized>( |
1292 | 0 | &mut self, |
1293 | 0 | key: &'static str, |
1294 | 0 | value: &T, |
1295 | 0 | ) -> Result<(), Self::Error> |
1296 | 0 | where |
1297 | 0 | T: Serialize, |
1298 | 0 | { |
1299 | 0 | let value = try!(value.serialize(ContentSerializer::<M::Error>::new())); |
1300 | 0 | self.fields.push((key, value)); |
1301 | 0 | Ok(()) |
1302 | 0 | } |
1303 | | |
1304 | 0 | fn end(self) -> Result<(), Self::Error> { |
1305 | 0 | try!(self |
1306 | 0 | .map |
1307 | 0 | .serialize_value(&Content::Struct(self.name, self.fields))); |
1308 | 0 | Ok(()) |
1309 | 0 | } |
1310 | | } |