use crate::dek::persistence::DEKPersisterTrait;
use crate::error::generator::PersistError;
use async_trait::async_trait;
use std::sync::Arc;
pub struct CustomPersister {
persist_fn: Arc<
dyn (Fn(
Vec<u8>,
String
) -> futures::future::BoxFuture<'static, Result<Vec<u8>, PersistError>>) +
Send +
Sync
>,
fetch_fn: Arc<
dyn (Fn(String) -> futures::future::BoxFuture<'static, Result<Vec<u8>, PersistError>>) +
Send +
Sync
>,
}
impl CustomPersister {
pub fn new<P, F>(persist_fn: P, fetch_fn: F) -> Self
where
P: Fn(
Vec<u8>,
String
) -> futures::future::BoxFuture<'static, Result<Vec<u8>, PersistError>> +
Send +
Sync +
'static,
F: Fn(String) -> futures::future::BoxFuture<'static, Result<Vec<u8>, PersistError>> +
Send +
Sync +
'static
{
CustomPersister {
persist_fn: Arc::new(persist_fn),
fetch_fn: Arc::new(fetch_fn),
}
}
}
#[async_trait]
impl DEKPersisterTrait for CustomPersister {
async fn persist(&self, dek: &Vec<u8>, context: String) -> Result<Vec<u8>, PersistError> {
(self.persist_fn)(dek.clone(), context).await
}
async fn fetch(&self, context: String) -> Result<Vec<u8>, PersistError> {
(self.fetch_fn)(context).await
}
}