domani/src/origin/mux.rs

58 lines
1.5 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
S: origin::Store + Sync + Send + 'static,
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
{
mapping_fn: F,
stores: Vec<S>,
}
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,
{
2023-06-29 14:54:55 +00:00
pub fn new(mapping_fn: F, stores: Vec<S>) -> Store<F, S> {
Store { mapping_fn, stores }
}
}
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 all_descrs(&self) -> Result<Vec<origin::Descr>, origin::AllDescrsError> {
let mut res = Vec::<origin::Descr>::new();
for store in self.stores.iter() {
store.all_descrs()?.into_iter().collect_into(&mut res);
}
Ok(res)
}
2024-01-10 09:42:48 +00:00
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)
}
}