From d8cd10a384e3cb8674214b68a83c690d927b48ee Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 10 Feb 2019 00:38:52 +0100 Subject: [PATCH] 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 --- bin/core/imag-annotate/src/main.rs | 2 +- .../src/annotateable.rs | 13 ++-- lib/entry/libimagentryannotation/src/iter.rs | 77 ------------------- lib/entry/libimagentryannotation/src/lib.rs | 1 - 4 files changed, 9 insertions(+), 84 deletions(-) delete mode 100644 lib/entry/libimagentryannotation/src/iter.rs diff --git a/bin/core/imag-annotate/src/main.rs b/bin/core/imag-annotate/src/main.rs index 03153fb8..ae4dea6b 100644 --- a/bin/core/imag-annotate/src/main.rs +++ b/bin/core/imag-annotate/src/main.rs @@ -194,7 +194,7 @@ fn list(rt: &Runtime) { .ok_or_else(|| EM::EntryNotFound(id.local_display_string())) .map_err(Error::from) .map_err_trace_exit_unwrap(1) - .annotations(rt.store()) + .annotations() .map_err_trace_exit_unwrap(1) .into_get_iter(rt.store()) .trace_unwrap_exit(1) diff --git a/lib/entry/libimagentryannotation/src/annotateable.rs b/lib/entry/libimagentryannotation/src/annotateable.rs index fdc12821..3c915846 100644 --- a/lib/entry/libimagentryannotation/src/annotateable.rs +++ b/lib/entry/libimagentryannotation/src/annotateable.rs @@ -36,13 +36,12 @@ use failure::ResultExt; use failure::Error; use failure::err_msg; -use iter::*; use module_path::ModuleEntryPath; pub trait Annotateable { fn annotate<'a>(&mut self, store: &'a Store) -> Result>; fn denotate<'a>(&mut self, store: &'a Store, ann_name: &str) -> Result>>; - fn annotations<'a>(&self, store: &'a Store) -> Result>; + fn annotations(&self) -> Result; fn is_annotation(&self) -> Result; } @@ -87,10 +86,14 @@ impl Annotateable for Entry { } /// Get all annotations of an entry - fn annotations<'a>(&self, store: &'a Store) -> Result> { + fn annotations(&self) -> Result { self.get_internal_links() - .map(|iter| StoreIdIterator::new(Box::new(iter.map(|e| e.get_store_id().clone()).map(Ok)))) - .map(|i| AnnotationIter::new(i, store)) + .map(|it| { + 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 { diff --git a/lib/entry/libimagentryannotation/src/iter.rs b/lib/entry/libimagentryannotation/src/iter.rs deleted file mode 100644 index e7601a9c..00000000 --- a/lib/entry/libimagentryannotation/src/iter.rs +++ /dev/null @@ -1,77 +0,0 @@ -// -// imag - the personal information management suite for the commandline -// Copyright (C) 2015-2019 Matthias Beyer 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>; - - fn next(&mut self) -> Option { - 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, - } - } - } - } - -} - diff --git a/lib/entry/libimagentryannotation/src/lib.rs b/lib/entry/libimagentryannotation/src/lib.rs index e3be82b9..92da306f 100644 --- a/lib/entry/libimagentryannotation/src/lib.rs +++ b/lib/entry/libimagentryannotation/src/lib.rs @@ -52,5 +52,4 @@ module_entry_path_mod!("annotations"); pub mod annotateable; pub mod annotation_fetcher; -pub mod iter;