Skip to main content

spine/
error.rs

1//! Minimal structural error type.
2//!
3//! The spine's verification surface (inclusion) reports failure with `bool` /
4//! `Option`, so it needs no error type there. Errors arise only where the spine
5//! *constructs* a checked value — sealing a frontier — and the input can be
6//! structurally ill-formed. This enum stays deliberately small: the
7//! algorithm-lifecycle errors (a malformed committed epoch timeline) belong to
8//! the `polydigest` combinator above the spine, not here.
9
10use std::fmt;
11
12/// A spine construction error.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum Error {
15    /// The spine arity is outside the valid range `2..=256`.
16    BadArity,
17    /// A frontier was sealed with a peak count that does not match the canonical
18    /// frontier geometry for `(tree_size, arity)` — a truncated or oversized
19    /// peaks slice.
20    MalformedFrontier,
21    /// A member-root fold was requested but no hasher was supplied for an
22    /// algorithm that has a frontier in the seal. Folding over a truncated
23    /// member-root list would yield a root that no algorithm published, so a
24    /// missing hasher is an error, not a silent skip. Carries the offending
25    /// algorithm ID.
26    MissingHasher {
27        /// The algorithm whose hasher was absent from the supplied set.
28        alg_id: u64,
29    },
30}
31
32impl fmt::Display for Error {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::BadArity => write!(f, "spine arity is outside the valid range 2..=256"),
36            Self::MalformedFrontier => {
37                write!(
38                    f,
39                    "frontier peak count does not match the canonical geometry for the sealed \
40                     (tree_size, arity)"
41                )
42            },
43            Self::MissingHasher { alg_id } => {
44                write!(
45                    f,
46                    "no hasher supplied for algorithm {alg_id}, which has a frontier in the seal; \
47                     a member-root fold cannot be computed with a missing hasher"
48                )
49            },
50        }
51    }
52}
53
54impl std::error::Error for Error {}
55
56/// A specialized `Result` alias for the spine.
57pub type Result<T> = std::result::Result<T, Error>;