Rewrite .map_err_trace_exit().unwrap() with new helper fn
This commit is contained in:
parent
eb4681bf65
commit
815cde76d0
7 changed files with 33 additions and 57 deletions
|
@ -82,14 +82,13 @@ fn add(rt: &Runtime) {
|
||||||
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
|
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
|
||||||
let sid = PathBuf::from(entry_name)
|
let sid = PathBuf::from(entry_name)
|
||||||
.into_storeid()
|
.into_storeid()
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap(); // safed by above call
|
|
||||||
|
|
||||||
let c = {
|
let c = {
|
||||||
let parse = |value: &str| -> Vec<i8> {
|
let parse = |value: &str| -> Vec<i8> {
|
||||||
value.split(".")
|
value.split(".")
|
||||||
.map(FromStr::from_str)
|
.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>>()
|
.collect::<Vec<i8>>()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -104,8 +103,7 @@ fn add(rt: &Runtime) {
|
||||||
|
|
||||||
rt.store()
|
rt.store()
|
||||||
.get(sid)
|
.get(sid)
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1)
|
||||||
.unwrap() // safed by above call
|
|
||||||
.map(|mut entry| {
|
.map(|mut entry| {
|
||||||
let _ = entry.set_coordinates(c)
|
let _ = entry.set_coordinates(c)
|
||||||
.map_err_trace_exit(1);
|
.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 entry_name = scmd.value_of("entry").unwrap(); // safed by clap
|
||||||
let sid = PathBuf::from(entry_name)
|
let sid = PathBuf::from(entry_name)
|
||||||
.into_storeid()
|
.into_storeid()
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap(); // safed by above call
|
|
||||||
|
|
||||||
let removed_value = rt
|
let removed_value = rt
|
||||||
.store()
|
.store()
|
||||||
.get(sid)
|
.get(sid)
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1)
|
||||||
.unwrap() // safed by above call
|
|
||||||
.unwrap_or_else(|| { // if we have Ok(None)
|
.unwrap_or_else(|| { // if we have Ok(None)
|
||||||
error!("No such entry: {}", entry_name);
|
error!("No such entry: {}", entry_name);
|
||||||
exit(1)
|
exit(1)
|
||||||
})
|
})
|
||||||
.remove_coordinates()
|
.remove_coordinates()
|
||||||
.map_err_trace_exit(1) // The delete action failed
|
.map_err_trace_exit_unwrap(1) // The delete action failed
|
||||||
.unwrap() // safed by above call
|
|
||||||
.unwrap_or_else(|| { // if we have Ok(None)
|
.unwrap_or_else(|| { // if we have Ok(None)
|
||||||
error!("Entry had no coordinates: {}", entry_name);
|
error!("Entry had no coordinates: {}", entry_name);
|
||||||
exit(1)
|
exit(1)
|
||||||
})
|
})
|
||||||
.map_err_trace_exit(1) // The parsing of the deleted values failed
|
.map_err_trace_exit_unwrap(1); // The parsing of the deleted values failed
|
||||||
.unwrap(); // safed by above call
|
|
||||||
|
|
||||||
if scmd.is_present("print-removed") {
|
if scmd.is_present("print-removed") {
|
||||||
println!("{}", removed_value);
|
println!("{}", removed_value);
|
||||||
|
@ -157,21 +151,18 @@ fn get(rt: &Runtime) {
|
||||||
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
|
let entry_name = scmd.value_of("entry").unwrap(); // safed by clap
|
||||||
let sid = PathBuf::from(entry_name)
|
let sid = PathBuf::from(entry_name)
|
||||||
.into_storeid()
|
.into_storeid()
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap(); // safed by above call
|
|
||||||
|
|
||||||
let value = rt
|
let value = rt
|
||||||
.store()
|
.store()
|
||||||
.get(sid)
|
.get(sid)
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1)
|
||||||
.unwrap() // safed by above call
|
|
||||||
.unwrap_or_else(|| { // if we have Ok(None)
|
.unwrap_or_else(|| { // if we have Ok(None)
|
||||||
error!("No such entry: {}", entry_name);
|
error!("No such entry: {}", entry_name);
|
||||||
exit(1)
|
exit(1)
|
||||||
})
|
})
|
||||||
.get_coordinates()
|
.get_coordinates()
|
||||||
.map_err_trace_exit(1) // The get action failed
|
.map_err_trace_exit_unwrap(1) // The get action failed
|
||||||
.unwrap() // safed by above call
|
|
||||||
.unwrap_or_else(|| { // if we have Ok(None)
|
.unwrap_or_else(|| { // if we have Ok(None)
|
||||||
error!("Entry has no coordinates: {}", entry_name);
|
error!("Entry has no coordinates: {}", entry_name);
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
|
@ -72,16 +72,14 @@ fn main() {
|
||||||
.value_of("pattern")
|
.value_of("pattern")
|
||||||
.map(Regex::new)
|
.map(Regex::new)
|
||||||
.unwrap() // ensured by clap
|
.unwrap() // ensured by clap
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap(); // ensured by line above
|
|
||||||
|
|
||||||
let overall_count = rt
|
let overall_count = rt
|
||||||
.store()
|
.store()
|
||||||
.entries()
|
.entries()
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1)
|
||||||
.unwrap() // ensured by above line
|
|
||||||
.into_get_iter(rt.store())
|
.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()))
|
.filter(|entry| pattern.is_match(entry.get_content()))
|
||||||
.map(|entry| show(&entry, &pattern, &opts, &mut count))
|
.map(|entry| show(&entry, &pattern, &opts, &mut count))
|
||||||
.count();
|
.count();
|
||||||
|
|
|
@ -63,8 +63,7 @@ fn main() {
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.map(StoreId::new_baseless)
|
.map(StoreId::new_baseless)
|
||||||
.unwrap() // unwrap safe by clap
|
.unwrap() // unwrap safe by clap
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap(); //secures by above call
|
|
||||||
|
|
||||||
let destname = rt
|
let destname = rt
|
||||||
.cli()
|
.cli()
|
||||||
|
@ -72,8 +71,7 @@ fn main() {
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.map(StoreId::new_baseless)
|
.map(StoreId::new_baseless)
|
||||||
.unwrap() // unwrap safe by clap
|
.unwrap() // unwrap safe by clap
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap(); //secures by above call
|
|
||||||
|
|
||||||
let _ = rt
|
let _ = rt
|
||||||
.store()
|
.store()
|
||||||
|
|
|
@ -27,15 +27,14 @@ pub fn ids(rt: &Runtime) {
|
||||||
let _ :Vec<_> = rt
|
let _ :Vec<_> = rt
|
||||||
.store()
|
.store()
|
||||||
.entries()
|
.entries()
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1)
|
||||||
.unwrap() //safed
|
|
||||||
.map(|e| if full {
|
.map(|e| if full {
|
||||||
e.with_base(base.clone())
|
e.with_base(base.clone())
|
||||||
} else {
|
} else {
|
||||||
e.without_base()
|
e.without_base()
|
||||||
})
|
})
|
||||||
.map(|i| i.to_str())
|
.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))
|
.map(|i| println!("{}", i))
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,14 +91,12 @@ fn main() {
|
||||||
.cli()
|
.cli()
|
||||||
.value_of("in")
|
.value_of("in")
|
||||||
.ok_or_else::<VE, _>(|| "No viewer given".to_owned().into())
|
.ok_or_else::<VE, _>(|| "No viewer given".to_owned().into())
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap(); // saved by above call
|
|
||||||
|
|
||||||
let config = rt
|
let config = rt
|
||||||
.config()
|
.config()
|
||||||
.ok_or_else::<VE, _>(|| "No configuration, cannot continue".to_owned().into())
|
.ok_or_else::<VE, _>(|| "No configuration, cannot continue".to_owned().into())
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let query = format!("view.viewers.{}", viewer);
|
let query = format!("view.viewers.{}", viewer);
|
||||||
match config.config().read(&query) {
|
match config.config().read(&query) {
|
||||||
|
@ -113,26 +111,21 @@ fn main() {
|
||||||
handlebars.register_escape_fn(::handlebars::no_escape);
|
handlebars.register_escape_fn(::handlebars::no_escape);
|
||||||
|
|
||||||
let _ = handlebars.register_template_string("template", viewer_template)
|
let _ = handlebars.register_template_string("template", viewer_template)
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let file = {
|
let file = {
|
||||||
let mut tmpfile = tempfile::NamedTempFile::new()
|
let mut tmpfile = tempfile::NamedTempFile::new()
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap();
|
|
||||||
if view_header {
|
if view_header {
|
||||||
let hdr = toml::ser::to_string_pretty(entry.get_header())
|
let hdr = toml::ser::to_string_pretty(entry.get_header())
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap();
|
|
||||||
let _ = tmpfile.write(format!("---\n{}---\n", hdr).as_bytes())
|
let _ = tmpfile.write(format!("---\n{}---\n", hdr).as_bytes())
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if view_content {
|
if view_content {
|
||||||
let _ = tmpfile.write(entry.get_content().as_bytes())
|
let _ = tmpfile.write(entry.get_content().as_bytes())
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpfile
|
tmpfile
|
||||||
|
@ -143,19 +136,18 @@ fn main() {
|
||||||
.to_str()
|
.to_str()
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.ok_or::<VE>("Cannot build path".to_owned().into())
|
.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 command = {
|
||||||
let mut data = BTreeMap::new();
|
let mut data = BTreeMap::new();
|
||||||
data.insert("entry", file_path);
|
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 mut elems = call.split_whitespace();
|
||||||
let command_string = elems
|
let command_string = elems
|
||||||
.next()
|
.next()
|
||||||
.ok_or::<VE>("No command".to_owned().into())
|
.ok_or::<VE>("No command".to_owned().into())
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap();
|
|
||||||
let mut cmd = Command::new(command_string);
|
let mut cmd = Command::new(command_string);
|
||||||
|
|
||||||
for arg in elems {
|
for arg in elems {
|
||||||
|
@ -165,7 +157,7 @@ fn main() {
|
||||||
cmd
|
cmd
|
||||||
};
|
};
|
||||||
|
|
||||||
if !command.status().map_err_trace_exit(1).unwrap().success() {
|
if !command.status().map_err_trace_exit_unwrap(1).success() {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -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 = rt.cli().subcommand_matches("create").unwrap();
|
||||||
|
|
||||||
let create_timed = create.value_of("timed")
|
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)
|
.map(Some)
|
||||||
.unwrap_or_else(|| match get_diary_timed_config(rt, diaryname) {
|
.unwrap_or_else(|| match get_diary_timed_config(rt, diaryname) {
|
||||||
Err(e) => trace_error_exit(&e, 1),
|
Err(e) => trace_error_exit(&e, 1),
|
||||||
|
|
|
@ -74,8 +74,7 @@ fn create(rt: &Runtime) {
|
||||||
let mut note = rt
|
let mut note = rt
|
||||||
.store()
|
.store()
|
||||||
.new_note(name.clone(), String::new())
|
.new_note(name.clone(), String::new())
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1);
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
if rt.cli().subcommand_matches("create").unwrap().is_present("edit") {
|
if rt.cli().subcommand_matches("create").unwrap().is_present("edit") {
|
||||||
let _ = note
|
let _ = note
|
||||||
|
@ -97,8 +96,7 @@ fn edit(rt: &Runtime) {
|
||||||
let _ = rt
|
let _ = rt
|
||||||
.store()
|
.store()
|
||||||
.get_note(name.clone())
|
.get_note(name.clone())
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit_unwrap(1)
|
||||||
.unwrap()
|
|
||||||
.map(|mut note| {
|
.map(|mut note| {
|
||||||
let _ = note
|
let _ = note
|
||||||
.edit_content(rt)
|
.edit_content(rt)
|
||||||
|
@ -118,7 +116,7 @@ fn list(rt: &Runtime) {
|
||||||
.map_err_trace_exit(1)
|
.map_err_trace_exit(1)
|
||||||
.map(|iter| {
|
.map(|iter| {
|
||||||
let notes = 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| {
|
.sorted_by(|note_a, note_b| {
|
||||||
if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
|
if let (Ok(a), Ok(b)) = (note_a.get_name(), note_b.get_name()) {
|
||||||
return a.cmp(&b)
|
return a.cmp(&b)
|
||||||
|
|
Loading…
Reference in a new issue