2023-06-25 15:51:05 +00:00
|
|
|
use crate::error::unexpected::Mappable;
|
2023-07-16 14:09:37 +00:00
|
|
|
use crate::{origin, util};
|
2023-06-25 15:51:05 +00:00
|
|
|
|
2023-06-29 14:54:55 +00:00
|
|
|
pub struct Store<F, S>
|
2023-06-25 15:51:05 +00:00
|
|
|
where
|
|
|
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
|
|
|
{
|
|
|
|
mapping_fn: F,
|
|
|
|
}
|
|
|
|
|
2023-06-29 14:54:55 +00:00
|
|
|
impl<F, S> Store<F, S>
|
2023-06-25 15:51:05 +00:00
|
|
|
where
|
2024-01-21 15:18:31 +00:00
|
|
|
S: origin::Store + Sync + Send + 'static,
|
2023-06-29 14:54:55 +00:00
|
|
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
2023-06-25 15:51:05 +00:00
|
|
|
{
|
2024-01-22 15:48:57 +00:00
|
|
|
pub fn new(mapping_fn: F) -> Store<F, S> {
|
|
|
|
Store { mapping_fn }
|
2023-06-29 14:54:55 +00:00
|
|
|
}
|
2023-06-25 15:51:05 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 17:03:51 +00:00
|
|
|
impl<F, S> origin::Store for Store<F, S>
|
2023-06-25 15:51:05 +00:00
|
|
|
where
|
2024-01-21 15:18:31 +00:00
|
|
|
S: origin::Store + Sync + Send + 'static,
|
2023-06-25 15:51:05 +00:00
|
|
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
|
|
|
{
|
2024-01-21 15:18:31 +00:00
|
|
|
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
|
|
|
|
})
|
2023-06-25 15:51:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 09:42:48 +00:00
|
|
|
fn get_file(
|
|
|
|
&self,
|
2023-07-16 14:09:37 +00:00
|
|
|
descr: &origin::Descr,
|
|
|
|
path: &str,
|
2024-01-30 17:47:09 +00:00
|
|
|
) -> util::BoxFuture<Result<util::BoxByteStream, origin::GetFileError>> {
|
|
|
|
let descr = descr.clone();
|
|
|
|
let path = path.to_string();
|
|
|
|
Box::pin(async move {
|
|
|
|
(self.mapping_fn)(&descr)
|
|
|
|
.or_unexpected_while(format!("mapping {:?} to store", &descr))?
|
|
|
|
.get_file(&descr, &path)
|
|
|
|
.await
|
|
|
|
})
|
2023-07-06 17:19:51 +00:00
|
|
|
}
|
2023-06-25 15:51:05 +00:00
|
|
|
}
|