write for entry

This commit is contained in:
NikVolf 2019-09-09 14:33:49 +03:00
parent 96b130e034
commit a0c33945ab
2 changed files with 20 additions and 2 deletions

View File

@ -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)

View File

@ -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<W: std::io::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::<LittleEndian>(left)?;
w.write_u32::<LittleEndian>(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<T: AsRef<[u8]>>(consensus_branch_id: u32, buf: T) -> std::io::Result<Self> {
let mut cursor = std::io::Cursor::new(buf);
Self::read(consensus_branch_id, &mut cursor)