summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-02-01 20:22:02 +0100
committerGitHub <noreply@github.com>2018-02-01 20:22:02 +0100
commit4c9add9deb1f32f07a031e83863aaac0580e73a9 (patch)
tree79711c7233231d28bbdcc8c979a217bfe1cfe966
parentda4b823048c91e30c8b4ed8eb2e3915fb29ce747 (diff)
parentda0c1fa5948a1174b4d01494531e05abba631166 (diff)
downloadimag-4c9add9deb1f32f07a031e83863aaac0580e73a9.zip
imag-4c9add9deb1f32f07a031e83863aaac0580e73a9.tar.gz
Merge pull request #1222 from matthiasbeyer/libimagstore/improve-iterator-extensions
libimagstore: improve iterator extensions
-rw-r--r--bin/core/imag-diagnostics/src/main.rs3
-rw-r--r--bin/core/imag-grep/src/main.rs3
-rw-r--r--lib/core/libimagstore/src/iter.rs15
-rw-r--r--lib/core/libimagstore/src/store.rs7
-rw-r--r--lib/core/libimagstore/src/storeid.rs67
-rw-r--r--lib/domain/libimaghabit/src/iter.rs13
-rw-r--r--lib/entry/libimagentrylink/src/internal.rs3
-rw-r--r--lib/entry/libimagentrymarkdown/src/processor.rs10
8 files changed, 99 insertions, 22 deletions
diff --git a/bin/core/imag-diagnostics/src/main.rs b/bin/core/imag-diagnostics/src/main.rs
index 7d3b06d..3f746cb 100644
--- a/bin/core/imag-diagnostics/src/main.rs
+++ b/bin/core/imag-diagnostics/src/main.rs
@@ -45,7 +45,6 @@ use libimagrt::setup::generate_runtime_setup;
use libimagerror::trace::MapErrTrace;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
-use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagstore::error::StoreError as Error;
use libimagentrylink::internal::*;
@@ -101,7 +100,7 @@ fn main() {
let diags = rt.store()
.entries()
.map_err_trace_exit_unwrap(1)
- .into_get_iter(rt.store())
+ .into_get_iter()
.map(|e| {
e.map_err_trace_exit_unwrap(1)
.ok_or(Error::from("Unable to get entry".to_owned()))
diff --git a/bin/core/imag-grep/src/main.rs b/bin/core/imag-grep/src/main.rs
index 6e92b60..e51f1cf 100644
--- a/bin/core/imag-grep/src/main.rs
+++ b/bin/core/imag-grep/src/main.rs
@@ -42,7 +42,6 @@ extern crate libimagerror;
use regex::Regex;
use libimagrt::setup::generate_runtime_setup;
-use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagstore::store::Entry;
use libimagerror::trace::MapErrTrace;
@@ -77,7 +76,7 @@ fn main() {
.store()
.entries()
.map_err_trace_exit_unwrap(1)
- .into_get_iter(rt.store())
+ .into_get_iter()
.filter_map(|res| res.map_err_trace_exit_unwrap(1))
.filter(|entry| pattern.is_match(entry.get_content()))
.map(|entry| show(&entry, &pattern, &opts, &mut count))
diff --git a/lib/core/libimagstore/src/iter.rs b/lib/core/libimagstore/src/iter.rs
index d42c92f..460ebf0 100644
--- a/lib/core/libimagstore/src/iter.rs
+++ b/lib/core/libimagstore/src/iter.rs
@@ -34,6 +34,12 @@ macro_rules! mk_iterator {
pub struct $itername<'a>(Box<Iterator<Item = StoreId>>, &'a Store);
+ impl<'a> $itername<'a> {
+ pub fn new(inner: Box<Iterator<Item = StoreId>>, store: &'a Store) -> Self {
+ $itername(inner, store)
+ }
+ }
+
impl<'a> Iterator for $itername<'a> {
type Item = Result<$yield>;
@@ -196,18 +202,14 @@ mod compile_test {
}
fn test_compile_get() {
- use super::get::StoreIdGetIteratorExtension;
-
let store = store();
let _ = store
.entries()
.unwrap()
- .into_get_iter(&store);
+ .into_get_iter();
}
fn test_compile_get_result() {
- use super::get::StoreIdGetResultIteratorExtension;
-
fn to_result(e: StoreId) -> Result<StoreId, ()> {
Ok(e)
}
@@ -216,8 +218,7 @@ mod compile_test {
let _ = store
.entries()
.unwrap()
- .map(to_result)
- .into_get_iter(&store);
+ .into_get_iter();
}
}
diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs
index 365576e..0a292ef 100644
--- a/lib/core/libimagstore/src/store.rs
+++ b/lib/core/libimagstore/src/store.rs
@@ -40,7 +40,7 @@ use toml_query::read::TomlValueReadTypeExt;
use error::{StoreError as SE, StoreErrorKind as SEK};
use error::ResultExt;
-use storeid::{IntoStoreId, StoreId, StoreIdIterator};
+use storeid::{IntoStoreId, StoreId, StoreIdIterator, StoreIdIteratorWithStore};
use file_abstraction::FileAbstractionInstance;
// We re-export the following things so tests can use them
@@ -721,7 +721,7 @@ impl Store {
}
/// Get _all_ entries in the store (by id as iterator)
- pub fn entries(&self) -> Result<StoreIdIterator> {
+ pub fn entries<'a>(&'a self) -> Result<StoreIdIteratorWithStore<'a>> {
self.backend
.pathes_recursively(self.path().clone())
.and_then(|iter| {
@@ -738,9 +738,8 @@ impl Store {
elems.push(sid);
}
}
- Ok(StoreIdIterator::new(Box::new(elems.into_iter())))
+ Ok(StoreIdIteratorWithStore::new(Box::new(elems.into_iter()), self))
})
-
}
/// Gets the path where this store is on the disk
diff --git a/lib/core/libimagstore/src/storeid.rs b/lib/core/libimagstore/src/storeid.rs
index dde25a2..b97c4c7 100644
--- a/lib/core/libimagstore/src/storeid.rs
+++ b/lib/core/libimagstore/src/storeid.rs
@@ -30,6 +30,12 @@ use error::StoreErrorKind as SEK;
use error::StoreError as SE;
use error::ResultExt;
use store::Result;
+use store::Store;
+
+use iter::create::StoreCreateIterator;
+use iter::delete::StoreDeleteIterator;
+use iter::get::StoreGetIterator;
+use iter::retrieve::StoreRetrieveIterator;
/// The Index into the Store
#[derive(Debug, Clone, Hash, Eq, PartialOrd, Ord)]
@@ -260,6 +266,67 @@ impl Iterator for StoreIdIterator {
}
+pub struct StoreIdIteratorWithStore<'a>(StoreIdIterator, &'a Store);
+
+impl<'a> Deref for StoreIdIteratorWithStore<'a> {
+ type Target = StoreIdIterator;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl<'a> Iterator for StoreIdIteratorWithStore<'a> {
+ type Item = StoreId;
+
+ fn next(&mut self) -> Option<StoreId> {
+ self.0.next()
+ }
+}
+
+impl<'a> StoreIdIteratorWithStore<'a> {
+
+ pub fn new(iter: Box<Iterator<Item = StoreId>>, store: &'a Store) -> Self {
+ StoreIdIteratorWithStore(StoreIdIterator::new(iter), store)
+ }
+
+ pub fn without_store(self) -> StoreIdIterator {
+ self.0
+ }
+
+ /// Transform the iterator into a StoreCreateIterator
+ ///
+ /// This immitates the API from `libimagstore::iter`.
+ pub fn into_create_iter(self) -> StoreCreateIterator<'a> {
+ StoreCreateIterator::new(Box::new(self.0), self.1)
+ }
+
+ /// Transform the iterator into a StoreDeleteIterator
+ ///
+ ///
+ /// This immitates the API from `libimagstore::iter`.
+ pub fn into_delete_iter(self) -> StoreDeleteIterator<'a> {
+ StoreDeleteIterator::new(Box::new(self.0), self.1)
+ }
+
+ /// Transform the iterator into a StoreGetIterator
+ ///
+ ///
+ /// This immitates the API from `libimagstore::iter`.
+ pub fn into_get_iter(self) -> StoreGetIterator<'a> {
+ StoreGetIterator::new(Box::new(self.0), self.1)
+ }
+
+ /// Transform the iterator into a StoreRetrieveIterator
+ ///
+ ///
+ /// This immitates the API from `libimagstore::iter`.
+ pub fn into_retrieve_iter(self) -> StoreRetrieveIterator<'a> {
+ StoreRetrieveIterator::new(Box::new(self.0), self.1)
+ }
+
+}
+
#[cfg(test)]
mod test {
use std::path::PathBuf;
diff --git a/lib/domain/libimaghabit/src/iter.rs b/lib/domain/libimaghabit/src/iter.rs
index 9d5d740..71f7220 100644
--- a/lib/domain/libimaghabit/src/iter.rs
+++ b/lib/domain/libimaghabit/src/iter.rs
@@ -18,6 +18,7 @@
//
use libimagstore::storeid::StoreIdIterator;
+use libimagstore::storeid::StoreIdIteratorWithStore;
use libimagstore::storeid::StoreId;
use util::IsHabitCheck;
@@ -43,6 +44,12 @@ impl From<StoreIdIterator> for HabitTemplateStoreIdIterator {
}
}
+impl<'a> From<StoreIdIteratorWithStore<'a>> for HabitTemplateStoreIdIterator {
+ fn from(sii: StoreIdIteratorWithStore<'a>) -> Self {
+ HabitTemplateStoreIdIterator(sii.without_store())
+ }
+}
+
pub struct HabitInstanceStoreIdIterator(StoreIdIterator);
impl HabitInstanceStoreIdIterator {
@@ -70,3 +77,9 @@ impl From<StoreIdIterator> for HabitInstanceStoreIdIterator {
}
}
+impl<'a> From<StoreIdIteratorWithStore<'a>> for HabitInstanceStoreIdIterator {
+ fn from(sii: StoreIdIteratorWithStore<'a>) -> Self {
+ HabitInstanceStoreIdIterator(sii.without_store())
+ }
+}
+
diff --git a/lib/entry/libimagentrylink/src/internal.rs b/lib/entry/libimagentrylink/src/internal.rs
index d7cddb6..371419c 100644
--- a/lib/entry/libimagentrylink/src/internal.rs
+++ b/lib/entry/libimagentrylink/src/internal.rs
@@ -616,7 +616,6 @@ pub mod store_check {
use internal::InternalLinker;
use libimagstore::storeid::StoreId;
- use libimagstore::iter::get::StoreIdGetIteratorExtension;
use libimagutil::debug_result::DebugResult;
// Helper data structure to collect incoming and outgoing links for each StoreId
@@ -636,7 +635,7 @@ pub mod store_check {
let aggregate_link_network = |store: &Store| -> Result<HashMap<StoreId, Linking>> {
store
.entries()?
- .into_get_iter(store)
+ .into_get_iter()
.fold(Ok(HashMap::new()), |map, element| {
map.and_then(|mut map| {
debug!("Checking element = {:?}", element);
diff --git a/lib/entry/libimagentrymarkdown/src/processor.rs b/lib/entry/libimagentrymarkdown/src/processor.rs
index 965635c..cdc7f3e 100644
--- a/lib/entry/libimagentrymarkdown/src/processor.rs
+++ b/lib/entry/libimagentrymarkdown/src/processor.rs
@@ -381,7 +381,7 @@ mod tests {
let entries = store.entries();
assert!(entries.is_ok());
- let entries : Vec<_> = entries.unwrap().collect();
+ let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries);
@@ -420,7 +420,7 @@ mod tests {
let entries = store.entries();
assert!(entries.is_ok());
- let entries : Vec<_> = entries.unwrap().collect();
+ let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(2, entries.len(), "Expected 2 links, got: {:?}", entries);
println!("{:?}", entries);
@@ -453,7 +453,7 @@ mod tests {
let entries = store.entries();
assert!(entries.is_ok());
- let entries : Vec<_> = entries.unwrap().collect();
+ let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(3, entries.len(), "Expected 3 links, got: {:?}", entries);
println!("{:?}", entries);
@@ -486,7 +486,7 @@ mod tests {
let entries = store.entries();
assert!(entries.is_ok());
- let entries : Vec<_> = entries.unwrap().collect();
+ let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(1, entries.len(), "Expected 1 entries, got: {:?}", entries);
println!("{:?}", entries);
@@ -514,7 +514,7 @@ mod tests {
let entries = store.entries();
assert!(entries.is_ok());
- let entries : Vec<_> = entries.unwrap().collect();
+ let entries : Vec<_> = entries.unwrap().without_store().collect();
assert_eq!(1, entries.len(), "Expected 1 entries, got: {:?}", entries);
}