浏览代码

Removed remaining instances of unimplemented!.

Matthew Carr 3 年之前
父节点
当前提交
ecc6f4d70a
共有 1 个文件被更改,包括 92 次插入48 次删除
  1. 92 48
      crates/node/src/serde_blocktree/ser.rs

+ 92 - 48
crates/node/src/serde_blocktree/ser.rs

@@ -28,6 +28,16 @@ pub fn to_vec<T: Serialize + ?Sized>(value: &T) -> Result<Vec<u8>> {
     Ok(serializer.output)
 }
 
+fn try_convert(len: Option<usize>) -> Result<u32> {
+    match len {
+        Some(count) => {
+            let length = u32::try_from(count).or_else(|_| Err(Error::SequenceTooLong(count)))?;
+            Ok(length)
+        },
+        None => Err(Error::UnknownLength),
+    }
+}
+
 impl<'a, T: Write> ser::Serializer for &'a mut Serializer<T> {
     type Ok = Ok;
     type Error = Error;
@@ -197,44 +207,51 @@ impl<'a, T: Write> ser::Serializer for &'a mut Serializer<T> {
     }
 
     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),
-        }
+        let length = try_convert(len)?;
+        self.serialize_u32(length)?;
+        Ok(self)
     }
 
-    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
-        unimplemented!();
+    /// A tuples length is not stored, only its entries.
+    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
+        Ok(self)
     }
 
+    /// A tuple struct is serialized the same way as a tuple, its name is ignore.
     fn serialize_tuple_struct(
-        self, name: &'static str, len: usize
+        self, _name: &'static str, _len: usize
     ) -> Result<Self::SerializeTupleStruct> {
-        unimplemented!();
+        Ok(self)
     }
 
+    /// The variant index is stored before the tuples values.
     fn serialize_tuple_variant(
-        self, name: &'static str, variant_index: u32, variant: &'static str, len: usize
+        self, _name: &'static str, variant_index: u32, _variant: &'static str, _len: usize
     ) -> Result<Self::SerializeTupleStruct> {
-        unimplemented!();
+        self.serialize_u32(variant_index)?;
+        Ok(self)
     }
 
+    /// The number of entries in the map is stored as a u32 prior to serializing the key value
+    /// pairs in the map. If there are more entries than a u32 can represent, then an error is
+    /// returned.
     fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
-        unimplemented!();
+        let length = try_convert(len)?;
+        self.serialize_u32(length)?;
+        Ok(self)
     }
 
-    fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
-        unimplemented!();
+    /// Since the members of a struct a known at compile time, no additional information is stored.
+    fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
+        Ok(self)
     }
 
+    /// The variant index is stored before the struct's members.
     fn serialize_struct_variant(
-        self, name: &'static str, variant_index: u32, variant: &'static str, len: usize
+        self, _name: &'static str, variant_index: u32, _variant: &'static str, _len: usize
     ) -> Result<Self::SerializeStructVariant> {
-        unimplemented!();
+        self.serialize_u32(variant_index)?;
+        Ok(self)
     }
 }
 
@@ -242,13 +259,13 @@ impl<'a, T: Write> SerializeSeq for &'a mut Serializer<T> {
     type Ok = Ok;
     type Error = Error;
 
-    fn serialize_element<U: ?Sized + Serialize>(&mut self, value: &U) -> Result<()> {
+    fn serialize_element<U: ?Sized + Serialize>(&mut self, value: &U) -> Result<Ok> {
         value.serialize(&mut **self)?;
         Ok(())
     }
 
-    /// We don't need to add a marker to the end of the sequence because we know its length.
-    fn end(self) -> Result<Self::Ok> {
+    /// No marker is added to the end of the sequence because we know its length.
+    fn end(self) -> Result<Ok> {
         Ok(())
     }
 }
@@ -257,12 +274,13 @@ 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 serialize_element<U: ?Sized + Serialize>(&mut self, value: &U) -> Result<Ok> {
+        value.serialize(&mut **self)?;
+        Ok(())
     }
 
-    fn end(self) -> Result<Self::Ok> {
-        unimplemented!();
+    fn end(self) -> Result<Ok> {
+        Ok(())
     }
 }
 
@@ -270,12 +288,13 @@ 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 serialize_field<U: ?Sized + Serialize>(&mut self, value: &U) -> Result<Ok> {
+        value.serialize(&mut **self)?;
+        Ok(())
     }
 
-    fn end(self) -> Result<Self::Ok> {
-        unimplemented!();
+    fn end(self) -> Result<Ok> {
+        Ok(())
     }
 }
 
@@ -283,12 +302,13 @@ 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 serialize_field<U: ?Sized + Serialize>(&mut self, value: &U) -> Result<Ok> {
+        value.serialize(&mut **self)?;
+        Ok(())
     }
 
-    fn end(self) -> Result<Self::Ok> {
-        unimplemented!();
+    fn end(self) -> Result<Ok> {
+        Ok(())
     }
 }
 
@@ -296,16 +316,18 @@ 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_key<U: ?Sized + Serialize>(&mut self, key: &U) -> Result<Ok> {
+        key.serialize(&mut **self)?;
+        Ok(())
     }
 
-    fn serialize_value<U: ?Sized>(&mut self, key: &U) -> Result<()> {
-        unimplemented!();
+    fn serialize_value<U: ?Sized + Serialize>(&mut self, value: &U) -> Result<Ok> {
+        value.serialize(&mut **self)?;
+        Ok(())
     }
 
-    fn end(self) -> Result<Self::Ok> {
-        unimplemented!();
+    fn end(self) -> Result<Ok> {
+        Ok(())
     }
 }
 
@@ -313,12 +335,14 @@ 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 serialize_field<U: ?Sized + Serialize>(
+        &mut self, _key: &'static str, value: &U) -> Result<Ok> {
+        value.serialize(&mut **self)?;
+        Ok(())
     }
 
-    fn end(self) -> Result<Self::Ok> {
-        unimplemented!();
+    fn end(self) -> Result<Ok> {
+        Ok(())
     }
 }
 
@@ -326,17 +350,29 @@ 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 serialize_field<U: ?Sized + Serialize>(
+        &mut self, _key: &'static str, value: &U) -> Result<Ok> {
+        value.serialize(&mut **self)?;
+        Ok(())
     }
 
-    fn end(self) -> Result<Self::Ok> {
-        unimplemented!();
+    fn end(self) -> Result<Ok> {
+        Ok(())
     }
 }
 
 mod test {
     use super::Result;
+    use super::super::super::{
+        VersionedBlock,
+        Block,
+        ReadCap,
+        WriteCap,
+        Certificate,
+        Hash,
+        Signature,
+        EnvelopedKey
+    };
 
     #[test]
     fn serialize_bool() -> Result<()> {
@@ -489,4 +525,12 @@ mod test {
         assert_eq!(expected, buffer);
         Ok(())
     }
+
+    #[test]
+    fn serialize_str() -> Result<()> {
+        let message = "vapid 😑";
+        let buffer = super::to_vec(message)?;
+        assert_eq!(vec![10, 0, 0, 0, 118, 97, 112, 105, 100, 32, 240, 159, 152, 145], buffer);
+        Ok(())
+    }
 }