Remove the most ugly parts

This commit is contained in:
Matthias Beyer 2016-07-06 19:50:55 +02:00
parent fadeae5214
commit aef80874da

View file

@ -44,8 +44,7 @@ fn main() {
} }
}; };
let scmd = rt.cli().subcommand_name(); match rt.cli().subcommand_name() {
match scmd {
Some("tw-hook") => tw_hook(&rt), Some("tw-hook") => tw_hook(&rt),
Some("exec") => exec(&rt), Some("exec") => exec(&rt),
Some("list") => list(&rt), Some("list") => list(&rt),
@ -56,25 +55,25 @@ fn main() {
fn tw_hook(rt: &Runtime) { fn tw_hook(rt: &Runtime) {
let subcmd = rt.cli().subcommand_matches("tw-hook").unwrap(); let subcmd = rt.cli().subcommand_matches("tw-hook").unwrap();
if subcmd.is_present("add") { if subcmd.is_present("add") {
let stdin = stdin(); let stdin = stdin();
let mut stdin = stdin.lock(); let mut stdin = stdin.lock();
let mut line = String::new(); let mut line = String::new();
match stdin.read_line(&mut line) {
Ok(_) => { } if let Err(e) = stdin.read_line(&mut line) {
Err(e) => { trace_error(&e);
error!("{}", e); exit(1);
return;
}
}; };
if let Ok(ttask) = import_task(&line.as_str()) { if let Ok(ttask) = import_task(&line.as_str()) {
let uuid = *ttask.uuid(); match serde_json::ser::to_string(&ttask) {
println!("{}", match serde_json::ser::to_string(&ttask) { Ok(val) => println!("{}", val),
Ok(val) => val,
Err(e) => { Err(e) => {
error!("{}", e); trace_error(&e);
return; exit(1);
} }
}); }
let uuid = *ttask.uuid();
match ttask.into_filelockentry(rt.store()) { match ttask.into_filelockentry(rt.store()) {
Ok(val) => { Ok(val) => {
println!("Task {} stored in imag", uuid); println!("Task {} stored in imag", uuid);
@ -82,22 +81,20 @@ fn tw_hook(rt: &Runtime) {
}, },
Err(e) => { Err(e) => {
trace_error(&e); trace_error(&e);
error!("{}", e); exit(1);
return;
} }
}; };
} } else {
else {
error!("No usable input"); error!("No usable input");
return; exit(1);
} }
} } else if subcmd.is_present("delete") {
else if subcmd.is_present("delete") {
// The used hook is "on-modify". This hook gives two json-objects // The used hook is "on-modify". This hook gives two json-objects
// per usage und wants one (the second one) back. // per usage und wants one (the second one) back.
let mut counter = 0; let mut counter = 0;
let stdin = stdin(); let stdin = stdin();
let stdin = stdin.lock(); let stdin = stdin.lock();
if let Ok(ttasks) = import_tasks(stdin) { if let Ok(ttasks) = import_tasks(stdin) {
for ttask in ttasks { for ttask in ttasks {
if counter % 2 == 1 { if counter % 2 == 1 {
@ -105,23 +102,21 @@ fn tw_hook(rt: &Runtime) {
// task before the change, and the second one after // task before the change, and the second one after
// the change. The (maybe modified) second one is // the change. The (maybe modified) second one is
// expected by taskwarrior. // expected by taskwarrior.
println!("{}", match serde_json::ser::to_string(&ttask) { match serde_json::ser::to_string(&ttask) {
Ok(val) => val, Ok(val) => println!("{}", val),
Err(e) => { Err(e) => {
error!("{}", e); trace_error(&e);
return; exit(1);
} }
}); }
match ttask.status() { match ttask.status() {
&task_hookrs::status::TaskStatus::Deleted => { &task_hookrs::status::TaskStatus::Deleted => {
match libimagtodo::delete::delete(rt.store(), *ttask.uuid()) { match libimagtodo::delete::delete(rt.store(), *ttask.uuid()) {
Ok(_) => { Ok(_) => println!("Deleted task {}", *ttask.uuid()),
println!("Deleted task {}", *ttask.uuid());
}
Err(e) => { Err(e) => {
trace_error(&e); trace_error(&e);
error!("{}", e); exit(1);
return;
} }
} }
} }
@ -131,12 +126,10 @@ fn tw_hook(rt: &Runtime) {
} // end if c % 2 } // end if c % 2
counter += 1; counter += 1;
} // end for } // end for
} // end if let } else {
else {
error!("No usable input"); error!("No usable input");
} }
} } else {
else {
// Should not be possible, as one argument is required via // Should not be possible, as one argument is required via
// ArgGroup // ArgGroup
unreachable!(); unreachable!();
@ -167,48 +160,46 @@ fn exec(rt: &Runtime) {
} }
fn list(rt: &Runtime) { fn list(rt: &Runtime) {
let subcmd = rt.cli().subcommand_matches("list").unwrap(); let subcmd = rt.cli().subcommand_matches("list").unwrap();
let mut args = Vec::new(); let mut args = Vec::new();
let verbose = subcmd.is_present("verbose"); let verbose = subcmd.is_present("verbose");
let iter = match libimagtodo::read::get_todo_iterator(rt.store()) { let iter = match libimagtodo::read::get_todo_iterator(rt.store()) {
//let iter = match rt.store().retrieve_for_module("todo/taskwarrior") {
Err(e) => { Err(e) => {
error!("{}", e); trace_error(&e);
return; exit(1);
} }
Ok(val) => val, Ok(val) => val,
}; };
for task in iter { for task in iter {
match task { match task {
Ok(val) => { Ok(val) => {
//let val = libimagtodo::task::Task::new(fle);
//println!("{:#?}", val.flentry);
let uuid = match val.flentry.get_header().read("todo.uuid") { let uuid = match val.flentry.get_header().read("todo.uuid") {
Ok(Some(u)) => u, Ok(Some(u)) => u,
Ok(None) => continue, Ok(None) => continue,
Err(e) => { Err(e) => {
error!("{}", e); trace_error(&e);
continue; continue;
} }
}; };
if verbose { if verbose {
args.clear(); args.clear();
args.push(format!("uuid:{}", uuid)); args.push(format!("uuid:{}", uuid));
args.push(format!("{}", "information")); args.push(format!("{}", "information"));
let tw_process = Command::new("task").stdin(Stdio::null()).args(&args).spawn() let tw_process = Command::new("task").stdin(Stdio::null()).args(&args).spawn()
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
error!("{}", e); trace_error(&e);
panic!("failed"); panic!("failed");
}); });
let output = tw_process.wait_with_output().unwrap_or_else(|e| { let output = tw_process.wait_with_output().unwrap_or_else(|e| {
panic!("failed to unwrap output: {}", e); panic!("failed to unwrap output: {}", e);
}); });
let outstring = String::from_utf8(output.stdout).unwrap_or_else(|e| { let outstring = String::from_utf8(output.stdout).unwrap_or_else(|e| {
panic!("failed to ececute: {}", e); panic!("failed to execute: {}", e);
}); });
println!("{}", outstring); println!("{}", outstring);
} } else {
else {
println!("{}", match uuid { println!("{}", match uuid {
toml::Value::String(s) => s, toml::Value::String(s) => s,
_ => { _ => {
@ -219,7 +210,7 @@ fn list(rt: &Runtime) {
} }
} }
Err(e) => { Err(e) => {
error!("{}", e); trace_error(&e);
continue; continue;
} }
} // end match task } // end match task