Rewrite internal linking handling to use less unwrap()

This commit is contained in:
Matthias Beyer 2016-10-31 23:55:17 +01:00
parent fa64c2d27d
commit b90dabaa97

View file

@ -88,80 +88,77 @@ fn handle_internal_linking(rt: &Runtime) {
debug!("Handle internal linking call"); debug!("Handle internal linking call");
let cmd = rt.cli().subcommand_matches("internal").unwrap(); let cmd = rt.cli().subcommand_matches("internal").unwrap();
if cmd.is_present("list") { match cmd.value_of("list") {
debug!("List..."); Some(list) => {
for entry in cmd.value_of("list").unwrap().split(',') { debug!("List...");
debug!("Listing for '{}'", entry); for entry in list.split(',') {
match get_entry_by_name(rt, entry) { debug!("Listing for '{}'", entry);
Ok(Some(e)) => { match get_entry_by_name(rt, entry) {
e.get_internal_links() Ok(Some(e)) => {
.map(|links| { e.get_internal_links()
let i = links .map(|links| {
.filter_map(|l| { let i = links
l.to_str() .filter_map(|l| {
.map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e)) l.to_str()
.ok() .map_warn_err(|e| format!("Failed to convert StoreId to string: {:?}", e))
}) .ok()
.enumerate(); })
.enumerate();
for (i, link) in i { for (i, link) in i {
println!("{: <3}: {}", i, link); println!("{: <3}: {}", i, link);
} }
}) })
.map_err_trace() .map_err_trace()
.ok(); .ok();
}, },
Ok(None) => { Ok(None) => {
warn!("Entry not found: {:?}", entry); warn!("Entry not found: {:?}", entry);
break; break;
}
Err(e) => {
trace_error(&e);
break;
},
} }
Err(e) => {
trace_error(&e);
break;
},
} }
debug!("Listing ready!");
},
None => {
let mut from = match get_from_entry(&rt) {
None => warn_exit("No 'from' entry", 1),
Some(s) => s,
};
debug!("Link from = {:?}", from.deref());
let to = match get_to_entries(&rt) {
None => warn_exit("No 'to' entry", 1),
Some(to) => to,
};
debug!("Link to = {:?}", to.iter().map(|f| f.deref()).collect::<Vec<&Entry>>());
match cmd.subcommand_name() {
Some("add") => {
for mut to_entry in to {
if let Err(e) = to_entry.add_internal_link(&mut from) {
trace_error_exit(&e, 1);
}
}
},
Some("remove") => {
for mut to_entry in to {
if let Err(e) = to_entry.remove_internal_link(&mut from) {
trace_error_exit(&e, 1);
}
}
},
_ => unreachable!(),
};
} }
debug!("Listing ready!");
} else {
let mut from = {
let from = get_from_entry(&rt);
if from.is_none() {
warn_exit("No 'from' entry", 1);
}
from.unwrap()
};
debug!("Link from = {:?}", from.deref());
let to = {
let to = get_to_entries(&rt);
if to.is_none() {
warn_exit("No 'to' entry", 1);
}
to.unwrap()
};
debug!("Link to = {:?}", to.iter().map(|f| f.deref()).collect::<Vec<&Entry>>());
match cmd.subcommand_name() {
Some("add") => {
for mut to_entry in to {
if let Err(e) = to_entry.add_internal_link(&mut from) {
trace_error_exit(&e, 1);
}
}
},
Some("remove") => {
for mut to_entry in to {
if let Err(e) = to_entry.remove_internal_link(&mut from) {
trace_error_exit(&e, 1);
}
}
},
_ => unreachable!(),
};
} }
} }