use crate::{
transactional::{
StorageTransaction,
Transaction,
Transactional,
},
Error as StorageError,
Mappable,
MerkleRoot,
MerkleRootStorage,
Result as StorageResult,
StorageInspect,
StorageMutate,
};
#[derive(Default, Clone, Copy, Debug)]
pub struct EmptyStorage;
impl AsRef<EmptyStorage> for EmptyStorage {
fn as_ref(&self) -> &EmptyStorage {
self
}
}
impl AsMut<EmptyStorage> for EmptyStorage {
fn as_mut(&mut self) -> &mut EmptyStorage {
self
}
}
impl Transactional for EmptyStorage {
type Storage = EmptyStorage;
fn transaction(&self) -> StorageTransaction<Self::Storage> {
StorageTransaction::new(EmptyStorage)
}
}
impl Transaction<EmptyStorage> for EmptyStorage {
fn commit(&mut self) -> StorageResult<()> {
Ok(())
}
}
pub trait MockStorageMethods {
fn get<M: Mappable + 'static>(
&self,
key: &M::Key,
) -> StorageResult<Option<std::borrow::Cow<'_, M::OwnedValue>>>;
fn contains_key<M: Mappable + 'static>(&self, key: &M::Key) -> StorageResult<bool>;
fn insert<M: Mappable + 'static>(
&mut self,
key: &M::Key,
value: &M::Value,
) -> StorageResult<Option<M::OwnedValue>>;
fn remove<M: Mappable + 'static>(
&mut self,
key: &M::Key,
) -> StorageResult<Option<M::OwnedValue>>;
fn root<Key: 'static, M: Mappable + 'static>(
&self,
key: &Key,
) -> StorageResult<MerkleRoot>;
}
mockall::mock! {
pub Storage {}
impl MockStorageMethods for Storage {
fn get<M: Mappable + 'static>(
&self,
key: &M::Key,
) -> StorageResult<Option<std::borrow::Cow<'static, M::OwnedValue>>>;
fn contains_key<M: Mappable + 'static>(&self, key: &M::Key) -> StorageResult<bool>;
fn insert<M: Mappable + 'static>(
&mut self,
key: &M::Key,
value: &M::Value,
) -> StorageResult<Option<M::OwnedValue>>;
fn remove<M: Mappable + 'static>(
&mut self,
key: &M::Key,
) -> StorageResult<Option<M::OwnedValue>>;
fn root<Key: 'static, M: Mappable + 'static>(&self, key: &Key) -> StorageResult<MerkleRoot>;
}
impl Transactional for Storage {
type Storage = Self;
fn transaction(&self) -> StorageTransaction<Self>;
}
impl Transaction<Self> for Storage {
fn commit(&mut self) -> StorageResult<()>;
}
}
impl MockStorage {
pub fn into_transactional(self) -> MockStorage {
let mut db = MockStorage::default();
db.expect_transaction()
.return_once(move || StorageTransaction::new(self));
db
}
}
impl AsRef<MockStorage> for MockStorage {
fn as_ref(&self) -> &MockStorage {
self
}
}
impl AsMut<MockStorage> for MockStorage {
fn as_mut(&mut self) -> &mut MockStorage {
self
}
}
impl<M> StorageInspect<M> for MockStorage
where
M: Mappable + 'static,
{
type Error = StorageError;
fn get(
&self,
key: &M::Key,
) -> StorageResult<Option<std::borrow::Cow<M::OwnedValue>>> {
MockStorageMethods::get::<M>(self, key)
}
fn contains_key(&self, key: &M::Key) -> StorageResult<bool> {
MockStorageMethods::contains_key::<M>(self, key)
}
}
impl<M> StorageMutate<M> for MockStorage
where
M: Mappable + 'static,
{
fn insert(
&mut self,
key: &M::Key,
value: &M::Value,
) -> StorageResult<Option<M::OwnedValue>> {
MockStorageMethods::insert::<M>(self, key, value)
}
fn remove(&mut self, key: &M::Key) -> StorageResult<Option<M::OwnedValue>> {
MockStorageMethods::remove::<M>(self, key)
}
}
impl<Key, M> MerkleRootStorage<Key, M> for MockStorage
where
Key: 'static,
M: Mappable + 'static,
{
fn root(&self, key: &Key) -> StorageResult<MerkleRoot> {
MockStorageMethods::root::<Key, M>(self, key)
}
}