1pub mod rocksdb;
2
3use lazy_static::lazy_static;
4use mockall::automock;
5use owning_ref::MutexGuardRefMut;
6use std::sync::Mutex;
7
8lazy_static! {
9 static ref ABCI_STORAGE_INSTANCE: Mutex<Option<AbciStorageType>> = Mutex::new(None);
10}
11
12type AbciStorageType = Box<dyn AbciStorage + Send>;
13type CustomStorageResult<T> = Result<T, Box<dyn std::error::Error>>;
14
15#[automock]
16pub trait AbciStorage {
17 fn write(&mut self, key: Vec<u8>, data: Vec<u8>) -> CustomStorageResult<()>;
18
19 fn get(&mut self, key: Vec<u8>) -> CustomStorageResult<Option<Vec<u8>>>;
20}
21
22pub fn set_abci_storage_instance<'ret>(
24 new_instance: AbciStorageType,
25) -> Result<
26 MutexGuardRefMut<'ret, Option<AbciStorageType>, AbciStorageType>,
27 Box<dyn std::error::Error>,
28> {
29 let mut instance = ABCI_STORAGE_INSTANCE.lock()?;
30 *instance = Some(new_instance);
31 let res = MutexGuardRefMut::new(instance).map_mut(|mg| mg.as_mut().unwrap());
34 Ok(res)
35}
36
37pub fn get_abci_storage_instance<'ret>() -> Result<
39 MutexGuardRefMut<'ret, Option<AbciStorageType>, AbciStorageType>,
40 Box<dyn std::error::Error>,
41> {
42 let instance = ABCI_STORAGE_INSTANCE.lock()?;
43 if instance.is_none() {
44 panic!("abci storage instance has not been set, execute set_storage_instance before calling this function");
46 }
47 let res = MutexGuardRefMut::new(instance).map_mut(|mg| mg.as_mut().unwrap());
50 Ok(res)
51}