Brian Picciano
2302e9ff64
This includes a refactoring of get_file to be completely async, as well as to add a new error case.
51 lines
1.3 KiB
Rust
51 lines
1.3 KiB
Rust
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,
|
|
) -> 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
|
|
})
|
|
}
|
|
}
|