From a0c33945ab3b534eb47ab0f000bbf15972b24b06 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 9 Sep 2019 14:33:49 +0300 Subject: [PATCH] write for entry --- examples/producer.rs | 2 +- src/entry.rs | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/examples/producer.rs b/examples/producer.rs index 1bc5824..108bc25 100644 --- a/examples/producer.rs +++ b/examples/producer.rs @@ -206,7 +206,7 @@ fn main() { let mut buf = Vec::new(); if let Some(out_file_path) = ::std::env::args().nth(1) { for node in initial_tree_vec.into_iter() { - node.write(&mut buf); + node.write(&mut buf).expect("Failed to write data"); } let mut file = std::fs::File::create(&out_file_path) diff --git a/src/entry.rs b/src/entry.rs index 982b879..72582c2 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -1,4 +1,4 @@ -use byteorder::{LittleEndian, ReadBytesExt}; +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use crate::{EntryKind, NodeData, Error, EntryLink, MAX_NODE_DATA_SIZE}; @@ -67,6 +67,24 @@ impl Entry { }) } + pub fn write(&self, w: &mut W) -> std::io::Result<()> { + match self.kind { + EntryKind::Node(EntryLink::Stored(left), EntryLink::Stored(right)) => { + w.write_u8(0)?; + w.write_u32::(left)?; + w.write_u32::(right)?; + }, + EntryKind::Leaf => { + w.write_u8(1)?; + }, + _ => { return Err(std::io::Error::from(std::io::ErrorKind::InvalidData)); } + } + + self.data.write(w)?; + + Ok(()) + } + pub fn from_bytes>(consensus_branch_id: u32, buf: T) -> std::io::Result { let mut cursor = std::io::Cursor::new(buf); Self::read(consensus_branch_id, &mut cursor)