mirror of https://github.com/vacp2p/pmtree.git
chore: reimplement Error API with Error trait objects
This commit is contained in:
parent
0a456cf697
commit
f261dc15bb
|
@ -30,5 +30,7 @@ impl std::fmt::Display for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for Error {}
|
||||||
|
|
||||||
/// Custom `Result` type with custom `Error` type
|
/// Custom `Result` type with custom `Error` type
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
10
src/tree.rs
10
src/tree.rs
|
@ -118,7 +118,7 @@ where
|
||||||
/// Sets a leaf at the specified tree index
|
/// Sets a leaf at the specified tree index
|
||||||
pub fn set(&mut self, key: usize, leaf: H::Fr) -> Result<()> {
|
pub fn set(&mut self, key: usize, leaf: H::Fr) -> Result<()> {
|
||||||
if key >= self.capacity() {
|
if key >= self.capacity() {
|
||||||
return Err(Error("Merkle Tree is full!".to_string()));
|
return Err(Box::new(Error("Merkle Tree is full!".to_string())));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.db
|
self.db
|
||||||
|
@ -177,7 +177,7 @@ where
|
||||||
/// Deletes a leaf at the `key` by setting it to its default value
|
/// Deletes a leaf at the `key` by setting it to its default value
|
||||||
pub fn delete(&mut self, key: usize) -> Result<()> {
|
pub fn delete(&mut self, key: usize) -> Result<()> {
|
||||||
if key >= self.next_index {
|
if key >= self.next_index {
|
||||||
return Err(Error("The key doesn't exist!".to_string()));
|
return Err(Box::new(Error("The key doesn't exist!".to_string())));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.set(key, H::default_leaf())?;
|
self.set(key, H::default_leaf())?;
|
||||||
|
@ -197,7 +197,9 @@ where
|
||||||
let end = self.next_index + leaves.len();
|
let end = self.next_index + leaves.len();
|
||||||
|
|
||||||
if end > self.capacity() {
|
if end > self.capacity() {
|
||||||
return Err(Error("Not enough space to insert the leaves!".to_string()));
|
return Err(Box::new(Error(
|
||||||
|
"Not enough space to insert the leaves!".to_string(),
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut subtree = HashMap::<Key, H::Fr>::new();
|
let mut subtree = HashMap::<Key, H::Fr>::new();
|
||||||
|
@ -309,7 +311,7 @@ where
|
||||||
/// Computes a Merkle proof for the leaf at the specified index
|
/// Computes a Merkle proof for the leaf at the specified index
|
||||||
pub fn proof(&self, index: usize) -> Result<MerkleProof<H>> {
|
pub fn proof(&self, index: usize) -> Result<MerkleProof<H>> {
|
||||||
if index >= self.capacity() {
|
if index >= self.capacity() {
|
||||||
return Err(Error("Index exceeds set size!".to_string()));
|
return Err(Box::new(Error("Index exceeds set size!".to_string())));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut witness = Vec::with_capacity(self.depth);
|
let mut witness = Vec::with_capacity(self.depth);
|
||||||
|
|
|
@ -12,7 +12,7 @@ impl Database for MemoryDB {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load(_dbpath: &str) -> Result<Self> {
|
fn load(_dbpath: &str) -> Result<Self> {
|
||||||
Err(Error("Cannot load in-memory DB".to_string()))
|
Err(Box::new(Error("Cannot load in-memory DB".to_string())))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, key: DBKey) -> Result<Option<Value>> {
|
fn get(&self, key: DBKey) -> Result<Option<Value>> {
|
||||||
|
|
|
@ -49,7 +49,7 @@ impl Database for MemoryDB {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load(_dbpath: &str) -> Result<Self> {
|
fn load(_dbpath: &str) -> Result<Self> {
|
||||||
Err(Error("Cannot load in-memory DB".to_string()))
|
Err(Box::new(Error("Cannot load in-memory DB".to_string())))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(&self, key: DBKey) -> Result<Option<Value>> {
|
fn get(&self, key: DBKey) -> Result<Option<Value>> {
|
||||||
|
@ -73,7 +73,7 @@ impl Database for MySled {
|
||||||
fn new(dbpath: &str) -> Result<Self> {
|
fn new(dbpath: &str) -> Result<Self> {
|
||||||
let db = sled::open(dbpath).unwrap();
|
let db = sled::open(dbpath).unwrap();
|
||||||
if db.was_recovered() {
|
if db.was_recovered() {
|
||||||
return Err(Error("Database exists, try load()!".to_string()));
|
return Err(Box::new(Error("Database exists, try load()!".to_string())));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MySled(db))
|
Ok(MySled(db))
|
||||||
|
@ -84,7 +84,9 @@ impl Database for MySled {
|
||||||
|
|
||||||
if !db.was_recovered() {
|
if !db.was_recovered() {
|
||||||
fs::remove_dir_all(dbpath).expect("Error removing db");
|
fs::remove_dir_all(dbpath).expect("Error removing db");
|
||||||
return Err(Error("Trying to load non-existing database!".to_string()));
|
return Err(Box::new(Error(
|
||||||
|
"Trying to load non-existing database!".to_string(),
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MySled(db))
|
Ok(MySled(db))
|
||||||
|
|
|
@ -11,7 +11,7 @@ impl Database for MySled {
|
||||||
fn new(dbpath: &str) -> Result<Self> {
|
fn new(dbpath: &str) -> Result<Self> {
|
||||||
let db = sled::open(dbpath).unwrap();
|
let db = sled::open(dbpath).unwrap();
|
||||||
if db.was_recovered() {
|
if db.was_recovered() {
|
||||||
return Err(Error("Database exists, try load()!".to_string()));
|
return Err(Box::new(Error("Database exists, try load()!".to_string())));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MySled(db))
|
Ok(MySled(db))
|
||||||
|
@ -22,7 +22,9 @@ impl Database for MySled {
|
||||||
|
|
||||||
if !db.was_recovered() {
|
if !db.was_recovered() {
|
||||||
fs::remove_dir_all(dbpath).expect("Error removing db");
|
fs::remove_dir_all(dbpath).expect("Error removing db");
|
||||||
return Err(Error("Trying to load non-existing database!".to_string()));
|
return Err(Box::new(Error(
|
||||||
|
"Trying to load non-existing database!".to_string(),
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(MySled(db))
|
Ok(MySled(db))
|
||||||
|
|
Loading…
Reference in New Issue