From f261dc15bbe823e9f7aaf69ba0ba9125cb6c163f Mon Sep 17 00:00:00 2001 From: Magamedrasul Ibragimov Date: Fri, 10 Feb 2023 13:33:58 +0400 Subject: [PATCH] chore: reimplement Error API with Error trait objects --- src/lib.rs | 4 +++- src/tree.rs | 10 ++++++---- tests/memory_keccak.rs | 2 +- tests/poseidon.rs | 8 +++++--- tests/sled_keccak.rs | 6 ++++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a8734cd..3bd7984 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 = std::result::Result; +pub type Result = std::result::Result>; diff --git a/src/tree.rs b/src/tree.rs index 3270442..ed7eb81 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -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::::new(); @@ -309,7 +311,7 @@ where /// Computes a Merkle proof for the leaf at the specified index pub fn proof(&self, index: usize) -> Result> { 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); diff --git a/tests/memory_keccak.rs b/tests/memory_keccak.rs index 921b71b..e389d3f 100644 --- a/tests/memory_keccak.rs +++ b/tests/memory_keccak.rs @@ -12,7 +12,7 @@ impl Database for MemoryDB { } fn load(_dbpath: &str) -> Result { - 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> { diff --git a/tests/poseidon.rs b/tests/poseidon.rs index 8426067..2f6117c 100644 --- a/tests/poseidon.rs +++ b/tests/poseidon.rs @@ -49,7 +49,7 @@ impl Database for MemoryDB { } fn load(_dbpath: &str) -> Result { - 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> { @@ -73,7 +73,7 @@ impl Database for MySled { fn new(dbpath: &str) -> Result { 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)) diff --git a/tests/sled_keccak.rs b/tests/sled_keccak.rs index 9eae448..2b05043 100644 --- a/tests/sled_keccak.rs +++ b/tests/sled_keccak.rs @@ -11,7 +11,7 @@ impl Database for MySled { fn new(dbpath: &str) -> Result { 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))