mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-11-03 04:47:02 +00:00
entry to the dedicated module
This commit is contained in:
48
src/entry.rs
Normal file
48
src/entry.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt, ByteOrder};
|
||||||
|
|
||||||
|
use crate::{EntryKind, NodeData, Error, EntryLink};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Entry {
|
||||||
|
pub(crate) kind: EntryKind,
|
||||||
|
pub(crate) data: NodeData,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Entry {
|
||||||
|
pub fn update_siblings(&mut self, left: EntryLink, right: EntryLink) {
|
||||||
|
self.kind = EntryKind::Node(left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn complete(&self) -> bool {
|
||||||
|
let leaves = self.leaf_count();
|
||||||
|
leaves & (leaves - 1) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn leaf_count(&self) -> u64 {
|
||||||
|
self.data.end_height - self.data.start_height + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_leaf(&self) -> bool {
|
||||||
|
if let EntryKind::Leaf = self.kind { true } else { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn left(&self) -> Result<EntryLink, Error> {
|
||||||
|
match self.kind {
|
||||||
|
EntryKind::Leaf => { Err(Error::ExpectedNode) }
|
||||||
|
EntryKind::Node(left, _) => Ok(left)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn right(&self) -> Result<EntryLink, Error> {
|
||||||
|
match self.kind {
|
||||||
|
EntryKind::Leaf => { Err(Error::ExpectedNode) }
|
||||||
|
EntryKind::Node(_, right) => Ok(right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<NodeData> for Entry {
|
||||||
|
fn from(s: NodeData) -> Self {
|
||||||
|
Entry { kind: EntryKind::Leaf, data: s }
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/lib.rs
47
src/lib.rs
@@ -4,9 +4,11 @@
|
|||||||
|
|
||||||
mod tree;
|
mod tree;
|
||||||
mod node_data;
|
mod node_data;
|
||||||
|
mod entry;
|
||||||
|
|
||||||
pub use tree::Tree;
|
pub use tree::Tree;
|
||||||
pub use node_data::NodeData;
|
pub use node_data::NodeData;
|
||||||
|
pub use entry::Entry;
|
||||||
|
|
||||||
#[derive(Debug, derive_more::Display)]
|
#[derive(Debug, derive_more::Display)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
@@ -38,51 +40,6 @@ pub enum EntryKind {
|
|||||||
Node(EntryLink, EntryLink),
|
Node(EntryLink, EntryLink),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Entry {
|
|
||||||
kind: EntryKind,
|
|
||||||
data: NodeData,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Entry {
|
|
||||||
pub fn update_siblings(&mut self, left: EntryLink, right: EntryLink) {
|
|
||||||
self.kind = EntryKind::Node(left, right);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn complete(&self) -> bool {
|
|
||||||
let leaves = self.leaf_count();
|
|
||||||
leaves & (leaves - 1) == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn leaf_count(&self) -> u64 {
|
|
||||||
self.data.end_height - self.data.start_height + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_leaf(&self) -> bool {
|
|
||||||
if let EntryKind::Leaf = self.kind { true } else { false }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn left(&self) -> Result<EntryLink, Error> {
|
|
||||||
match self.kind {
|
|
||||||
EntryKind::Leaf => { Err(Error::ExpectedNode) }
|
|
||||||
EntryKind::Node(left, _) => Ok(left)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn right(&self) -> Result<EntryLink, Error> {
|
|
||||||
match self.kind {
|
|
||||||
EntryKind::Leaf => { Err(Error::ExpectedNode) }
|
|
||||||
EntryKind::Node(_, right) => Ok(right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<NodeData> for Entry {
|
|
||||||
fn from(s: NodeData) -> Self {
|
|
||||||
Entry { kind: EntryKind::Leaf, data: s }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error {
|
impl Error {
|
||||||
pub (crate) fn augment(self, link: EntryLink) -> Self {
|
pub (crate) fn augment(self, link: EntryLink) -> Self {
|
||||||
match self {
|
match self {
|
||||||
|
|||||||
Reference in New Issue
Block a user