1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#[cfg(feature = "std")]
use std::error;
use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Error {
    TreeToLarge,
    NumLeavesNotPowerOfTwo,
    IndexOutOfRange,
    IndicesUnsortedOrDuplicate,
    DuplicateLeafMismatch,
    NotEnoughHashes,
    RootHashMismatch,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Error::*;
        match *self {
            TreeToLarge => write!(f, "Tree too large"),
            NumLeavesNotPowerOfTwo => write!(f, "Doesn't have a power of two of leaves"),
            IndexOutOfRange => write!(f, "Index out of range"),
            IndicesUnsortedOrDuplicate => write!(f, "Indices are unsorted or duplicate"),
            DuplicateLeafMismatch => write!(f, "Duplicate leaf mismatch"),
            NotEnoughHashes => write!(f, "Not enough hashes to verify proof"),
            RootHashMismatch => write!(f, "Verification failed since root hashes don't match"),
        }
    }
}

#[cfg(feature = "std")]
impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            _ => None,
        }
    }
}