use crate::prelude::*;
use crate::{IndexType, NodeIndex};
#[derive(Debug, thiserror::Error)]
pub enum NodeError {
#[error("Node already contains child {child}.")]
Duplicate {
child: usize,
},
#[error(
"Cannot add child {child} to {parent}: \
node already has a parent."
)]
ParentConflict {
parent: usize,
child: usize,
},
#[error("Node at {index:?} could not be found with in tree.")]
NodeNotFound {
index: usize,
},
}
#[derive(Debug, thiserror::Error)]
pub enum EntryError {
#[error("Cannot construct hash from digest of length {0}.")]
InvalidByteSliceLength(usize),
}
#[derive(Debug, thiserror::Error)]
pub enum TreeError {
#[error("Tree is missing a root node.")]
MissingRoot,
#[error("Node is disjoint (no parent).")]
DisjointNode,
#[error("Index {index} is out of bounds for tree of length {len}.")]
IndexOutOfBounds {
index: usize,
len: usize,
},
#[error("{0}")]
NodeError(#[from] NodeError),
#[error("Builder has already been finalized and cannot be modified.")]
AlreadyFinalized,
#[error("Invalid operation '{operation}': {reason}.")]
InvalidOperation {
operation: &'static str,
reason: String,
},
#[error("Tree is in an inconsistent state: {details}.")]
InconsistentState {
details: String,
},
#[error("Validation failed with {count} error(s): {summary}.")]
ValidationFailed {
count: usize,
summary: String,
errors: Vec<TreeError>,
},
}
#[derive(Debug, thiserror::Error)]
pub enum MrkleError {
#[error("{0}")]
TreeError(#[from] TreeError),
#[error("{0}")]
ProofError(#[from] ProofError),
}
#[derive(Debug, thiserror::Error)]
pub enum ProofError {
#[error("Expected a tree length greater then 1.")]
InvalidSize,
#[error("Expected {expected} hashes, and got only {len}.")]
IncompleteProof {
len: usize,
expected: usize,
},
#[error("Invalid path, cannot start with an internal node.")]
UnexpectedInternalNode,
#[error("Expected {expected:?}, found {actual:?}.")]
RootHashMissMatch {
expected: Vec<u8>,
actual: Vec<u8>,
},
#[error("Expected root index {0}, got {1}.")]
PathRootMismatch(usize, usize),
#[error("{0}")]
TreeError(#[from] TreeError),
}
impl ProofError {
#[inline]
#[allow(dead_code)]
pub fn out_of_bounds<Ix: IndexType>(len: usize, index: NodeIndex<Ix>) -> ProofError {
ProofError::from(TreeError::IndexOutOfBounds {
index: index.index(),
len,
})
}
}