From 7de2577725a27635491dfc7d83787bfc9dccd64f Mon Sep 17 00:00:00 2001 From: mario Date: Tue, 28 Jun 2016 20:13:11 +0200 Subject: [PATCH] implemented add-hook --- imag-todo/src/main.rs | 46 ++++++++++++++++++++++++++++++----------- libimagtodo/Cargo.toml | 2 +- libimagtodo/src/task.rs | 35 +++++++++++++++++++++++++------ 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/imag-todo/src/main.rs b/imag-todo/src/main.rs index 96f8eafc..38e5ef1d 100644 --- a/imag-todo/src/main.rs +++ b/imag-todo/src/main.rs @@ -1,19 +1,29 @@ extern crate clap; extern crate glob; -extern crate task_hookrs; #[macro_use] extern crate log; +extern crate serde_json; extern crate semver; extern crate toml; #[macro_use] extern crate version; +extern crate task_hookrs; + extern crate libimagrt; extern crate libimagstore; extern crate libimagutil; +extern crate libimagtodo; use std::process::exit; use std::process::{Command, Stdio}; +use std::io::stdin; + +use task_hookrs::import::import; +use task_hookrs::task::Task as TTask; use libimagrt::runtime::Runtime; +use libimagtodo::task::Task; +use libimagtodo::task::IntoTask; +use libimagutil::trace::trace_error; mod ui; @@ -24,7 +34,7 @@ fn main() { let version = &version!()[..]; let about = "Interface with taskwarrior"; let ui = build_ui(Runtime::get_default_cli_builder(name, version, about)); - + let rt = { let rt = Runtime::new(ui); if rt.is_ok() { @@ -36,20 +46,32 @@ fn main() { } }; - + let scmd = rt.cli().subcommand_name(); match scmd { Some("tw-hook") => { let subcmd = rt.cli().subcommand_matches("tw-hook").unwrap(); if subcmd.is_present("add") { - println!("To be implemented"); - // - // TODO @Kevin: import function aus task_hookrs benutzen, um - // stdin auszulesen, und dann auf dem - // task_hookrs::task::Task den Trait für die - // Umwandlung aufrufen. - // + if let Ok(ttasks) = task_hookrs::import::import(stdin()) { + for ttask in ttasks { + println!("{}", match serde_json::ser::to_string(&ttask) { + Ok(val) => val, + Err(e) => { + error!("{}", e); + return; + } + }); + let task = match ttask.into_filelockentry(rt.store()) { + Ok(val) => val, + Err(e) => { + trace_error(&e); + error!("{}", e); + return; + } + }; + } + } } else if subcmd.is_present("delete") { println!("To be implemented"); @@ -73,7 +95,7 @@ fn main() { let mut tw_process = Command::new("task").stdin(Stdio::null()).args(&args).spawn().unwrap_or_else(|e| { panic!("failed to execute taskwarrior: {}", e); }); - + let output = tw_process.wait_with_output().unwrap_or_else(|e| { panic!("failed to unwrap output: {}", e); }); @@ -87,6 +109,6 @@ fn main() { }, _ => panic!("Reached unreachable Code"), } - + } diff --git a/libimagtodo/Cargo.toml b/libimagtodo/Cargo.toml index dc1f6d8b..25e6386e 100644 --- a/libimagtodo/Cargo.toml +++ b/libimagtodo/Cargo.toml @@ -5,7 +5,7 @@ authors = ["mario "] [dependencies] semver = "0.2" -task-hookrs = { git = "https://github.com/matthiasbeyer/task-hookrs.git" } +task-hookrs = "0.1.0" uuid = "0.2.0" toml = "0.1.28" diff --git a/libimagtodo/src/task.rs b/libimagtodo/src/task.rs index 8c3b0886..e8384a73 100644 --- a/libimagtodo/src/task.rs +++ b/libimagtodo/src/task.rs @@ -1,4 +1,5 @@ use std::ops::Deref; +use std::collections::BTreeMap; use toml::Value; use task_hookrs::task::Task as TTask; @@ -49,13 +50,35 @@ impl<'a> IntoTask<'a> for TTask { let uuid = self.uuid(); let store_id = ModuleEntryPath::new(format!("taskwarrior/{}", uuid)).into_storeid(); match store.retrieve(store_id) { - Err(e) => Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))), - Ok(mut fle) => { - match fle.get_header_mut().set("todo.uuid", Value::String(format!("{}", uuid))) { - Err(e) => Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))), - Ok(_) => Ok(Task { flentry : fle }) - } + Err(e) => { + return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) }, + Ok(mut fle) => { + { + let mut header = fle.get_header_mut(); + match header.read("todo") { + Ok(None) => { + match header.set("todo", Value::Table(BTreeMap::new())) { + Ok(_) => { }, + Err(e) => { + return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) + } + } + } + Ok(Some(_)) => { } + Err(e) => { + } + } + match header.set("todo.uuid", Value::String(format!("{}",uuid))) { + Ok(_) => { }, + Err(e) => { + return Err(TodoError::new(TodoErrorKind::StoreError, Some(Box::new(e)))) + } + } + } + // If none of the errors above have returned the function, everything is fine + Ok(Task { flentry : fle } ) + } } } }