|
@@ -1,23 +1,39 @@
|
|
|
use std::io::Write;
|
|
|
-use serde::{ser, Serialize};
|
|
|
+use std::convert::TryFrom;
|
|
|
+use serde::{
|
|
|
+ ser::{
|
|
|
+ self,
|
|
|
+ SerializeSeq,
|
|
|
+ SerializeTuple,
|
|
|
+ SerializeTupleStruct,
|
|
|
+ SerializeTupleVariant,
|
|
|
+ SerializeMap,
|
|
|
+ SerializeStruct,
|
|
|
+ SerializeStructVariant,
|
|
|
+ },
|
|
|
+ Serialize,
|
|
|
+};
|
|
|
|
|
|
use super::error::{Error, Result, MapError};
|
|
|
|
|
|
+type Ok = ();
|
|
|
+
|
|
|
pub struct Serializer<T: Write> {
|
|
|
output: T,
|
|
|
}
|
|
|
|
|
|
-pub fn to_vec<T: Serialize>(value: T) -> Vec<u8> {
|
|
|
- let mut buffer = Vec::new();
|
|
|
- let mut serializer = Serializer { output: buffer };
|
|
|
- serializer.serialize(value)
|
|
|
+pub fn to_vec<T: Serialize>(value: &T) -> Result<Vec<u8>> {
|
|
|
+ let mut serializer = Serializer { output: Vec::new()};
|
|
|
+ value.serialize(&mut serializer)?;
|
|
|
+ Ok(serializer.output)
|
|
|
}
|
|
|
|
|
|
-impl<'a, T: Write> ser::Serializer for &'a Serializer<T> {
|
|
|
- type Ok = ();
|
|
|
+impl<'a, T: Write> ser::Serializer for &'a mut Serializer<T> {
|
|
|
+ type Ok = Ok;
|
|
|
type Error = Error;
|
|
|
type SerializeSeq = Self;
|
|
|
type SerializeTuple = Self;
|
|
|
+ type SerializeTupleVariant = Self;
|
|
|
type SerializeTupleStruct = Self;
|
|
|
type SerializeMap = Self;
|
|
|
type SerializeStruct = Self;
|
|
@@ -179,4 +195,159 @@ impl<'a, T: Write> ser::Serializer for &'a Serializer<T> {
|
|
|
value.serialize(self)?;
|
|
|
Ok(())
|
|
|
}
|
|
|
+
|
|
|
+ fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
|
|
|
+ match len {
|
|
|
+ Some(count) => {
|
|
|
+ let length = u32::try_from(count).or_else(|_| Err(Error::SequenceTooLong(count)))?;
|
|
|
+ self.serialize_u32(length)?;
|
|
|
+ Ok(self)
|
|
|
+ },
|
|
|
+ None => Err(Error::UnknownLength),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn serialize_tuple_struct(
|
|
|
+ self, name: &'static str, len: usize
|
|
|
+ ) -> Result<Self::SerializeTupleStruct> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn serialize_tuple_variant(
|
|
|
+ self, name: &'static str, variant_index: u32, variant: &'static str, len: usize
|
|
|
+ ) -> Result<Self::SerializeTupleStruct> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn serialize_struct_variant(
|
|
|
+ self, name: &'static str, variant_index: u32, variant: &'static str, len: usize
|
|
|
+ ) -> Result<Self::SerializeStructVariant> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'a, T: Write> SerializeSeq for &'a mut Serializer<T> {
|
|
|
+ type Ok = Ok;
|
|
|
+ type Error = Error;
|
|
|
+
|
|
|
+ fn serialize_element<U: ?Sized>(&mut self, value: &U) -> Result<()> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn end(self) -> Result<Self::Ok> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'a, T: Write> SerializeTuple for &'a mut Serializer<T> {
|
|
|
+ type Ok = Ok;
|
|
|
+ type Error = Error;
|
|
|
+
|
|
|
+ fn serialize_element<U: ?Sized>(&mut self, value: &U) -> Result<()> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn end(self) -> Result<Self::Ok> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'a, T: Write> SerializeTupleStruct for &'a mut Serializer<T> {
|
|
|
+ type Ok = Ok;
|
|
|
+ type Error = Error;
|
|
|
+
|
|
|
+ fn serialize_field<U: ?Sized>(&mut self, value: &U) -> Result<()> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn end(self) -> Result<Self::Ok> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'a, T: Write> SerializeTupleVariant for &'a mut Serializer<T> {
|
|
|
+ type Ok = Ok;
|
|
|
+ type Error = Error;
|
|
|
+
|
|
|
+ fn serialize_field<U: ?Sized>(&mut self, value: &U) -> Result<()> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn end(self) -> Result<Self::Ok> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'a, T: Write> SerializeMap for &'a mut Serializer<T> {
|
|
|
+ type Ok = Ok;
|
|
|
+ type Error = Error;
|
|
|
+
|
|
|
+ fn serialize_key<U: ?Sized>(&mut self, key: &U) -> Result<()> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn serialize_value<U: ?Sized>(&mut self, key: &U) -> Result<()> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn end(self) -> Result<Self::Ok> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+impl<'a, T: Write> SerializeStruct for &'a mut Serializer<T> {
|
|
|
+ type Ok = Ok;
|
|
|
+ type Error = Error;
|
|
|
+
|
|
|
+ fn serialize_field<U: ?Sized>(&mut self, key: &'static str, value: &U) -> Result<()> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn end(self) -> Result<Self::Ok> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'a, T: Write> SerializeStructVariant for &'a mut Serializer<T> {
|
|
|
+ type Ok = Ok;
|
|
|
+ type Error = Error;
|
|
|
+
|
|
|
+ fn serialize_field<U: ?Sized>(&mut self, key: &'static str, value: &U) -> Result<()> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+
|
|
|
+ fn end(self) -> Result<Self::Ok> {
|
|
|
+ unimplemented!();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+mod test {
|
|
|
+ use super::Result;
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn serialize_bool() -> Result<()> {
|
|
|
+ {
|
|
|
+ let mut buffer = super::to_vec(&true)?;
|
|
|
+ assert_eq!(1, buffer.pop().unwrap());
|
|
|
+ assert_eq!(true, buffer.is_empty());
|
|
|
+ }
|
|
|
+ {
|
|
|
+ let mut buffer = super::to_vec(&false)?;
|
|
|
+ assert_eq!(0, buffer.pop().unwrap());
|
|
|
+ assert_eq!(true, buffer.is_empty());
|
|
|
+ }
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+}
|