Rewrite use of iterator with own iterator implementation

This commit is contained in:
Matthias Beyer 2018-04-20 20:59:39 +02:00
parent bae9188a08
commit 255f4211c9
3 changed files with 55 additions and 4 deletions

View file

@ -0,0 +1,48 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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 libimagstore::storeid::StoreIdIterator;
use libimagstore::storeid::StoreId;
pub struct TaskIdIterator(StoreIdIterator);
impl TaskIdIterator {
pub fn new(inner: StoreIdIterator) -> Self {
TaskIdIterator(inner)
}
}
impl Iterator for TaskIdIterator {
type Item = StoreId;
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.0.next() {
None => return None,
Some(n) => if n.is_in_collection(&["todo", "taskwarrior"]) {
return Some(n)
}, // else continue
}
}
}
}

View file

@ -51,4 +51,5 @@ module_entry_path_mod!("todo");
pub mod error;
pub mod task;
pub mod taskstore;
pub mod iter;

View file

@ -28,13 +28,14 @@ use task_hookrs::task::Task as TTask;
use task_hookrs::import::{import_task, import_tasks};
use libimagstore::store::{FileLockEntry, Store};
use libimagstore::storeid::{IntoStoreId, StoreIdIterator};
use libimagstore::storeid::IntoStoreId;
use module_path::ModuleEntryPath;
use error::TodoErrorKind as TEK;
use error::TodoError as TE;
use error::Result;
use error::ResultExt;
use iter::TaskIdIterator;
/// Task struct containing a `FileLockEntry`
pub trait TaskStore<'a> {
@ -46,7 +47,7 @@ pub trait TaskStore<'a> {
fn retrieve_task_from_string(&'a self, s: String) -> Result<FileLockEntry<'a>>;
fn delete_tasks_by_imports<R: BufRead>(&self, r: R) -> Result<()>;
fn delete_task_by_uuid(&self, uuid: Uuid) -> Result<()>;
fn all_tasks(&self) -> Result<StoreIdIterator>;
fn all_tasks(&self) -> Result<TaskIdIterator>;
fn new_from_twtask(&'a self, task: TTask) -> Result<FileLockEntry<'a>>;
}
@ -167,8 +168,9 @@ impl<'a> TaskStore<'a> for Store {
.chain_err(|| TEK::StoreError)
}
fn all_tasks(&self) -> Result<StoreIdIterator> {
self.retrieve_for_module("todo/taskwarrior")
fn all_tasks(&self) -> Result<TaskIdIterator> {
self.entries()
.map(|i| TaskIdIterator::new(i.without_store()))
.chain_err(|| TEK::StoreError)
}