vacp2p_pmtree/
lib.rs

1//! # pmtree
2//! Persistent Merkle Tree in Rust
3//!
4//! ## How it stored
5//! { (usize::MAX - 1) : depth }
6//! { (usize::MAX)     : next_index}
7//! { Position (tuple - (depth, index), converted to DBKey) : Value}
8
9pub mod database;
10pub mod hasher;
11pub mod tree;
12
13use std::fmt::{Debug, Display};
14
15pub use database::*;
16pub use hasher::*;
17pub use tree::MerkleTree;
18
19/// Denotes keys in a database
20pub type DBKey = [u8; 8];
21
22/// Denotes values in a database
23pub type Value = Vec<u8>;
24
25/// Denotes pmtree Merkle tree errors
26#[derive(Debug)]
27pub enum TreeErrorKind {
28    MerkleTreeIsFull,
29    InvalidKey,
30    IndexOutOfBounds,
31    CustomError(String),
32}
33
34/// Denotes pmtree database errors
35#[derive(Debug)]
36pub enum DatabaseErrorKind {
37    CannotLoadDatabase,
38    DatabaseExists,
39    CustomError(String),
40}
41
42/// Denotes pmtree errors
43#[derive(Debug)]
44pub enum PmtreeErrorKind {
45    /// Error in database
46    DatabaseError(DatabaseErrorKind),
47    /// Error in tree
48    TreeError(TreeErrorKind),
49    /// Custom error
50    CustomError(String),
51}
52
53impl Display for PmtreeErrorKind {
54    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
55        match self {
56            PmtreeErrorKind::DatabaseError(e) => write!(f, "Database error: {e:?}"),
57            PmtreeErrorKind::TreeError(e) => write!(f, "Tree error: {e:?}"),
58            PmtreeErrorKind::CustomError(e) => write!(f, "Custom error: {e:?}"),
59        }
60    }
61}
62
63impl std::error::Error for PmtreeErrorKind {}
64
65/// Custom `Result` type with custom `Error` type
66pub type PmtreeResult<T> = std::result::Result<T, PmtreeErrorKind>;