Skip to main content

cmt/
error.rs

1//! Errors raised by the mutable tree's construction and mutation surface.
2//!
3//! The spine reports proof *verification* with `bool`/`Option`; these errors
4//! arise only where the CMT rejects an ill-formed mutation or seal.
5
6use std::fmt;
7
8/// A CMT construction or mutation error.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum Error {
11    /// The configured arity is outside the spine's `2..=256` range.
12    InvalidArity(u64),
13    /// An algorithm ID was registered twice.
14    DuplicateAlgorithm(u64),
15    /// A `set` at `index` would leave a gap: the tree is dense, so an index may
16    /// be at most `len` (which appends).
17    IndexGap {
18        /// The rejected index.
19        index: u64,
20        /// The current cell count.
21        len: u64,
22    },
23    /// Sealing an empty tree, which has no root to carry.
24    EmptySeal,
25    /// The spine rejected the resumable frontier as malformed.
26    MalformedSeal,
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Self::InvalidArity(k) => write!(f, "arity {k} is outside the supported range 2..=256"),
33            Self::DuplicateAlgorithm(id) => write!(f, "algorithm {id} is already registered"),
34            Self::IndexGap { index, len } => {
35                write!(
36                    f,
37                    "index {index} leaves a gap in a dense tree of length {len} (max settable \
38                     index is {len})"
39                )
40            },
41            Self::EmptySeal => write!(f, "cannot seal an empty tree: there is no root to carry"),
42            Self::MalformedSeal => {
43                write!(f, "the spine rejected the sealed resumable frontier")
44            },
45        }
46    }
47}
48
49impl std::error::Error for Error {}
50
51/// A specialized `Result` for the mutable tree.
52pub type Result<T> = std::result::Result<T, Error>;