fix review notes and other issues

This commit is contained in:
NikVolf 2019-11-28 15:31:23 +03:00
parent 26be46573e
commit 9059f53873
4 changed files with 22 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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