chore: reimplement Error API with Error trait objects

This commit is contained in:
Magamedrasul Ibragimov 2023-02-10 13:33:58 +04:00
parent 0a456cf697
commit f261dc15bb
5 changed files with 19 additions and 11 deletions

View File

@ -30,5 +30,7 @@ impl std::fmt::Display for Error {
}
}
impl std::error::Error for Error {}
/// 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>>;

View File

@ -118,7 +118,7 @@ where
/// Sets a leaf at the specified tree index
pub fn set(&mut self, key: usize, leaf: H::Fr) -> Result<()> {
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
@ -177,7 +177,7 @@ where
/// Deletes a leaf at the `key` by setting it to its default value
pub fn delete(&mut self, key: usize) -> Result<()> {
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())?;
@ -197,7 +197,9 @@ where
let end = self.next_index + leaves.len();
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();
@ -309,7 +311,7 @@ where
/// Computes a Merkle proof for the leaf at the specified index
pub fn proof(&self, index: usize) -> Result<MerkleProof<H>> {
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);

View File

@ -12,7 +12,7 @@ impl Database for MemoryDB {
}
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>> {

View File

@ -49,7 +49,7 @@ impl Database for MemoryDB {
}
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>> {
@ -73,7 +73,7 @@ impl Database for MySled {
fn new(dbpath: &str) -> Result<Self> {
let db = sled::open(dbpath).unwrap();
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))
@ -84,7 +84,9 @@ impl Database for MySled {
if !db.was_recovered() {
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))

View File

@ -11,7 +11,7 @@ impl Database for MySled {
fn new(dbpath: &str) -> Result<Self> {
let db = sled::open(dbpath).unwrap();
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))
@ -22,7 +22,9 @@ impl Database for MySled {
if !db.was_recovered() {
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))