2015-05-18 01:23:12 -05:00
|
|
|
use serde;
|
2015-09-24 09:03:36 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::error;
|
2015-03-26 01:03:01 -05:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde::bytes::{ByteBuf, Bytes};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
struct Error;
|
|
|
|
|
|
|
|
impl serde::de::Error for Error {
|
2015-08-07 10:08:56 -05:00
|
|
|
fn syntax(_: &str) -> 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-08-06 19:47:44 -05:00
|
|
|
fn unknown_field(_field: &str) -> Error { Error }
|
2015-04-12 02:30:20 -05:00
|
|
|
|
2015-08-06 19:47:44 -05:00
|
|
|
fn missing_field(_field: &'static str) -> 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl serde::Serializer for BytesSerializer {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn visit_unit(&mut self) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bool(&mut self, _v: bool) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_i64(&mut self, _v: i64) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_u64(&mut self, _v: u64) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_f32(&mut self, _v: f32) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_f64(&mut self, _v: f64) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_char(&mut self, _v: char) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str(&mut self, _v: &str) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_none(&mut self) -> Result<(), Error> {
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_some<V>(&mut self, _value: V) -> Result<(), Error>
|
|
|
|
where V: serde::Serialize,
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq<V>(&mut self, _visitor: V) -> Result<(), Error>
|
|
|
|
where V: serde::ser::SeqVisitor,
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_seq_elt<T>(&mut self, _value: T) -> Result<(), Error>
|
|
|
|
where T: serde::Serialize
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map<V>(&mut self, _visitor: V) -> Result<(), Error>
|
|
|
|
where V: serde::ser::MapVisitor,
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_map_elt<K, V>(&mut self, _key: K, _value: V) -> Result<(), Error>
|
|
|
|
where K: serde::Serialize,
|
|
|
|
V: serde::Serialize,
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes(&mut self, bytes: &[u8]) -> Result<(), Error> {
|
|
|
|
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;
|
|
|
|
|
|
|
|
fn visit<V>(&mut self, _visitor: V) -> Result<V::Value, Error>
|
|
|
|
where V: serde::de::Visitor,
|
|
|
|
{
|
|
|
|
Err(Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
|
|
|
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])));
|
|
|
|
}
|