use thiserror::Error;
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[expect(
clippy::error_impl_error,
reason = "Error is the canonical name for this crate's error type"
)]
pub enum Error {
#[error("expected at least one leaf")]
EmptyLeaves,
#[error("index {index} is out of bounds (len {len})")]
IndexOutOfBounds {
index: usize,
len: usize,
},
#[error("index {0} is not a leaf node")]
NotALeaf(usize),
#[error("root node has no parent")]
RootHasNoParent,
#[error("root node has no sibling")]
RootHasNoSibling,
#[error("duplicate leaf index {0}")]
DuplicateIndex(usize),
#[error("leaf not found in tree")]
LeafNotFound,
#[error("tree validation failed: computed root does not match")]
InvalidTree,
#[error("value at index {0} does not match its leaf hash")]
ValueMismatch(usize),
#[error("unable to prove value at index {0}")]
UnableToProve(usize),
#[error("generated multi-proof does not verify against the root")]
UnableToProveMulti,
#[error("invalid multiproof: expected {expected} proof elements, got {got}")]
InvalidMultiproof {
expected: usize,
got: usize,
},
#[error("incompatible multiproof: leaves({leaves}) + proof({proof}) != flags({flags}) + 1")]
IncompatibleMultiproof {
leaves: usize,
proof: usize,
flags: usize,
},
#[error("multiproof stack underflow")]
MultiproofStackEmpty,
#[error("multiproof proof exhausted")]
MultiproofProofExhausted,
#[error("multiproof did not converge to a single root")]
MultiproofNotConverged,
#[error("unknown format: {0}")]
UnknownFormat(String),
#[error(
"node-hash kind mismatch: serialised tree is '{serialized}' but caller provided '{provided}'"
)]
NodeHashKindMismatch {
serialized: &'static str,
provided: &'static str,
},
#[error("missing leaf encoding")]
MissingLeafEncoding,
#[error("hex decode: {0}")]
HexDecode(String),
#[error("ABI encode: {0}")]
AbiEncode(String),
#[error("node must be exactly 32 bytes, got {0}")]
InvalidNodeLength(usize),
}
impl From<hex::FromHexError> for Error {
fn from(err: hex::FromHexError) -> Self {
Self::HexDecode(err.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;