浏览代码

Added stubs for all of the unimplmenented Serializer methods
to get the node crate building again.

Matthew Carr 3 年之前
父节点
当前提交
f67bf6ca4a
共有 2 个文件被更改,包括 184 次插入7 次删除
  1. 6 0
      crates/node/src/serde_blocktree/error.rs
  2. 178 7
      crates/node/src/serde_blocktree/ser.rs

+ 6 - 0
crates/node/src/serde_blocktree/error.rs

@@ -8,6 +8,8 @@ pub enum Error {
     Message(String),
     Io(std::io::Error),
     Eof,
+    UnknownLength,
+    SequenceTooLong(usize),
 }
 
 impl std::error::Error for Error {}
@@ -16,7 +18,11 @@ impl Display for Error {
     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
         match self {
             Error::Message(message) => formatter.write_str(message),
+            Error::Io(io_error) => io_error.fmt(formatter),
             Error::Eof => formatter.write_str("unexpected end of input"),
+            Error::UnknownLength => formatter.write_str("sequence had an unknown length"),
+            Error::SequenceTooLong(length) => formatter.write_fmt(
+                format_args!("sequence was longer than 2**32 - 1: {}", length)),
         }
     }
 }

+ 178 - 7
crates/node/src/serde_blocktree/ser.rs

@@ -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(())
+    }
+}