imag-store: Use Err/Ok Result map utils to refactor code

This commit is contained in:
Matthias Beyer 2016-09-06 11:27:42 +02:00
parent a39f07f9a5
commit daca530dd5
2 changed files with 11 additions and 15 deletions

View file

@ -1,13 +1,12 @@
use std::path::PathBuf; use std::path::PathBuf;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::trace::trace_error_exit; use libimagerror::trace::MapErrTrace;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagutil::warn_exit::warn_exit; use libimagutil::warn_exit::warn_exit;
use libimagutil::warn_result::*;
pub fn delete(rt: &Runtime) { pub fn delete(rt: &Runtime) {
use std::process::exit;
rt.cli() rt.cli()
.subcommand_matches("delete") .subcommand_matches("delete")
.map(|sub| { .map(|sub| {
@ -15,15 +14,13 @@ pub fn delete(rt: &Runtime) {
.map(|id| { .map(|id| {
let path = PathBuf::from(id); let path = PathBuf::from(id);
let path = try!(StoreId::new(Some(rt.store().path().clone()), path) let path = try!(StoreId::new(Some(rt.store().path().clone()), path)
.map_err(|e| trace_error_exit(&e, 1))); .map_err_trace_exit(1));
debug!("Deleting file at {:?}", id); debug!("Deleting file at {:?}", id);
rt.store() rt.store()
.delete(path) .delete(path)
.map_err(|e| { .map_warn_err(|e| format!("Error: {:?}", e))
warn!("Error: {:?}", e); .map_err_trace_exit(1)
exit(1);
})
}) })
.or_else(|| warn_exit("No ID passed. Will exit now", 1)) .or_else(|| warn_exit("No ID passed. Will exit now", 1))
}) })

View file

@ -6,7 +6,8 @@ use toml::Value;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreId;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
use libimagerror::trace::{trace_error, trace_error_exit}; use libimagerror::trace::MapErrTrace;
use libimagutil::debug_result::*;
pub fn retrieve(rt: &Runtime) { pub fn retrieve(rt: &Runtime) {
rt.cli() rt.cli()
@ -16,18 +17,16 @@ pub fn retrieve(rt: &Runtime) {
.map(|id| { .map(|id| {
let path = PathBuf::from(id); let path = PathBuf::from(id);
let path = try!(StoreId::new(Some(rt.store().path().clone()), path) let path = try!(StoreId::new(Some(rt.store().path().clone()), path)
.map_err(|e| trace_error_exit(&e, 1))); .map_err_trace_exit(1));
debug!("path = {:?}", path); debug!("path = {:?}", path);
rt.store() rt.store()
// "id" must be present, enforced via clap spec // "id" must be present, enforced via clap spec
.retrieve(path) .retrieve(path)
.map(|e| print_entry(rt, scmd, e)) .map(|e| print_entry(rt, scmd, e))
.map_err(|e| { .map_dbg_str("No entry")
debug!("No entry."); .map_dbg(|e| format!("{:?}", e))
debug!("{}:", e); .map_err_trace()
trace_error(&e);
})
}) })
}); });
} }