use crate::error::unexpected::Mappable; use crate::{origin, util}; pub struct Store where S: origin::Store + Sync + Send + 'static, F: Fn(&origin::Descr) -> Option + Sync + Send, { mapping_fn: F, stores: Vec, } impl Store where S: origin::Store + Sync + Send + 'static, F: Fn(&origin::Descr) -> Option + Sync + Send, { pub fn new(mapping_fn: F, stores: Vec) -> Store { Store { mapping_fn, stores } } } impl origin::Store for Store where S: origin::Store + Sync + Send + 'static, F: Fn(&origin::Descr) -> Option + 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, origin::AllDescrsError> { let mut res = Vec::::new(); for store in self.stores.iter() { store.all_descrs()?.into_iter().collect_into(&mut res); } Ok(res) } fn get_file( &self, descr: &origin::Descr, path: &str, ) -> Result { (self.mapping_fn)(descr) .or_unexpected_while(format!("mapping {:?} to store", &descr))? .get_file(descr, path) } }