]> git.proxmox.com Git - rustc.git/blob - src/vendor/serde/src/ser/content.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / vendor / serde / src / ser / content.rs
1 use core::marker::PhantomData;
2
3 #[cfg(all(not(feature = "std"), feature = "collections"))]
4 use collections::{String, Vec};
5
6 #[cfg(all(feature = "alloc", not(feature = "std")))]
7 use alloc::boxed::Box;
8
9 #[cfg(feature = "collections")]
10 use collections::borrow::ToOwned;
11
12 use ser::{self, Serialize, Serializer};
13
14 pub struct SerializeTupleVariantAsMapValue<M> {
15 map: M,
16 name: &'static str,
17 fields: Vec<Content>,
18 }
19
20 impl<M> SerializeTupleVariantAsMapValue<M> {
21 pub fn new(map: M, name: &'static str, len: usize) -> Self {
22 SerializeTupleVariantAsMapValue {
23 map: map,
24 name: name,
25 fields: Vec::with_capacity(len),
26 }
27 }
28 }
29
30 impl<M> ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue<M>
31 where M: ser::SerializeMap
32 {
33 type Ok = M::Ok;
34 type Error = M::Error;
35
36 fn serialize_field<T: ?Sized + Serialize>(&mut self,
37 value: &T)
38 -> Result<(), M::Error>
39 {
40 let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
41 self.fields.push(value);
42 Ok(())
43 }
44
45 fn end(mut self) -> Result<M::Ok, M::Error> {
46 try!(self.map.serialize_value(&Content::TupleStruct(self.name, self.fields)));
47 self.map.end()
48 }
49 }
50
51 pub struct SerializeStructVariantAsMapValue<M> {
52 map: M,
53 name: &'static str,
54 fields: Vec<(&'static str, Content)>,
55 }
56
57 impl<M> SerializeStructVariantAsMapValue<M> {
58 pub fn new(map: M, name: &'static str, len: usize) -> Self {
59 SerializeStructVariantAsMapValue {
60 map: map,
61 name: name,
62 fields: Vec::with_capacity(len),
63 }
64 }
65 }
66
67 impl<M> ser::SerializeStructVariant for SerializeStructVariantAsMapValue<M>
68 where M: ser::SerializeMap
69 {
70 type Ok = M::Ok;
71 type Error = M::Error;
72
73 fn serialize_field<T: ?Sized + Serialize>(&mut self,
74 key: &'static str,
75 value: &T)
76 -> Result<(), M::Error>
77 {
78 let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
79 self.fields.push((key, value));
80 Ok(())
81 }
82
83 fn end(mut self) -> Result<M::Ok, M::Error> {
84 try!(self.map.serialize_value(&Content::Struct(self.name, self.fields)));
85 self.map.end()
86 }
87 }
88
89 #[derive(Debug)]
90 enum Content {
91 Bool(bool),
92
93 U8(u8),
94 U16(u16),
95 U32(u32),
96 U64(u64),
97
98 I8(i8),
99 I16(i16),
100 I32(i32),
101 I64(i64),
102
103 F32(f32),
104 F64(f64),
105
106 Char(char),
107 String(String),
108 Bytes(Vec<u8>),
109
110 None,
111 Some(Box<Content>),
112
113 Unit,
114 UnitStruct(&'static str),
115 UnitVariant(&'static str, usize, &'static str),
116 NewtypeStruct(&'static str, Box<Content>),
117 NewtypeVariant(&'static str, usize, &'static str, Box<Content>),
118
119 Seq(Vec<Content>),
120 SeqFixedSize(Vec<Content>),
121 Tuple(Vec<Content>),
122 TupleStruct(&'static str, Vec<Content>),
123 TupleVariant(&'static str, usize, &'static str, Vec<Content>),
124 Map(Vec<(Content, Content)>),
125 Struct(&'static str, Vec<(&'static str, Content)>),
126 StructVariant(&'static str, usize, &'static str, Vec<(&'static str, Content)>),
127 }
128
129 impl Serialize for Content {
130 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
131 where S: Serializer
132 {
133 match *self {
134 Content::Bool(b) => serializer.serialize_bool(b),
135 Content::U8(u) => serializer.serialize_u8(u),
136 Content::U16(u) => serializer.serialize_u16(u),
137 Content::U32(u) => serializer.serialize_u32(u),
138 Content::U64(u) => serializer.serialize_u64(u),
139 Content::I8(i) => serializer.serialize_i8(i),
140 Content::I16(i) => serializer.serialize_i16(i),
141 Content::I32(i) => serializer.serialize_i32(i),
142 Content::I64(i) => serializer.serialize_i64(i),
143 Content::F32(f) => serializer.serialize_f32(f),
144 Content::F64(f) => serializer.serialize_f64(f),
145 Content::Char(c) => serializer.serialize_char(c),
146 Content::String(ref s) => serializer.serialize_str(s),
147 Content::Bytes(ref b) => serializer.serialize_bytes(b),
148 Content::None => serializer.serialize_none(),
149 Content::Some(ref c) => serializer.serialize_some(&**c),
150 Content::Unit => serializer.serialize_unit(),
151 Content::UnitStruct(n) => serializer.serialize_unit_struct(n),
152 Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v),
153 Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c),
154 Content::NewtypeVariant(n, i, v, ref c) => serializer.serialize_newtype_variant(n, i, v, &**c),
155 Content::Seq(ref elements) => elements.serialize(serializer),
156 Content::SeqFixedSize(ref elements) => {
157 use ser::SerializeSeq;
158 let mut seq = try!(serializer.serialize_seq_fixed_size(elements.len()));
159 for e in elements {
160 try!(seq.serialize_element(e));
161 }
162 seq.end()
163 }
164 Content::Tuple(ref elements) => {
165 use ser::SerializeTuple;
166 let mut tuple = try!(serializer.serialize_tuple(elements.len()));
167 for e in elements {
168 try!(tuple.serialize_element(e));
169 }
170 tuple.end()
171 }
172 Content::TupleStruct(n, ref fields) => {
173 use ser::SerializeTupleStruct;
174 let mut ts = try!(serializer.serialize_tuple_struct(n, fields.len()));
175 for f in fields {
176 try!(ts.serialize_field(f));
177 }
178 ts.end()
179 }
180 Content::TupleVariant(n, i, v, ref fields) => {
181 use ser::SerializeTupleVariant;
182 let mut tv = try!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
183 for f in fields {
184 try!(tv.serialize_field(f));
185 }
186 tv.end()
187 }
188 Content::Map(ref entries) => {
189 use ser::SerializeMap;
190 let mut map = try!(serializer.serialize_map(Some(entries.len())));
191 for &(ref k, ref v) in entries {
192 try!(map.serialize_entry(k, v));
193 }
194 map.end()
195 }
196 Content::Struct(n, ref fields) => {
197 use ser::SerializeStruct;
198 let mut s = try!(serializer.serialize_struct(n, fields.len()));
199 for &(k, ref v) in fields {
200 try!(s.serialize_field(k, v));
201 }
202 s.end()
203 }
204 Content::StructVariant(n, i, v, ref fields) => {
205 use ser::SerializeStructVariant;
206 let mut sv = try!(serializer.serialize_struct_variant(n, i, v, fields.len()));
207 for &(k, ref v) in fields {
208 try!(sv.serialize_field(k, v));
209 }
210 sv.end()
211 }
212 }
213 }
214 }
215
216 struct ContentSerializer<E> {
217 error: PhantomData<E>,
218 }
219
220 impl<E> ContentSerializer<E> {
221 fn new() -> Self {
222 ContentSerializer {
223 error: PhantomData,
224 }
225 }
226 }
227
228 impl<E> Serializer for ContentSerializer<E>
229 where E: ser::Error
230 {
231 type Ok = Content;
232 type Error = E;
233
234 type SerializeSeq = SerializeSeq<E>;
235 type SerializeTuple = SerializeTuple<E>;
236 type SerializeTupleStruct = SerializeTupleStruct<E>;
237 type SerializeTupleVariant = SerializeTupleVariant<E>;
238 type SerializeMap = SerializeMap<E>;
239 type SerializeStruct = SerializeStruct<E>;
240 type SerializeStructVariant = SerializeStructVariant<E>;
241
242 fn serialize_bool(self, v: bool) -> Result<Content, E> {
243 Ok(Content::Bool(v))
244 }
245
246 fn serialize_i8(self, v: i8) -> Result<Content, E> {
247 Ok(Content::I8(v))
248 }
249
250 fn serialize_i16(self, v: i16) -> Result<Content, E> {
251 Ok(Content::I16(v))
252 }
253
254 fn serialize_i32(self, v: i32) -> Result<Content, E> {
255 Ok(Content::I32(v))
256 }
257
258 fn serialize_i64(self, v: i64) -> Result<Content, E> {
259 Ok(Content::I64(v))
260 }
261
262 fn serialize_u8(self, v: u8) -> Result<Content, E> {
263 Ok(Content::U8(v))
264 }
265
266 fn serialize_u16(self, v: u16) -> Result<Content, E> {
267 Ok(Content::U16(v))
268 }
269
270 fn serialize_u32(self, v: u32) -> Result<Content, E> {
271 Ok(Content::U32(v))
272 }
273
274 fn serialize_u64(self, v: u64) -> Result<Content, E> {
275 Ok(Content::U64(v))
276 }
277
278 fn serialize_f32(self, v: f32) -> Result<Content, E> {
279 Ok(Content::F32(v))
280 }
281
282 fn serialize_f64(self, v: f64) -> Result<Content, E> {
283 Ok(Content::F64(v))
284 }
285
286 fn serialize_char(self, v: char) -> Result<Content, E> {
287 Ok(Content::Char(v))
288 }
289
290 fn serialize_str(self, value: &str) -> Result<Content, E> {
291 Ok(Content::String(value.to_owned()))
292 }
293
294 fn serialize_bytes(self, value: &[u8]) -> Result<Content, E> {
295 Ok(Content::Bytes(value.to_owned()))
296 }
297
298 fn serialize_none(self) -> Result<Content, E> {
299 Ok(Content::None)
300 }
301
302 fn serialize_some<T: ?Sized + Serialize>(self,
303 value: &T)
304 -> Result<Content, E> {
305 Ok(Content::Some(Box::new(try!(value.serialize(self)))))
306 }
307
308 fn serialize_unit(self) -> Result<Content, E> {
309 Ok(Content::Unit)
310 }
311
312 fn serialize_unit_struct(self,
313 name: &'static str)
314 -> Result<Content, E> {
315 Ok(Content::UnitStruct(name))
316 }
317
318 fn serialize_unit_variant(self,
319 name: &'static str,
320 variant_index: usize,
321 variant: &'static str)
322 -> Result<Content, E> {
323 Ok(Content::UnitVariant(name, variant_index, variant))
324 }
325
326 fn serialize_newtype_struct<T: ?Sized + Serialize>(self,
327 name: &'static str,
328 value: &T)
329 -> Result<Content, E> {
330 Ok(Content::NewtypeStruct(name, Box::new(try!(value.serialize(self)))))
331 }
332
333 fn serialize_newtype_variant<T: ?Sized + Serialize>(self,
334 name: &'static str,
335 variant_index: usize,
336 variant: &'static str,
337 value: &T)
338 -> Result<Content, E> {
339 Ok(Content::NewtypeVariant(name, variant_index, variant, Box::new(try!(value.serialize(self)))))
340 }
341
342 fn serialize_seq(self,
343 len: Option<usize>)
344 -> Result<Self::SerializeSeq, E> {
345 Ok(SerializeSeq {
346 fixed_size: false,
347 elements: Vec::with_capacity(len.unwrap_or(0)),
348 error: PhantomData,
349 })
350 }
351
352 fn serialize_seq_fixed_size(self,
353 size: usize)
354 -> Result<Self::SerializeSeq, E> {
355 Ok(SerializeSeq {
356 fixed_size: true,
357 elements: Vec::with_capacity(size),
358 error: PhantomData,
359 })
360 }
361
362 fn serialize_tuple(self,
363 len: usize)
364 -> Result<Self::SerializeTuple, E> {
365 Ok(SerializeTuple {
366 elements: Vec::with_capacity(len),
367 error: PhantomData,
368 })
369 }
370
371 fn serialize_tuple_struct(self,
372 name: &'static str,
373 len: usize)
374 -> Result<Self::SerializeTupleStruct, E> {
375 Ok(SerializeTupleStruct {
376 name: name,
377 fields: Vec::with_capacity(len),
378 error: PhantomData,
379 })
380 }
381
382 fn serialize_tuple_variant(self,
383 name: &'static str,
384 variant_index: usize,
385 variant: &'static str,
386 len: usize)
387 -> Result<Self::SerializeTupleVariant, E> {
388 Ok(SerializeTupleVariant {
389 name: name,
390 variant_index: variant_index,
391 variant: variant,
392 fields: Vec::with_capacity(len),
393 error: PhantomData,
394 })
395 }
396
397 fn serialize_map(self,
398 len: Option<usize>)
399 -> Result<Self::SerializeMap, E> {
400 Ok(SerializeMap {
401 entries: Vec::with_capacity(len.unwrap_or(0)),
402 key: None,
403 error: PhantomData,
404 })
405 }
406
407 fn serialize_struct(self,
408 name: &'static str,
409 len: usize)
410 -> Result<Self::SerializeStruct, E> {
411 Ok(SerializeStruct {
412 name: name,
413 fields: Vec::with_capacity(len),
414 error: PhantomData,
415 })
416 }
417
418 fn serialize_struct_variant(self,
419 name: &'static str,
420 variant_index: usize,
421 variant: &'static str,
422 len: usize)
423 -> Result<Self::SerializeStructVariant, E> {
424 Ok(SerializeStructVariant {
425 name: name,
426 variant_index: variant_index,
427 variant: variant,
428 fields: Vec::with_capacity(len),
429 error: PhantomData,
430 })
431 }
432 }
433
434 struct SerializeSeq<E> {
435 fixed_size: bool,
436 elements: Vec<Content>,
437 error: PhantomData<E>,
438 }
439
440 impl<E> ser::SerializeSeq for SerializeSeq<E>
441 where E: ser::Error
442 {
443 type Ok = Content;
444 type Error = E;
445
446 fn serialize_element<T: ?Sized + Serialize>(&mut self,
447 value: &T)
448 -> Result<(), E> {
449 let value = try!(value.serialize(ContentSerializer::<E>::new()));
450 self.elements.push(value);
451 Ok(())
452 }
453
454 fn end(self) -> Result<Content, E> {
455 Ok(if self.fixed_size {
456 Content::SeqFixedSize(self.elements)
457 } else {
458 Content::Seq(self.elements)
459 })
460 }
461 }
462
463 struct SerializeTuple<E> {
464 elements: Vec<Content>,
465 error: PhantomData<E>,
466 }
467
468 impl<E> ser::SerializeTuple for SerializeTuple<E>
469 where E: ser::Error
470 {
471 type Ok = Content;
472 type Error = E;
473
474 fn serialize_element<T: ?Sized + Serialize>(&mut self,
475 value: &T)
476 -> Result<(), E> {
477 let value = try!(value.serialize(ContentSerializer::<E>::new()));
478 self.elements.push(value);
479 Ok(())
480 }
481
482 fn end(self) -> Result<Content, E> {
483 Ok(Content::Tuple(self.elements))
484 }
485 }
486
487 struct SerializeTupleStruct<E> {
488 name: &'static str,
489 fields: Vec<Content>,
490 error: PhantomData<E>,
491 }
492
493 impl<E> ser::SerializeTupleStruct for SerializeTupleStruct<E>
494 where E: ser::Error
495 {
496 type Ok = Content;
497 type Error = E;
498
499 fn serialize_field<T: ?Sized + Serialize>(&mut self,
500 value: &T)
501 -> Result<(), E> {
502 let value = try!(value.serialize(ContentSerializer::<E>::new()));
503 self.fields.push(value);
504 Ok(())
505 }
506
507 fn end(self) -> Result<Content, E> {
508 Ok(Content::TupleStruct(self.name, self.fields))
509 }
510 }
511
512 struct SerializeTupleVariant<E> {
513 name: &'static str,
514 variant_index: usize,
515 variant: &'static str,
516 fields: Vec<Content>,
517 error: PhantomData<E>,
518 }
519
520 impl<E> ser::SerializeTupleVariant for SerializeTupleVariant<E>
521 where E: ser::Error
522 {
523 type Ok = Content;
524 type Error = E;
525
526 fn serialize_field<T: ?Sized + Serialize>(&mut self,
527 value: &T)
528 -> Result<(), E> {
529 let value = try!(value.serialize(ContentSerializer::<E>::new()));
530 self.fields.push(value);
531 Ok(())
532 }
533
534 fn end(self) -> Result<Content, E> {
535 Ok(Content::TupleVariant(self.name, self.variant_index, self.variant, self.fields))
536 }
537 }
538
539 struct SerializeMap<E> {
540 entries: Vec<(Content, Content)>,
541 key: Option<Content>,
542 error: PhantomData<E>,
543 }
544
545 impl<E> ser::SerializeMap for SerializeMap<E>
546 where E: ser::Error
547 {
548 type Ok = Content;
549 type Error = E;
550
551 fn serialize_key<T: ?Sized + Serialize>(&mut self,
552 key: &T)
553 -> Result<(), E> {
554 let key = try!(key.serialize(ContentSerializer::<E>::new()));
555 self.key = Some(key);
556 Ok(())
557 }
558
559 fn serialize_value<T: ?Sized + Serialize>(&mut self,
560 value: &T)
561 -> Result<(), E> {
562 let key = self.key.take().expect("serialize_value called before serialize_key");
563 let value = try!(value.serialize(ContentSerializer::<E>::new()));
564 self.entries.push((key, value));
565 Ok(())
566 }
567
568 fn end(self) -> Result<Content, E> {
569 Ok(Content::Map(self.entries))
570 }
571
572 fn serialize_entry<K: ?Sized + Serialize, V: ?Sized + Serialize>(&mut self,
573 key: &K,
574 value: &V)
575 -> Result<(), E> {
576 let key = try!(key.serialize(ContentSerializer::<E>::new()));
577 let value = try!(value.serialize(ContentSerializer::<E>::new()));
578 self.entries.push((key, value));
579 Ok(())
580 }
581 }
582
583 struct SerializeStruct<E> {
584 name: &'static str,
585 fields: Vec<(&'static str, Content)>,
586 error: PhantomData<E>,
587 }
588
589 impl<E> ser::SerializeStruct for SerializeStruct<E>
590 where E: ser::Error
591 {
592 type Ok = Content;
593 type Error = E;
594
595 fn serialize_field<T: ?Sized + Serialize>(&mut self,
596 key: &'static str,
597 value: &T)
598 -> Result<(), E> {
599 let value = try!(value.serialize(ContentSerializer::<E>::new()));
600 self.fields.push((key, value));
601 Ok(())
602 }
603
604 fn end(self) -> Result<Content, E> {
605 Ok(Content::Struct(self.name, self.fields))
606 }
607 }
608
609 struct SerializeStructVariant<E> {
610 name: &'static str,
611 variant_index: usize,
612 variant: &'static str,
613 fields: Vec<(&'static str, Content)>,
614 error: PhantomData<E>,
615 }
616
617 impl<E> ser::SerializeStructVariant for SerializeStructVariant<E>
618 where E: ser::Error
619 {
620 type Ok = Content;
621 type Error = E;
622
623 fn serialize_field<T: ?Sized + Serialize>(&mut self,
624 key: &'static str,
625 value: &T)
626 -> Result<(), E> {
627 let value = try!(value.serialize(ContentSerializer::<E>::new()));
628 self.fields.push((key, value));
629 Ok(())
630 }
631
632 fn end(self) -> Result<Content, E> {
633 Ok(Content::StructVariant(self.name, self.variant_index, self.variant, self.fields))
634 }
635 }