2017-04-13 19:34:42 -05:00
|
|
|
// Copyright 2017 Serde Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
use serde::{ser, Serialize};
|
2016-06-28 23:47:54 -05:00
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
use token::Token;
|
|
|
|
|
2017-02-27 19:09:31 -06:00
|
|
|
/// A `Serializer` that ensures that a value serializes to a given list of tokens.
|
2017-04-13 13:04:16 -05:00
|
|
|
#[derive(Debug)]
|
2017-04-05 02:27:30 -05:00
|
|
|
pub struct Serializer<'a> {
|
|
|
|
tokens: &'a [Token],
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-04-05 02:27:30 -05:00
|
|
|
impl<'a> Serializer<'a> {
|
2017-02-27 19:09:31 -06:00
|
|
|
/// Creates the serializer.
|
2017-04-05 02:27:30 -05:00
|
|
|
pub fn new(tokens: &'a [Token]) -> Self {
|
2017-04-13 14:28:23 -05:00
|
|
|
Serializer { tokens: tokens }
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 19:09:31 -06:00
|
|
|
/// Pulls the next token off of the serializer, ignoring it.
|
2017-04-19 16:12:56 -05:00
|
|
|
fn next_token(&mut self) -> Option<Token> {
|
2017-04-05 03:06:42 -05:00
|
|
|
if let Some((&first, rest)) = self.tokens.split_first() {
|
2017-04-05 02:27:30 -05:00
|
|
|
self.tokens = rest;
|
|
|
|
Some(first)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2017-04-19 12:41:58 -05:00
|
|
|
|
|
|
|
pub fn remaining(&self) -> usize {
|
|
|
|
self.tokens.len()
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-04-05 02:17:50 -05:00
|
|
|
macro_rules! assert_next_token {
|
2017-04-19 16:12:56 -05:00
|
|
|
($ser:expr, $expected:ident) => {
|
|
|
|
assert_next_token!($ser, $expected, Token::$expected, true);
|
2017-04-05 03:06:42 -05:00
|
|
|
};
|
2017-04-19 16:12:56 -05:00
|
|
|
($ser:expr, $expected:ident($v:expr)) => {
|
|
|
|
assert_next_token!($ser, $expected, Token::$expected(v), v == $v);
|
2017-04-05 03:06:42 -05:00
|
|
|
};
|
2017-04-19 16:12:56 -05:00
|
|
|
($ser:expr, $expected:ident { $($k:ident),* }) => {
|
2017-04-19 14:20:17 -05:00
|
|
|
let compare = ($($k,)*);
|
2017-04-19 16:12:56 -05:00
|
|
|
assert_next_token!($ser, $expected, Token::$expected { $($k),* }, ($($k,)*) == compare);
|
2017-04-05 03:06:42 -05:00
|
|
|
};
|
2017-04-19 16:12:56 -05:00
|
|
|
($ser:expr, $expected:ident, $pat:pat, $guard:expr) => {
|
2017-04-10 21:41:32 -05:00
|
|
|
match $ser.next_token() {
|
2017-04-19 14:20:17 -05:00
|
|
|
Some($pat) if $guard => {}
|
2017-04-05 02:17:50 -05:00
|
|
|
Some(other) => {
|
2017-04-19 16:12:56 -05:00
|
|
|
panic!("expected Token::{} but serialized as {}",
|
|
|
|
stringify!($expected), other);
|
2017-04-05 02:17:50 -05:00
|
|
|
}
|
|
|
|
None => {
|
2017-04-05 03:06:42 -05:00
|
|
|
panic!("expected Token::{} after end of serialized tokens",
|
2017-04-19 16:12:56 -05:00
|
|
|
stringify!($expected));
|
2017-04-05 02:17:50 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-05 03:06:42 -05:00
|
|
|
};
|
2017-04-05 02:17:50 -05:00
|
|
|
}
|
|
|
|
|
2017-04-05 02:27:30 -05:00
|
|
|
impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
2016-06-28 23:47:54 -05:00
|
|
|
type Error = Error;
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
type SerializeSeq = Self;
|
|
|
|
type SerializeTuple = Self;
|
|
|
|
type SerializeTupleStruct = Self;
|
2017-04-19 16:12:56 -05:00
|
|
|
type SerializeTupleVariant = Variant<'s, 'a>;
|
2017-01-14 17:36:51 -06:00
|
|
|
type SerializeMap = Self;
|
|
|
|
type SerializeStruct = Self;
|
2017-04-19 16:12:56 -05:00
|
|
|
type SerializeStructVariant = Variant<'s, 'a>;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_bool(self, v: bool) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, Bool(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_i8(self, v: i8) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, I8(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_i16(self, v: i16) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, I16(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_i32(self, v: i32) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, I32(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_i64(self, v: i64) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, I64(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_u8(self, v: u8) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, U8(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_u16(self, v: u16) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, U16(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_u32(self, v: u32) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, U32(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_u64(self, v: u64) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, U64(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_f32(self, v: f32) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, F32(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_f64(self, v: f64) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, F64(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_char(self, v: char) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, Char(v));
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_str(self, v: &str) -> Result<(), Error> {
|
2017-04-19 16:12:56 -05:00
|
|
|
match self.tokens.first() {
|
|
|
|
Some(&Token::BorrowedStr(_)) => assert_next_token!(self, BorrowedStr(v)),
|
|
|
|
Some(&Token::String(_)) => assert_next_token!(self, String(v)),
|
|
|
|
_ => assert_next_token!(self, Str(v)),
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:12:56 -05:00
|
|
|
fn serialize_bytes(self, v: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
match self.tokens.first() {
|
|
|
|
Some(&Token::BorrowedBytes(_)) => assert_next_token!(self, BorrowedBytes(v)),
|
|
|
|
Some(&Token::ByteBuf(_)) => assert_next_token!(self, ByteBuf(v)),
|
|
|
|
_ => assert_next_token!(self, Bytes(v)),
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_unit(self) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, Unit);
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-04-05 02:17:50 -05:00
|
|
|
fn serialize_unit_struct(self, name: &'static str) -> Result<(), Error> {
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, UnitStruct { name });
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_unit_variant(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
) -> Result<(), Error> {
|
2017-04-19 14:20:17 -05:00
|
|
|
if self.tokens.first() == Some(&Token::Enum { name }) {
|
2017-04-05 03:15:42 -05:00
|
|
|
self.next_token();
|
|
|
|
assert_next_token!(self, Str(variant));
|
|
|
|
assert_next_token!(self, Unit);
|
|
|
|
} else {
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, UnitVariant { name, variant });
|
2017-04-05 03:15:42 -05:00
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-02-12 23:59:04 -06:00
|
|
|
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<(), Error>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, NewtypeStruct { name });
|
2016-06-28 23:47:54 -05:00
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_newtype_variant<T: ?Sized>(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-04-19 14:20:17 -05:00
|
|
|
if self.tokens.first() == Some(&Token::Enum { name }) {
|
2017-04-05 03:15:42 -05:00
|
|
|
self.next_token();
|
|
|
|
assert_next_token!(self, Str(variant));
|
|
|
|
} else {
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, NewtypeVariant { name, variant });
|
2017-04-05 03:15:42 -05:00
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_none(self) -> Result<(), Error> {
|
2017-04-05 12:30:46 -05:00
|
|
|
assert_next_token!(self, None);
|
2016-07-12 09:16:48 -05:00
|
|
|
Ok(())
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<(), Error>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-04-05 12:30:46 -05:00
|
|
|
assert_next_token!(self, Some);
|
2016-06-28 23:47:54 -05:00
|
|
|
value.serialize(self)
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_seq(self, len: Option<usize>) -> Result<Self, Error> {
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, Seq { len });
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(self)
|
2016-07-12 08:44:19 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_tuple(self, len: usize) -> Result<Self, Error> {
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, Tuple { len });
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, TupleStruct { name, len });
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_tuple_variant(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
len: usize,
|
2017-04-19 16:12:56 -05:00
|
|
|
) -> Result<Self::SerializeTupleVariant, Error> {
|
|
|
|
if self.tokens.first() == Some(&Token::Enum { name }) {
|
|
|
|
self.next_token();
|
|
|
|
assert_next_token!(self, Str(variant));
|
|
|
|
let len = Some(len);
|
|
|
|
assert_next_token!(self, Seq { len });
|
|
|
|
Ok(Variant { ser: self, end: Token::SeqEnd })
|
|
|
|
} else {
|
|
|
|
assert_next_token!(self, TupleVariant { name, variant, len });
|
|
|
|
Ok(Variant { ser: self, end: Token::TupleVariantEnd })
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:36:51 -06:00
|
|
|
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, Map { len });
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(self)
|
2016-07-12 08:44:19 -05:00
|
|
|
}
|
|
|
|
|
2017-04-05 02:17:50 -05:00
|
|
|
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self, Error> {
|
2017-04-19 14:20:17 -05:00
|
|
|
assert_next_token!(self, Struct { name, len });
|
2017-01-14 17:36:51 -06:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_struct_variant(
|
|
|
|
self,
|
|
|
|
name: &'static str,
|
|
|
|
_variant_index: u32,
|
|
|
|
variant: &'static str,
|
|
|
|
len: usize,
|
2017-04-19 16:12:56 -05:00
|
|
|
) -> Result<Self::SerializeStructVariant, Error> {
|
|
|
|
if self.tokens.first() == Some(&Token::Enum { name }) {
|
|
|
|
self.next_token();
|
|
|
|
assert_next_token!(self, Str(variant));
|
|
|
|
let len = Some(len);
|
|
|
|
assert_next_token!(self, Map { len });
|
|
|
|
Ok(Variant { ser: self, end: Token::MapEnd })
|
|
|
|
} else {
|
|
|
|
assert_next_token!(self, StructVariant { name, variant, len });
|
|
|
|
Ok(Variant { ser: self, end: Token::StructVariantEnd })
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
|
|
|
|
2017-04-19 16:12:56 -05:00
|
|
|
pub struct Variant<'s, 'a: 's> {
|
|
|
|
ser: &'s mut Serializer<'a>,
|
|
|
|
end: Token,
|
|
|
|
}
|
|
|
|
|
2017-04-05 02:27:30 -05:00
|
|
|
impl<'s, 'a> ser::SerializeSeq for &'s mut Serializer<'a> {
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
value.serialize(&mut **self)
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, SeqEnd);
|
2016-07-12 09:16:48 -05:00
|
|
|
Ok(())
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
|
|
|
|
2017-04-05 02:27:30 -05:00
|
|
|
impl<'s, 'a> ser::SerializeTuple for &'s mut Serializer<'a> {
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
value.serialize(&mut **self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, TupleEnd);
|
2016-07-12 08:44:19 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-04-05 02:27:30 -05:00
|
|
|
impl<'s, 'a> ser::SerializeTupleStruct for &'s mut Serializer<'a> {
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2016-07-12 08:44:19 -05:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
value.serialize(&mut **self)
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, TupleStructEnd);
|
2016-07-12 09:16:48 -05:00
|
|
|
Ok(())
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
|
|
|
|
2017-04-19 16:12:56 -05:00
|
|
|
impl<'s, 'a> ser::SerializeTupleVariant for Variant<'s, 'a> {
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2016-06-28 23:47:54 -05:00
|
|
|
{
|
2017-04-19 16:12:56 -05:00
|
|
|
value.serialize(&mut *self.ser)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Error> {
|
2017-04-19 16:12:56 -05:00
|
|
|
match self.end {
|
|
|
|
Token::TupleVariantEnd => assert_next_token!(self.ser, TupleVariantEnd),
|
|
|
|
Token::SeqEnd => assert_next_token!(self.ser, SeqEnd),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2016-07-12 08:44:19 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-04-05 02:27:30 -05:00
|
|
|
impl<'s, 'a> ser::SerializeMap for &'s mut Serializer<'a> {
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-02-12 23:59:04 -06:00
|
|
|
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2017-02-12 23:59:04 -06:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
key.serialize(&mut **self)
|
2016-07-27 14:12:07 -05:00
|
|
|
}
|
|
|
|
|
2017-02-12 23:59:04 -06:00
|
|
|
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
T: Serialize,
|
2017-02-12 23:59:04 -06:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
value.serialize(&mut **self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Self::Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, MapEnd);
|
2016-07-12 08:44:19 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-04-05 02:27:30 -05:00
|
|
|
impl<'s, 'a> ser::SerializeStruct for &'s mut Serializer<'a> {
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_field<T: ?Sized>(
|
|
|
|
&mut self,
|
|
|
|
key: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where
|
|
|
|
T: Serialize,
|
2017-02-12 23:59:04 -06:00
|
|
|
{
|
2017-01-14 17:36:51 -06:00
|
|
|
try!(key.serialize(&mut **self));
|
|
|
|
value.serialize(&mut **self)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Self::Error> {
|
2017-04-05 03:06:42 -05:00
|
|
|
assert_next_token!(self, StructEnd);
|
2016-07-12 08:44:19 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2017-01-14 17:36:51 -06:00
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-04-19 16:12:56 -05:00
|
|
|
impl<'s, 'a> ser::SerializeStructVariant for Variant<'s, 'a> {
|
2017-01-14 17:36:51 -06:00
|
|
|
type Ok = ();
|
|
|
|
type Error = Error;
|
2016-06-28 23:47:54 -05:00
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_field<T: ?Sized>(
|
|
|
|
&mut self,
|
|
|
|
key: &'static str,
|
|
|
|
value: &T,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where
|
|
|
|
T: Serialize,
|
2017-02-12 23:59:04 -06:00
|
|
|
{
|
2017-04-19 16:12:56 -05:00
|
|
|
try!(key.serialize(&mut *self.ser));
|
|
|
|
value.serialize(&mut *self.ser)
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|
2016-07-12 08:44:19 -05:00
|
|
|
|
2017-01-14 23:39:37 -06:00
|
|
|
fn end(self) -> Result<(), Self::Error> {
|
2017-04-19 16:12:56 -05:00
|
|
|
match self.end {
|
|
|
|
Token::StructVariantEnd => assert_next_token!(self.ser, StructVariantEnd),
|
|
|
|
Token::MapEnd => assert_next_token!(self.ser, MapEnd),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2016-07-12 08:44:19 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-28 23:47:54 -05:00
|
|
|
}
|