reading for Entry

This commit is contained in:
NikVolf
2019-09-07 14:00:34 +03:00
parent 03524ba7d0
commit de053e1d8f
2 changed files with 27 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt, ByteOrder};
use byteorder::{LittleEndian, ReadBytesExt};
use crate::{EntryKind, NodeData, Error, EntryLink};
@@ -39,6 +39,31 @@ impl Entry {
EntryKind::Node(_, right) => Ok(right)
}
}
pub fn read<R: std::io::Read>(&self, consensus_branch_id: u32, r: &mut R) -> std::io::Result<Self> {
let kind = {
match r.read_u8()? {
0 => {
let left = r.read_u32::<LittleEndian>()?;
let right = r.read_u32::<LittleEndian>()?;
EntryKind::Node(EntryLink::Stored(left), EntryLink::Stored(right))
},
1 => {
EntryKind::Leaf
},
_ => {
return Err(std::io::Error::from(std::io::ErrorKind::InvalidData))
},
}
};
let data = NodeData::read(consensus_branch_id, r)?;
Ok(Entry {
kind,
data,
})
}
}
impl From<NodeData> for Entry {

View File

@@ -121,7 +121,7 @@ impl NodeData {
Ok(())
}
pub fn read<R: std::io::Read>(&self, consensus_branch_id: u32, r: &mut R) -> std::io::Result<Self> {
pub fn read<R: std::io::Read>(consensus_branch_id: u32, r: &mut R) -> std::io::Result<Self> {
let mut data = Self::default();
data.consensus_branch_id = consensus_branch_id;
r.read_exact(&mut data.subtree_commitment)?;