Remove iterator types

With this patch, libimagentryannotation does not have special iterator
types anymore. This makes the whole thing more comfortable to use.

In imag-annotate, the parameter for the functioncall was removed.

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-02-10 00:38:52 +01:00
parent c84258da3d
commit d8cd10a384
4 changed files with 9 additions and 84 deletions

View file

@ -194,7 +194,7 @@ fn list(rt: &Runtime) {
.ok_or_else(|| EM::EntryNotFound(id.local_display_string())) .ok_or_else(|| EM::EntryNotFound(id.local_display_string()))
.map_err(Error::from) .map_err(Error::from)
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.annotations(rt.store()) .annotations()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.into_get_iter(rt.store()) .into_get_iter(rt.store())
.trace_unwrap_exit(1) .trace_unwrap_exit(1)

View file

@ -36,13 +36,12 @@ use failure::ResultExt;
use failure::Error; use failure::Error;
use failure::err_msg; use failure::err_msg;
use iter::*;
use module_path::ModuleEntryPath; use module_path::ModuleEntryPath;
pub trait Annotateable { pub trait Annotateable {
fn annotate<'a>(&mut self, store: &'a Store) -> Result<FileLockEntry<'a>>; fn annotate<'a>(&mut self, store: &'a Store) -> Result<FileLockEntry<'a>>;
fn denotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<Option<FileLockEntry<'a>>>; fn denotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result<Option<FileLockEntry<'a>>>;
fn annotations<'a>(&self, store: &'a Store) -> Result<AnnotationIter<'a>>; fn annotations(&self) -> Result<StoreIdIterator>;
fn is_annotation(&self) -> Result<bool>; fn is_annotation(&self) -> Result<bool>;
} }
@ -87,10 +86,14 @@ impl Annotateable for Entry {
} }
/// Get all annotations of an entry /// Get all annotations of an entry
fn annotations<'a>(&self, store: &'a Store) -> Result<AnnotationIter<'a>> { fn annotations(&self) -> Result<StoreIdIterator> {
self.get_internal_links() self.get_internal_links()
.map(|iter| StoreIdIterator::new(Box::new(iter.map(|e| e.get_store_id().clone()).map(Ok)))) .map(|it| {
.map(|i| AnnotationIter::new(i, store)) it.filter(|link| link.get_store_id().is_in_collection(&["annotation"]))
.map(|link| Ok(link.get_store_id().clone()))
})
.map(Box::new)
.map(|inner| StoreIdIterator::new(inner))
} }
fn is_annotation(&self) -> Result<bool> { fn is_annotation(&self) -> Result<bool> {

View file

@ -1,77 +0,0 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use toml_query::read::TomlValueReadTypeExt;
use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreIdIterator;
use libimagerror::errors::ErrorMsg as EM;
use failure::Fallible as Result;
use failure::ResultExt;
use failure::Error;
use failure::err_msg;
#[derive(Debug)]
pub struct AnnotationIter<'a>(StoreIdIterator, &'a Store);
impl<'a> AnnotationIter<'a> {
pub fn new(iter: StoreIdIterator, store: &'a Store) -> AnnotationIter<'a> {
AnnotationIter(iter, store)
}
}
impl<'a> Iterator for AnnotationIter<'a> {
type Item = Result<FileLockEntry<'a>>;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.0.next() {
None => return None, // iterator consumed
Some(Err(e)) => return Some(Err(e).map_err(Error::from)),
Some(Ok(id)) => match self.1.get(id) {
Err(e) => {
return Some(Err(e)
.context(err_msg("Store read error"))
.map_err(Error::from))
},
Ok(Some(entry)) => {
match entry
.get_header()
.read_bool("annotation.is_annotation")
.context(EM::EntryHeaderReadError)
.map_err(Error::from)
{
Ok(None) => continue, // not an annotation
Ok(Some(false)) => continue,
Ok(Some(true)) => return Some(Ok(entry)),
Err(e) => return Some(Err(e)),
}
},
Ok(None) => continue,
}
}
}
}
}

View file

@ -52,5 +52,4 @@ module_entry_path_mod!("annotations");
pub mod annotateable; pub mod annotateable;
pub mod annotation_fetcher; pub mod annotation_fetcher;
pub mod iter;