From 9059f53873fcc59e4c6b0c4c9fb7caddc5f9f128 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Thu, 28 Nov 2019 15:31:23 +0300 Subject: [PATCH] fix review notes and other issues --- src/entry.rs | 4 ++-- src/lib.rs | 16 ++++++++++------ src/node_data.rs | 3 +-- src/tree.rs | 25 +++++++++---------------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/entry.rs b/src/entry.rs index 257b9dd..ab95a38 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -40,7 +40,7 @@ impl Entry { /// Left child pub fn left(&self) -> Result { 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 { match self.kind { - EntryKind::Leaf => { Err(Error::ExpectedNode) } + EntryKind::Leaf => { Err(Error::node_expected()) } EntryKind::Node(_, right) => Ok(right) } } diff --git a/src/lib.rs b/src/lib.rs index c0c781c..0c26dfa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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), } 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 } } diff --git a/src/node_data.rs b/src/node_data.rs index 0e972f9..55b2df2 100644 --- a/src/node_data.rs +++ b/src/node_data.rs @@ -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) diff --git a/src/tree.rs b/src/tree.rs index d3b6dcd..c020c72 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -28,22 +28,15 @@ pub struct Tree { impl Tree { /// Resolve link originated from this tree pub fn resolve_link(&self, link: EntryLink) -> Result { - 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 {