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