domani/src/origin/mux.rs

51 lines
1.3 KiB
Rust
Raw Normal View History

use crate::error::unexpected::Mappable;
use crate::{origin, util};
2023-06-29 14:54:55 +00:00
pub struct Store<F, S>
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>
where
S: origin::Store + Sync + Send + 'static,
2023-06-29 14:54:55 +00:00
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
{
pub fn new(mapping_fn: F) -> Store<F, S> {
Store { mapping_fn }
2023-06-29 14:54:55 +00:00
}
}
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
})
}
2024-01-10 09:42:48 +00:00
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
})
}
}