Merge pull request #5 from NikVolf/review-fixes

Review and other fixes
This commit is contained in:
Nikolay Volf 2019-11-29 01:10:27 -08:00 committed by GitHub
commit e2c131fdc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 26 deletions

View File

@ -40,7 +40,7 @@ impl Entry {
/// Left child
pub fn left(&self) -> Result<EntryLink, Error> {
match self.kind {
EntryKind::Leaf => { Err(Error::ExpectedNode) }
EntryKind::Leaf => { Err(Error::node_expected()) }
EntryKind::Node(left, _) => Ok(left)
}
}
@ -48,7 +48,7 @@ impl Entry {
/// Right child.
pub fn right(&self) -> Result<EntryLink, Error> {
match self.kind {
EntryKind::Leaf => { Err(Error::ExpectedNode) }
EntryKind::Leaf => { Err(Error::node_expected()) }
EntryKind::Node(_, right) => Ok(right)
}
}

View File

@ -17,18 +17,16 @@ pub use entry::{Entry, MAX_ENTRY_SIZE};
pub enum Error {
/// Entry expected to be presented in the tree view while it was not.
ExpectedInMemory(EntryLink),
/// Entry expected to be a node.
ExpectedNode,
/// Entry expected to be a node (specifying for which link this is not true).
ExpectedNodeForLink(EntryLink),
ExpectedNode(Option<EntryLink>),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Self::ExpectedInMemory(l) => write!(f, "Node/leaf expected to be in memory: {}", l),
Self::ExpectedNode => write!(f, "Node expected"),
Self::ExpectedNodeForLink(l) => write!(f, "Node expected, not leaf: {}", l),
Self::ExpectedNode(None) => write!(f, "Node expected"),
Self::ExpectedNode(Some(l)) => write!(f, "Node expected, not leaf: {}", l),
}
}
}
@ -63,9 +61,15 @@ pub enum EntryKind {
}
impl Error {
/// Entry expected to be a node (specifying for which link this is not true).
pub fn link_node_expected(link: EntryLink) -> Self { Self::ExpectedNode(Some(link)) }
/// Some entry is expected to be node
pub fn node_expected() -> Self { Self::ExpectedNode(None) }
pub (crate) fn augment(self, link: EntryLink) -> Self {
match self {
Error::ExpectedNode => Error::ExpectedNodeForLink(link),
Error::ExpectedNode(_) => Error::ExpectedNode(Some(link)),
val => val
}
}

View File

@ -194,9 +194,8 @@ impl NodeData {
Self::read(consensus_branch_id, &mut cursor)
}
/// Hash node metadata
pub fn hash(&self) -> [u8; 32] {
let mut buf = [0u8; 32];
let bytes = self.to_bytes();
blake2b_personal(&personalization(self.consensus_branch_id), &bytes)

View File

@ -28,22 +28,15 @@ pub struct Tree {
impl Tree {
/// Resolve link originated from this tree
pub fn resolve_link(&self, link: EntryLink) -> Result<IndexedNode, Error> {
match link {
EntryLink::Generated(index) => {
let node = self.generated.get(index as usize).ok_or(Error::ExpectedInMemory(link))?;
Ok(IndexedNode {
node,
link,
})
},
EntryLink::Stored(index) => {
let node = self.stored.get(&index).ok_or(Error::ExpectedInMemory(link))?;
Ok(IndexedNode {
node,
link,
})
},
}
match link {
EntryLink::Generated(index) => self.generated.get(index as usize),
EntryLink::Stored(index) => self.stored.get(&index),
}
.map(|node| IndexedNode {
node,
link,
})
.ok_or(Error::ExpectedInMemory(link))
}
fn push(&mut self, data: Entry) -> EntryLink {