sparse_merkle_tree/
error.rs

1use crate::{string, H256};
2
3pub type Result<T> = ::core::result::Result<T, Error>;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum Error {
7    MissingBranch(u8, H256),
8    MissingLeaf(H256),
9    CorruptedProof,
10    EmptyProof,
11    EmptyKeys,
12    IncorrectNumberOfLeaves { expected: usize, actual: usize },
13    Store(string::String),
14    CorruptedStack,
15    NonSiblings,
16    InvalidCode(u8),
17    NonMergableRange,
18}
19
20impl core::fmt::Display for Error {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        match self {
23            Error::MissingBranch(height, key) => {
24                write!(
25                    f,
26                    "Corrupted store, missing branch height:{}, key:{:?}",
27                    height, key
28                )?;
29            }
30            Error::MissingLeaf(key) => {
31                write!(f, "Corrupted store, missing leaf {:?}", key)?;
32            }
33            Error::CorruptedProof => {
34                write!(f, "Corrupted proof")?;
35            }
36            Error::EmptyProof => {
37                write!(f, "Empty proof")?;
38            }
39            Error::EmptyKeys => {
40                write!(f, "Empty keys")?;
41            }
42            Error::IncorrectNumberOfLeaves { expected, actual } => {
43                write!(
44                    f,
45                    "Incorrect number of leaves, expected {} actual {}",
46                    expected, actual
47                )?;
48            }
49            Error::Store(err_msg) => {
50                write!(f, "Backend store error: {}", err_msg)?;
51            }
52            Error::CorruptedStack => {
53                write!(f, "Corrupted serialized proof stack")?;
54            }
55            Error::NonSiblings => {
56                write!(f, "Merging non-siblings in serialized stack")?;
57            }
58            Error::InvalidCode(code) => {
59                write!(f, "Invalid serialized proof code: {}", code)?;
60            }
61            Error::NonMergableRange => {
62                write!(f, "Ranges can not be merged")?;
63            }
64        }
65        Ok(())
66    }
67}
68
69#[cfg(feature = "std")]
70impl std::error::Error for Error {}