Move deletion logic into lib

This commit is contained in:
Matthias Beyer 2016-08-06 11:39:31 +02:00
parent 6222aac837
commit 0fd8b3b286
2 changed files with 40 additions and 38 deletions

View file

@ -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

View file

@ -100,6 +100,43 @@ impl<'a> Task<'a> {
})
}
pub fn delete_by_imports<R: BufRead>(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))))