summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-09-06 11:09:42 +0200
committerMatthias Beyer <mail@beyermatthias.de>2016-09-08 13:57:09 +0200
commit23a42dcb0d1ccce630b572fb017080992a074a69 (patch)
tree64b52e0def5c9baa977e3ffd2b377c53cb8f2cee
parentd5d83400fab342df1f658ab55cb15ebab4481eee (diff)
imag-link: Use Err/Ok map utility functions to refactor code
-rw-r--r--imag-link/src/main.rs67
1 files changed, 20 insertions, 47 deletions
diff --git a/imag-link/src/main.rs b/imag-link/src/main.rs
index 7f036e6e..a30758e5 100644
--- a/imag-link/src/main.rs
+++ b/imag-link/src/main.rs
@@ -34,10 +34,11 @@ use libimagstore::error::StoreError;
use libimagstore::store::Entry;
use libimagstore::store::FileLockEntry;
use libimagstore::store::Store;
-use libimagerror::trace::{trace_error, trace_error_exit};
+use libimagerror::trace::{MapErrTrace, trace_error, trace_error_exit};
use libimagentrylink::external::ExternalLinker;
use libimagutil::warn_result::*;
use libimagutil::warn_exit::warn_exit;
+use libimagutil::info_result::*;
use clap::ArgMatches;
use url::Url;
@@ -89,7 +90,7 @@ fn handle_internal_linking(rt: &Runtime) {
println!("{: <3}: {}", i, link);
}
})
- .map_err(|e| trace_error(&e))
+ .map_err_trace()
.ok();
},
@@ -236,38 +237,17 @@ fn handle_external_linking(rt: &Runtime) {
}
fn add_link_to_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
- let link = matches.value_of("add").unwrap();
-
- let link = Url::parse(link);
- if link.is_err() {
- debug!("URL parsing error...");
- trace_error_exit(&link.unwrap_err(), 1);
- }
- let link = link.unwrap();
-
- if let Err(e) = entry.add_external_link(store, link) {
- debug!("Error while adding external link...");
- trace_error(&e);
- } else {
- debug!("Everything worked well");
- info!("Ok");
- }
+ Url::parse(matches.value_of("add").unwrap())
+ .map_err_trace_exit(1)
+ .map(|link| entry.add_external_link(store, link).map_err_trace().map_info_str("Ok"))
+ .ok();
}
fn remove_link_from_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
- let link = matches.value_of("remove").unwrap();
-
- let link = Url::parse(link);
- if link.is_err() {
- trace_error_exit(&link.unwrap_err(), 1);
- }
- let link = link.unwrap();
-
- if let Err(e) = entry.remove_external_link(store, link) {
- trace_error(&e);
- } else {
- info!("Ok");
- }
+ Url::parse(matches.value_of("remove").unwrap())
+ .map_err_trace_exit(1)
+ .map(|link| entry.remove_external_link(store, link).map_err_trace().map_info_str("Ok"))
+ .ok();
}
fn set_links_for_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLockEntry) {
@@ -289,29 +269,22 @@ fn set_links_for_entry(store: &Store, matches: &ArgMatches, entry: &mut FileLock
.filter_map(|x| x)
.collect();
- if let Err(e) = entry.set_external_links(store, links) {
- trace_error(&e);
- } else {
- info!("Ok");
- }
+ entry.set_external_links(store, links)
+ .map_err_trace()
+ .map_info_str("Ok")
+ .ok();
}
fn list_links_for_entry(store: &Store, entry: &mut FileLockEntry) {
- let res = entry.get_external_links(store)
+ entry.get_external_links(store)
.and_then(|links| {
for (i, link) in links.iter().enumerate() {
println!("{: <3}: {}", i, link);
}
Ok(())
- });
-
- match res {
- Err(e) => {
- trace_error(&e);
- },
- Ok(_) => {
- info!("Ok");
- },
- }
+ })
+ .map_err_trace()
+ .map_info_str("Ok")
+ .ok();
}