From 0543cb345320a15280a5af7db941bb9fbffb4cd6 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 7 Jun 2022 17:50:10 +0200 Subject: Cleaner error management (less error-prone api) --- src/db/sled_adapter.rs | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'src/db/sled_adapter.rs') diff --git a/src/db/sled_adapter.rs b/src/db/sled_adapter.rs index b07401c9..d0d9e9c0 100644 --- a/src/db/sled_adapter.rs +++ b/src/db/sled_adapter.rs @@ -9,7 +9,10 @@ use sled::transaction::{ UnabortableTransactionError, }; -use crate::{Db, Error, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxResult, Value, ValueIter}; +use crate::{ + Db, Error, IDb, ITx, ITxFn, Result, TxError, TxFnResult, TxOpError, TxOpResult, TxResult, + Value, ValueIter, +}; pub use sled; @@ -21,6 +24,12 @@ impl From for Error { } } +impl From for TxOpError { + fn from(e: sled::Error) -> TxOpError { + TxOpError(e.into()) + } +} + // -- db pub struct SledDb { @@ -177,51 +186,54 @@ struct SledTx<'a> { } impl<'a> SledTx<'a> { - fn get_tree(&self, i: usize) -> Result<&TransactionalTree> { + fn get_tree(&self, i: usize) -> TxOpResult<&TransactionalTree> { self.trees.get(i).ok_or_else(|| { - Error( + TxOpError(Error( "invalid tree id (it might have been openned after the transaction started)".into(), - ) + )) }) } - fn save_error(&self, v: std::result::Result) -> Result { + fn save_error( + &self, + v: std::result::Result, + ) -> TxOpResult { match v { Ok(x) => Ok(x), Err(e) => { let txt = format!("{}", e); self.err.set(Some(e)); - Err(Error(txt.into())) + Err(TxOpError(Error(txt.into()))) } } } } impl<'a> ITx for SledTx<'a> { - fn get(&self, tree: usize, key: &[u8]) -> Result> { + fn get(&self, tree: usize, key: &[u8]) -> TxOpResult> { let tree = self.get_tree(tree)?; let tmp = self.save_error(tree.get(key))?; Ok(tmp.map(|x| x.to_vec())) } - fn len(&self, _tree: usize) -> Result { + fn len(&self, _tree: usize) -> TxOpResult { unimplemented!(".len() in transaction not supported with Sled backend") } - fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result> { + fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> TxOpResult> { let tree = self.get_tree(tree)?; let old_val = self.save_error(tree.insert(key, value))?; Ok(old_val.map(|x| x.to_vec())) } - fn remove(&mut self, tree: usize, key: &[u8]) -> Result> { + fn remove(&mut self, tree: usize, key: &[u8]) -> TxOpResult> { let tree = self.get_tree(tree)?; let old_val = self.save_error(tree.remove(key))?; Ok(old_val.map(|x| x.to_vec())) } - fn iter(&self, _tree: usize) -> Result> { + fn iter(&self, _tree: usize) -> TxOpResult> { unimplemented!("Iterators in transactions not supported with Sled backend"); } - fn iter_rev(&self, _tree: usize) -> Result> { + fn iter_rev(&self, _tree: usize) -> TxOpResult> { unimplemented!("Iterators in transactions not supported with Sled backend"); } @@ -230,7 +242,7 @@ impl<'a> ITx for SledTx<'a> { _tree: usize, _low: Bound<&'r [u8]>, _high: Bound<&'r [u8]>, - ) -> Result> { + ) -> TxOpResult> { unimplemented!("Iterators in transactions not supported with Sled backend"); } fn range_rev<'r>( @@ -238,7 +250,7 @@ impl<'a> ITx for SledTx<'a> { _tree: usize, _low: Bound<&'r [u8]>, _high: Bound<&'r [u8]>, - ) -> Result> { + ) -> TxOpResult> { unimplemented!("Iterators in transactions not supported with Sled backend"); } } -- cgit v1.2.3