2015-09-24 09:03:36 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::error;
|
2016-04-24 12:59:57 -05:00
|
|
|
|
|
|
|
extern crate serde;
|
|
|
|
use self::serde::Serialize;
|
|
|
|
use self::serde::bytes::{ByteBuf, Bytes};
|
2015-03-26 01:03:01 -05:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
struct Error;
|
|
|
|
|
2016-01-28 12:41:21 -06:00
|
|
|
impl serde::ser::Error for Error {
|
2016-02-26 23:12:16 -06:00
|
|
|
fn custom<T: Into<String>>(_: T) -> Error { Error }
|
2016-01-28 12:41:21 -06:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:03:01 -05:00
|
|
|
impl serde::de::Error for Error {
|
2016-02-26 23:12:16 -06:00
|
|
|
fn custom<T: Into<String>>(_: T) -> Error { Error }
|
2015-03-26 01:03:01 -05:00
|
|
|
|
2015-08-06 19:47:44 -05:00
|
|
|
fn end_of_stream() -> Error { Error }
|
2015-03-26 01:03:01 -05:00
|
|
|
}
|
|
|
|
|
2015-09-24 09:03:36 -05:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
formatter.write_str(format!("{:?}", self).as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for Error {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"Serde Deserialization Error"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&error::Error> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:03:01 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct BytesSerializer {
|
|
|
|
bytes: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BytesSerializer {
|
|
|
|
fn new(bytes: Vec<u8>) -> Self {
|
|
|
|
BytesSerializer {
|
|
|
|
bytes: bytes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:44:19 -05:00
|
|
|
struct SeqSerializer;
|
|
|
|
|
|
|
|
impl serde::ser::SeqSerializer for SeqSerializer {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_elt<S: ?Sized, T>(&mut self, _serializer: &mut S, _value: T) -> Result<(), Self::Error>
|
|
|
|
where T: Serialize, S: serde::ser::Serializer<Error = Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drop<S: ?Sized>(self, _serializer: &mut S) -> Result<(), Self::Error> where S: serde::ser::Serializer<Error = Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MapSerializer;
|
|
|
|
|
|
|
|
impl serde::ser::MapSerializer for MapSerializer {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn serialize_elt<S: ?Sized, K, V>(&mut self, _serializer: &mut S, _key: K, _value: V) -> Result<(), Self::Error>
|
|
|
|
where K: Serialize,
|
|
|
|
V: Serialize,
|
|
|
|
S: serde::ser::Serializer<Error = Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drop<S: ?Sized>(self, _serializer: &mut S) -> Result<(), Self::Error> where S: serde::ser::Serializer<Error = Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:03:01 -05:00
|
|
|
impl serde::Serializer for BytesSerializer {
|
|
|
|
type Error = Error;
|
2016-07-12 08:44:19 -05:00
|
|
|
type SeqSerializer = SeqSerializer;
|
|
|
|
type MapSerializer = MapSerializer;
|
2015-03-26 01:03:01 -05:00
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_unit(&mut self) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_bool(&mut self, _v: bool) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_i64(&mut self, _v: i64) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_u64(&mut self, _v: u64) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_f32(&mut self, _v: f32) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_f64(&mut self, _v: f64) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_char(&mut self, _v: char) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_str(&mut self, _v: &str) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_none(&mut self) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_some<V>(&mut self, _value: V) -> Result<(), Error>
|
2015-03-26 01:03:01 -05:00
|
|
|
where V: serde::Serialize,
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:44:19 -05:00
|
|
|
fn serialize_seq<'a>(&'a mut self, _len: Option<usize>) -> Result<serde::ser::SeqHelper<'a, Self>, Error>
|
2015-03-26 01:03:01 -05:00
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_seq_elt<T>(&mut self, _value: T) -> Result<(), Error>
|
2015-03-26 01:03:01 -05:00
|
|
|
where T: serde::Serialize
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:44:19 -05:00
|
|
|
fn serialize_seq_end(&mut self) -> Result<(), Error>
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_map<'a>(&mut self, _: Option<usize>) -> Result<serde::ser::MapHelper<'a, Self>, Error>
|
2015-03-26 01:03:01 -05:00
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_map_elt<K, V>(&mut self, _key: K, _value: V) -> Result<(), Error>
|
2015-03-26 01:03:01 -05:00
|
|
|
where K: serde::Serialize,
|
|
|
|
V: serde::Serialize,
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2016-07-12 08:44:19 -05:00
|
|
|
fn serialize_map_end(&mut self) -> Result<(), Error>
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn serialize_bytes(&mut self, bytes: &[u8]) -> Result<(), Error> {
|
2015-03-26 01:03:01 -05:00
|
|
|
assert_eq!(self.bytes, bytes);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct BytesDeserializer {
|
|
|
|
bytes: Option<Vec<u8>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BytesDeserializer {
|
|
|
|
fn new(bytes: Vec<u8>) -> Self {
|
|
|
|
BytesDeserializer {
|
|
|
|
bytes: Some(bytes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl serde::Deserializer for BytesDeserializer {
|
|
|
|
type Error = Error;
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn deserialize<V>(&mut self, _visitor: V) -> Result<V::Value, Error>
|
2015-03-26 01:03:01 -05:00
|
|
|
where V: serde::de::Visitor,
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
2015-10-19 06:06:30 -05:00
|
|
|
fn deserialize_bytes<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
2015-03-26 01:03:01 -05:00
|
|
|
where V: serde::de::Visitor,
|
|
|
|
{
|
|
|
|
visitor.visit_byte_buf(self.bytes.take().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bytes_ser_bytes() {
|
|
|
|
let buf = vec![];
|
|
|
|
let bytes = Bytes::from(&buf);
|
|
|
|
let mut ser = BytesSerializer::new(vec![]);
|
|
|
|
bytes.serialize(&mut ser).unwrap();
|
|
|
|
|
|
|
|
let buf = vec![1, 2, 3];
|
|
|
|
let bytes = Bytes::from(&buf);
|
|
|
|
let mut ser = BytesSerializer::new(vec![1, 2, 3]);
|
|
|
|
bytes.serialize(&mut ser).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_byte_buf_de_bytes() {
|
|
|
|
let mut de = BytesDeserializer::new(vec![]);
|
|
|
|
let bytes = serde::Deserialize::deserialize(&mut de);
|
|
|
|
assert_eq!(bytes, Ok(ByteBuf::new()));
|
|
|
|
|
|
|
|
let mut de = BytesDeserializer::new(vec![1, 2, 3]);
|
|
|
|
let bytes = serde::Deserialize::deserialize(&mut de);
|
|
|
|
assert_eq!(bytes, Ok(ByteBuf::from(vec![1, 2, 3])));
|
|
|
|
}
|