You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
use crate::error::unexpected::Mappable;
|
|
use crate::{origin, util};
|
|
|
|
pub struct Store<F, S>
|
|
where
|
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
|
{
|
|
mapping_fn: F,
|
|
}
|
|
|
|
impl<F, S> Store<F, S>
|
|
where
|
|
S: origin::Store + Sync + Send + 'static,
|
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
|
{
|
|
pub fn new(mapping_fn: F) -> Store<F, S> {
|
|
Store { mapping_fn }
|
|
}
|
|
}
|
|
|
|
impl<F, S> origin::Store for Store<F, S>
|
|
where
|
|
S: origin::Store + Sync + Send + 'static,
|
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
|
{
|
|
fn sync(&self, descr: &origin::Descr) -> util::BoxFuture<'_, Result<(), origin::SyncError>> {
|
|
let descr = descr.clone();
|
|
Box::pin(async move {
|
|
(self.mapping_fn)(&descr)
|
|
.or_unexpected_while(format!("mapping {:?} to store", &descr))?
|
|
.sync(&descr)
|
|
.await
|
|
})
|
|
}
|
|
|
|
fn get_file(
|
|
&self,
|
|
descr: &origin::Descr,
|
|
path: &str,
|
|
) -> Result<util::BoxByteStream, origin::GetFileError> {
|
|
(self.mapping_fn)(descr)
|
|
.or_unexpected_while(format!("mapping {:?} to store", &descr))?
|
|
.get_file(descr, path)
|
|
}
|
|
}
|
|
|