Browse Source

Started writing the serde_blocktree module.

Matthew Carr 3 years ago
parent
commit
efaa032faf

+ 9 - 0
crates/node/Cargo.lock

@@ -3,3 +3,12 @@
 [[package]]
 name = "node"
 version = "0.1.0"
+dependencies = [
+ "serde",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.136"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"

+ 1 - 0
crates/node/Cargo.toml

@@ -7,3 +7,4 @@ edition = "2018"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+serde = "1.0.136"

+ 2 - 0
crates/node/src/main.rs

@@ -1,3 +1,5 @@
+mod serde_blocktree;
+
 use std::collections::{hash_map, HashMap};
 
 enum VersionedBlock {

+ 0 - 0
crates/node/src/serde_blocktree/de.rs


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

@@ -0,0 +1,45 @@
+use std::fmt::{self, Display};
+use serde::{ser, de};
+
+pub type Result<T> = std::result::Result<T, Error>;
+
+#[derive(Debug)]
+pub enum Error {
+    Message(String),
+    Io(std::io::Error),
+    Eof,
+}
+
+impl std::error::Error for Error {}
+
+impl Display for Error {
+    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+        match self {
+            Error::Message(message) => formatter.write_str(message),
+            Error::Eof => formatter.write_str("unexpected end of input"),
+        }
+    }
+}
+
+impl ser::Error for Error {
+    fn custom<T: Display>(message: T) -> Self {
+        Error::Message(message.to_string())
+    }
+}
+
+impl de::Error for Error {
+    fn custom<T: Display>(message: T) -> Self {
+        Error::Message(message.to_string())
+    }
+}
+
+pub trait MapError<T> {
+    /// Returns self if no error occurred, otherwise converts the error to a serde_blocktree error.
+    fn map_error(self) -> Result<T>;
+}
+
+impl<T> MapError<T> for std::io::Result<T> {
+    fn map_error(self) -> Result<T> {
+        self.or_else(|err| Result::Err(Error::Io(err)))
+    }
+}

+ 5 - 0
crates/node/src/serde_blocktree/mod.rs

@@ -0,0 +1,5 @@
+mod error;
+mod ser;
+mod de;
+
+pub use error::{Error, Result};

+ 35 - 0
crates/node/src/serde_blocktree/ser.rs

@@ -0,0 +1,35 @@
+use std::io::Write;
+use serde::{ser, Serialize};
+
+use super::error::{Error, Result, MapError};
+
+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)
+}
+
+impl<'a, T: Write> ser::Serializer for &'a Serializer<T> {
+    type Ok = ();
+    type Error = Error;
+    type SerializeSeq = Self;
+    type SerializeTuple = Self;
+    type SerializeTupleStruct = Self;
+    type SerializeMap = Self;
+    type SerializeStruct = Self;
+    type SerializeStructVariant = Self;
+
+    fn serialize_bool(self, v: bool) -> Result<Self::Ok> {
+        self.output.write(&[v as u8]).map_error()?;
+        Ok(())
+    }
+
+    fn serialize_u8(self, v: u8) -> Result<Self::Ok> {
+        self.output.write(&[v]).map_error()?;
+        Ok(())
+    }
+}