Rewrite .map_err_trace_exit().unwrap() with new helper fn

This commit is contained in:
Matthias Beyer 2017-10-12 19:08:36 +02:00
parent eb4681bf65
commit 815cde76d0
7 changed files with 33 additions and 57 deletions

View file

@ -82,14 +82,13 @@ fn add(rt: &Runtime) {
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
let sid = PathBuf::from(entry_name)
.into_storeid()
.map_err_trace_exit(1)
.unwrap(); // safed by above call
.map_err_trace_exit_unwrap(1);
let c = {
let parse = |value: &str| -> Vec<i8> {
value.split(".")
.map(FromStr::from_str)
.map(|elem| elem.map_err_trace_exit(1).unwrap())
.map(|elem| elem.map_err_trace_exit_unwrap(1))
.collect::<Vec<i8>>()
};
@ -104,8 +103,7 @@ fn add(rt: &Runtime) {
rt.store()
.get(sid)
.map_err_trace_exit(1)
.unwrap() // safed by above call
.map_err_trace_exit_unwrap(1)
.map(|mut entry| {
let _ = entry.set_coordinates(c)
.map_err_trace_exit(1);
@ -122,27 +120,23 @@ fn remove(rt: &Runtime) {
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
let sid = PathBuf::from(entry_name)
.into_storeid()
.map_err_trace_exit(1)
.unwrap(); // safed by above call
.map_err_trace_exit_unwrap(1);
let removed_value = rt
.store()
.get(sid)
.map_err_trace_exit(1)
.unwrap() // safed by above call
.map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| { // if we have Ok(None)
error!("No such entry: {}", entry_name);
exit(1)
})
.remove_coordinates()
.map_err_trace_exit(1) // The delete action failed
.unwrap() // safed by above call
.map_err_trace_exit_unwrap(1) // The delete action failed
.unwrap_or_else(|| { // if we have Ok(None)
error!("Entry had no coordinates: {}", entry_name);
exit(1)
})
.map_err_trace_exit(1) // The parsing of the deleted values failed
.unwrap(); // safed by above call
.map_err_trace_exit_unwrap(1); // The parsing of the deleted values failed
if scmd.is_present("print-removed") {
println!("{}", removed_value);
@ -157,21 +151,18 @@ fn get(rt: &Runtime) {
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
let sid = PathBuf::from(entry_name)
.into_storeid()
.map_err_trace_exit(1)
.unwrap(); // safed by above call
.map_err_trace_exit_unwrap(1);
let value = rt
.store()
.get(sid)
.map_err_trace_exit(1)
.unwrap() // safed by above call
.map_err_trace_exit_unwrap(1)
.unwrap_or_else(|| { // if we have Ok(None)
error!("No such entry: {}", entry_name);
exit(1)
})
.get_coordinates()
.map_err_trace_exit(1) // The get action failed
.unwrap() // safed by above call
.map_err_trace_exit_unwrap(1) // The get action failed
.unwrap_or_else(|| { // if we have Ok(None)
error!("Entry has no coordinates: {}", entry_name);
exit(1)

View file

@ -72,16 +72,14 @@ fn main() {
.value_of("pattern")
.map(Regex::new)
.unwrap() // ensured by clap
.map_err_trace_exit(1)
.unwrap(); // ensured by line above
.map_err_trace_exit_unwrap(1);
let overall_count = rt
.store()
.entries()
.map_err_trace_exit(1)
.unwrap() // ensured by above line
.map_err_trace_exit_unwrap(1)
.into_get_iter(rt.store())
.filter_map(|res| res.map_err_trace_exit(1).unwrap())
.filter_map(|res| res.map_err_trace_exit_unwrap(1))
.filter(|entry| pattern.is_match(entry.get_content()))
.map(|entry| show(&entry, &pattern, &opts, &mut count))
.count();

View file

@ -63,8 +63,7 @@ fn main() {
.map(PathBuf::from)
.map(StoreId::new_baseless)
.unwrap() // unwrap safe by clap
.map_err_trace_exit(1)
.unwrap(); //secures by above call
.map_err_trace_exit_unwrap(1);
let destname = rt
.cli()
@ -72,8 +71,7 @@ fn main() {
.map(PathBuf::from)
.map(StoreId::new_baseless)
.unwrap() // unwrap safe by clap
.map_err_trace_exit(1)
.unwrap(); //secures by above call
.map_err_trace_exit_unwrap(1);
let _ = rt
.store()

View file

@ -27,15 +27,14 @@ pub fn ids(rt: &Runtime) {
let _ :Vec<_> = rt
.store()
.entries()
.map_err_trace_exit(1)
.unwrap() //safed
.map_err_trace_exit_unwrap(1)
.map(|e| if full {
e.with_base(base.clone())
} else {
e.without_base()
e.without_base()
})
.map(|i| i.to_str())
.map(|elem| elem.map_err_trace_exit(1).unwrap())
.map(|elem| elem.map_err_trace_exit_unwrap(1))
.map(|i| println!("{}", i))
.collect();
}

View file

@ -91,14 +91,12 @@ fn main() {
.cli()
.value_of("in")
.ok_or_else::<VE, _>(|| "No viewer given".to_owned().into())
.map_err_trace_exit(1)
.unwrap(); // saved by above call
.map_err_trace_exit_unwrap(1);
let config = rt
.config()
.ok_or_else::<VE, _>(|| "No configuration, cannot continue".to_owned().into())
.map_err_trace_exit(1)
.unwrap();
.map_err_trace_exit_unwrap(1);
let query = format!("view.viewers.{}", viewer);
match config.config().read(&query) {
@ -113,26 +111,21 @@ fn main() {
handlebars.register_escape_fn(::handlebars::no_escape);
let _ = handlebars.register_template_string("template", viewer_template)
.map_err_trace_exit(1)
.unwrap();
.map_err_trace_exit_unwrap(1);
let file = {
let mut tmpfile = tempfile::NamedTempFile::new()
.map_err_trace_exit(1)
.unwrap();
.map_err_trace_exit_unwrap(1);
if view_header {
let hdr = toml::ser::to_string_pretty(entry.get_header())
.map_err_trace_exit(1)
.unwrap();
.map_err_trace_exit_unwrap(1);
let _ = tmpfile.write(format!("---\n{}---\n", hdr).as_bytes())
.map_err_trace_exit(1)
.unwrap();
.map_err_trace_exit_unwrap(1);
}
if view_content {
let _ = tmpfile.write(entry.get_content().as_bytes())
.map_err_trace_exit(1)
.unwrap();
.map_err_trace_exit_unwrap(1);
}
tmpfile
@ -143,19 +136,18 @@ fn main() {
.to_str()
.map(String::from)
.ok_or::<VE>("Cannot build path".to_owned().into())
.map_err_trace_exit(1).unwrap();
.map_err_trace_exit_unwrap(1);
let mut command = {
let mut data = BTreeMap::new();
data.insert("entry", file_path);
let call = handlebars.render("template", &data).map_err_trace_exit(1).unwrap();
let call = handlebars.render("template", &data).map_err_trace_exit_unwrap(1);
let mut elems = call.split_whitespace();
let command_string = elems
.next()
.ok_or::<VE>("No command".to_owned().into())
.map_err_trace_exit(1)
.unwrap();
.map_err_trace_exit_unwrap(1);
let mut cmd = Command::new(command_string);
for arg in elems {
@ -165,7 +157,7 @@ fn main() {
cmd
};
if !command.status().map_err_trace_exit(1).unwrap().success() {
if !command.status().map_err_trace_exit_unwrap(1).success() {
exit(1)
}
},

View file

@ -63,7 +63,7 @@ fn create_entry<'a>(diary: &'a Store, diaryname: &str, rt: &Runtime) -> FileLock
let create = rt.cli().subcommand_matches("create").unwrap();
let create_timed = create.value_of("timed")
.map(|t| parse_timed_string(t, diaryname).map_err_trace_exit(1).unwrap())
.map(|t| parse_timed_string(t, diaryname).map_err_trace_exit_unwrap(1))
.map(Some)
.unwrap_or_else(|| match get_diary_timed_config(rt, diaryname) {
Err(e) => trace_error_exit(&e, 1),

View file

@ -74,8 +74,7 @@ fn create(rt: &Runtime) {
let mut note = rt
.store()
.new_note(name.clone(), String::new())
.map_err_trace_exit(1)
.unwrap();
.map_err_trace_exit_unwrap(1);
if rt.cli().subcommand_matches("create").unwrap().is_present("edit") {
let _ = note
@ -97,8 +96,7 @@ fn edit(rt: &Runtime) {
let _ = rt
.store()
.get_note(name.clone())
.map_err_trace_exit(1)
.unwrap()
.map_err_trace_exit_unwrap(1)
.map(|mut note| {
let _ = note
.edit_content(rt)
@ -118,7 +116,7 @@ fn list(rt: &Runtime) {
.map_err_trace_exit(1)
.map(|iter| {
let notes = iter
.filter_map(|noteid| rt.store().get(noteid).map_err_trace_exit(1).unwrap())
.filter_map(|noteid| rt.store().get(noteid).map_err_trace_exit_unwrap(1))
.sorted_by(|note_a, note_b| {
if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
return a.cmp(&b)