From 0fd8b3b286f42cccfa5728b13de2070a9252d35c Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 6 Aug 2016 11:39:31 +0200 Subject: [PATCH] Move deletion logic into lib --- imag-todo/src/main.rs | 41 +++-------------------------------------- libimagtodo/src/task.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/imag-todo/src/main.rs b/imag-todo/src/main.rs index dae6a34d..d5149ad8 100644 --- a/imag-todo/src/main.rs +++ b/imag-todo/src/main.rs @@ -60,44 +60,9 @@ fn tw_hook(rt: &Runtime) { // The used hook is "on-modify". This hook gives two json-objects // per usage und wants one (the second one) back. let stdin = stdin(); - let stdin = stdin.lock(); - - match import_tasks(stdin) { - Ok(ttasks) => for (counter, ttask) in ttasks.iter().enumerate() { - if counter % 2 == 1 { - // Only every second task is needed, the first one is the - // task before the change, and the second one after - // the change. The (maybe modified) second one is - // expected by taskwarrior. - match serde_json::ser::to_string(&ttask) { - Ok(val) => println!("{}", val), - Err(e) => { - trace_error(&e); - exit(1); - } - } - - // Taskwarrior does not have the concept of deleted tasks, but only modified - // ones. - // - // Here we check if the status of a task is deleted and if yes, we delete it - // from the store. - if *ttask.status() == TaskStatus::Deleted { - match Task::delete_by_uuid(rt.store(), *ttask.uuid()) { - Ok(_) => println!("Deleted task {}", *ttask.uuid()), - Err(e) => { - trace_error(&e); - exit(1); - } - } - } - } // end if c % 2 - }, - Err(e) => { - trace_error(&e); - exit(1); - }, - } + Task::delete_by_imports(rt.store(), stdin.lock()) + .map_err(|e| trace_error(&e)) + .ok(); } else { // Should not be possible, as one argument is required via // ArgGroup diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index 89db1b71..1b44259b 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -100,6 +100,43 @@ impl<'a> Task<'a> { }) } + pub fn delete_by_imports(store: &Store, r: R) -> Result<()> { + use serde_json::ser::to_string as serde_to_string; + use task_hookrs::status::TaskStatus; + + for (counter, res_ttask) in import_tasks(r).into_iter().enumerate() { + match res_ttask { + Ok(ttask) => { + if counter % 2 == 1 { + // Only every second task is needed, the first one is the + // task before the change, and the second one after + // the change. The (maybe modified) second one is + // expected by taskwarrior. + match serde_to_string(&ttask).map_err_into(TodoErrorKind::ImportError) { + // use println!() here, as we talk with TW + Ok(val) => println!("{}", val), + Err(e) => return Err(e), + } + + // Taskwarrior does not have the concept of deleted tasks, but only modified + // ones. + // + // Here we check if the status of a task is deleted and if yes, we delete it + // from the store. + if *ttask.status() == TaskStatus::Deleted { + match Task::delete_by_uuid(store, *ttask.uuid()) { + Ok(_) => info!("Deleted task {}", *ttask.uuid()), + Err(e) => return Err(e), + } + } + } // end if c % 2 + }, + Err(e) => return Err(e).map_err_into(TodoErrorKind::ImportError), + } + } + Ok(()) + } + pub fn delete_by_uuid(store: &Store, uuid: Uuid) -> Result<()> { store.delete(ModuleEntryPath::new(format!("taskwarrior/{}", uuid)).into_storeid()) .map_err(|e| TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e))))