97 lines
2.5 KiB
Rust
97 lines
2.5 KiB
Rust
|
use crate::error::unexpected::Mappable;
|
||
|
use crate::origin::{self, store};
|
||
|
use std::sync;
|
||
|
|
||
|
struct Store<F, S>
|
||
|
where
|
||
|
S: store::Store,
|
||
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
||
|
{
|
||
|
mapping_fn: F,
|
||
|
stores: Vec<S>,
|
||
|
}
|
||
|
|
||
|
pub fn new<F, S>(mapping_fn: F, stores: Vec<S>) -> Box<dyn store::Store>
|
||
|
where
|
||
|
S: store::Store + 'static,
|
||
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send + 'static,
|
||
|
{
|
||
|
Box::new(Store { mapping_fn, stores })
|
||
|
}
|
||
|
|
||
|
impl<F, S> store::Store for Store<F, S>
|
||
|
where
|
||
|
S: store::Store,
|
||
|
F: Fn(&origin::Descr) -> Option<S> + Sync + Send,
|
||
|
{
|
||
|
fn sync(&self, descr: origin::Descr, limits: store::Limits) -> Result<(), store::SyncError> {
|
||
|
(self.mapping_fn)(&descr)
|
||
|
.or_unexpected_while(format!("fetching store for {:?}", &descr))?
|
||
|
.sync(descr, limits)
|
||
|
}
|
||
|
|
||
|
fn get(&self, descr: origin::Descr) -> Result<sync::Arc<dyn origin::Origin>, store::GetError> {
|
||
|
(self.mapping_fn)(&descr)
|
||
|
.or_unexpected_while(format!("fetching store for {:?}", &descr))?
|
||
|
.get(descr)
|
||
|
}
|
||
|
|
||
|
fn all_descrs(&self) -> Result<Vec<origin::Descr>, store::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)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use crate::origin::{self, store};
|
||
|
use mockall::predicate;
|
||
|
use std::{cell, rc};
|
||
|
|
||
|
#[test]
|
||
|
fn basic() {
|
||
|
let descrA = origin::Descr::Git {
|
||
|
url: "A".to_string(),
|
||
|
branch_name: "A".to_string(),
|
||
|
};
|
||
|
|
||
|
//let descrB = origin::Descr::Git {
|
||
|
// url: "B".to_string(),
|
||
|
// branch_name: "B".to_string(),
|
||
|
//};
|
||
|
|
||
|
let sA_mock = rc::Rc::new(store::MockStore::new());
|
||
|
|
||
|
//let mut sB = store::MockStore::new();
|
||
|
|
||
|
let s = {
|
||
|
let sA: rc::Rc<dyn store::Store> = sA_mock.clone();
|
||
|
let sA = sA.as_ref();
|
||
|
|
||
|
super::new(
|
||
|
|descr| match descr {
|
||
|
&origin::Descr::Git { ref url, .. } if url == "A" => Some(sA),
|
||
|
//&origin::Descr::Git { ref url, .. } if url == "B" => Some(sB),
|
||
|
_ => None,
|
||
|
},
|
||
|
//vec![sA],
|
||
|
)
|
||
|
};
|
||
|
|
||
|
sA_mock
|
||
|
.expect_sync()
|
||
|
.with(predicate::eq(descrA.clone()), predicate::always())
|
||
|
.times(1)
|
||
|
.return_const(Ok::<(), store::SyncError>(()));
|
||
|
|
||
|
assert_eq!(Ok(()), s.sync(descrA, store::Limits {}))
|
||
|
}
|
||
|
}
|
||
|
*/
|