merkle_tree_db/
error.rs

1// TREE ERROR
2// ================================================================================================
3
4/// Errors associated with the tree. These errors are returned by the tree methods and wrap
5/// errors returned by the underlying components, these include:
6/// - DataError - errors associated with the underlying data the tree is built on
7/// - NodeError - errors associated with the nodes in the tree
8/// - DepthTooLarge - error returned when the specified tree depth is too large
9/// - KeyError - error associated with the key used to access the tree
10#[derive(Debug, PartialEq, Eq)]
11pub enum TreeError {
12    DataError(DataError),
13    NodeError(NodeError),
14    DepthTooLarge(usize, usize),
15    KeyError(KeyError),
16}
17
18impl core::fmt::Display for TreeError {
19    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20        use TreeError::*;
21        match self {
22            DataError(err) => write!(f, "Data Error: {err}"),
23            NodeError(err) => write!(f, "Node Error: {err}"),
24            DepthTooLarge(actual, max) => {
25                write!(f, "depth {actual} too large - max supported depth is {max}",)
26            }
27            KeyError(err) => write!(f, "key error: {err}"),
28        }
29    }
30}
31
32// DATA ERROR
33// ================================================================================================
34
35/// Errors associated with the underlying data the tree is built on.
36#[derive(Debug, PartialEq, Eq)]
37pub enum DataError {
38    DatabaseDataNotFound(Vec<u8>),
39    NullNodeDataNotFound(Vec<u8>),
40    InMemoryDataNotFound(Vec<u8>),
41    InMemoryNotSupported,
42}
43
44impl core::fmt::Display for DataError {
45    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46        use DataError::*;
47        match self {
48            DatabaseDataNotFound(hash) => {
49                write!(f, "database data not found for hash {hash:?}")
50            }
51            NullNodeDataNotFound(hash) => {
52                write!(f, "null node data not found for hash {hash:?}")
53            }
54            InMemoryNotSupported => write!(f, "in-memory data not supported for immutable tree"),
55            InMemoryDataNotFound(hash) => {
56                write!(f, "in-memory data not found for hash {hash:?}")
57            }
58        }
59    }
60}
61
62// NODE ERROR
63// ================================================================================================
64
65/// Errors associated with the nodes in the tree.
66#[derive(Debug, PartialEq, Eq)]
67pub enum NodeError {
68    DecodeNodeEmptyValue,
69    DecodeNodeNoData,
70    DecodeNodeInvalidPrefix(u8),
71    DecodeNodeHashFailed(Vec<u8>),
72    DecodeNodeInvalidLength(usize, usize),
73    InconsistentDefaultHashes,
74    InvalidNodeType(String, String),
75}
76
77impl core::fmt::Display for NodeError {
78    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
79        use NodeError::*;
80        match self {
81            DecodeNodeEmptyValue => write!(f, "decode node failed - empty value provided"),
82            DecodeNodeNoData => write!(f, "decode node failed - no data provided"),
83            DecodeNodeInvalidPrefix(prefix) => {
84                write!(f, "decode node failed - invalid prefix {prefix}",)
85            }
86            DecodeNodeHashFailed(data) => {
87                write!(
88                    f,
89                    "decode node failed - hash decode failed for data {data:?}",
90                )
91            }
92            DecodeNodeInvalidLength(expected, actual) => {
93                write!(
94                    f,
95                    "decode node failed - invalid length - expected {expected}, got {actual}",
96                )
97            }
98            InconsistentDefaultHashes => {
99                write!(f, "inconsistent default hashes")
100            }
101            InvalidNodeType(expected, actual) => {
102                write!(
103                    f,
104                    "invalid node type - method not supported - expected {expected}, got {actual}",
105                )
106            }
107        }
108    }
109}
110
111// KEY ERROR
112// ================================================================================================
113
114/// Errors associated with the keys used to access the tree.
115#[derive(Debug, PartialEq, Eq)]
116pub enum KeyError {
117    IncorrectKeySize(usize, usize),
118    BitIndexOutOfBounds(usize, usize),
119    LeafIndexOutOfBounds(u64, u64),
120}
121
122impl core::fmt::Display for KeyError {
123    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
124        use KeyError::*;
125        match self {
126            IncorrectKeySize(expected, actual) => {
127                write!(f, "incorrect key size - expected {expected}, got {actual}",)
128            }
129            BitIndexOutOfBounds(bit, max) => {
130                write!(
131                    f,
132                    "bit index out of bounds - index {bit} is out of range - max {max}",
133                )
134            }
135            LeafIndexOutOfBounds(index, max) => {
136                write!(
137                    f,
138                    "leaf index out of bounds - index {index} is out of range - max {max}",
139                )
140            }
141        }
142    }
143}