Add id iterator

This commit is contained in:
Matthias Beyer 2018-04-10 22:32:50 +02:00
parent bb0d4319c3
commit c242a362e4
2 changed files with 47 additions and 0 deletions

View file

@ -35,6 +35,7 @@
while_true, while_true,
)] )]
extern crate filters;
extern crate toml; extern crate toml;
extern crate toml_query; extern crate toml_query;
#[macro_use] extern crate error_chain; #[macro_use] extern crate error_chain;

View file

@ -19,9 +19,14 @@
use std::path::PathBuf; use std::path::PathBuf;
use filters::filter::Filter;
use libimagstore::store::Store; use libimagstore::store::Store;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::IntoStoreId;
use libimagstore::storeid::StoreId;
use libimagstore::storeid::StoreIdIteratorWithStore;
use error::WikiError as WE; use error::WikiError as WE;
use error::Result; use error::Result;
@ -46,6 +51,47 @@ impl<'a, 'b> Wiki<'a, 'b> {
self.0.retrieve(sid).map_err(WE::from) self.0.retrieve(sid).map_err(WE::from)
} }
pub fn all_ids(&self) -> Result<WikiIdIterator> {
let filter = IdIsInWikiFilter(self.1);
Ok(WikiIdIterator(self.0.entries()?, filter))
}
}
pub struct WikiIdIterator<'a>(StoreIdIteratorWithStore<'a>, IdIsInWikiFilter<'a>);
impl<'a> Iterator for WikiIdIterator<'a> {
type Item = StoreId;
fn next(&mut self) -> Option<Self::Item> {
while let Some(next) = self.0.next() {
if self.1.filter(&next) {
return Some(next)
}
}
None
}
}
pub struct IdIsInWikiFilter<'a>(&'a str);
impl<'a> IdIsInWikiFilter<'a> {
pub fn new(wiki_name: &'a str) -> Self {
IdIsInWikiFilter(wiki_name)
}
}
impl<'a> Filter<StoreId> for IdIsInWikiFilter<'a> {
fn filter(&self, id: &StoreId) -> bool {
id.is_in_collection(&["wiki", &self.0])
}
}
impl<'a> Filter<Entry> for IdIsInWikiFilter<'a> {
fn filter(&self, e: &Entry) -> bool {
e.get_location().is_in_collection(&["wiki", &self.0])
}
} }