I'm not sure how to idiomatically handle the following expressions that return Result
s. This is in a main function which is parsing comand-line args, so this is the end of the line so to speak. I don't want to propagate errors but rather handle them here. The first match statement is fine. Within the Ok
arm of the match, the two functions that return Result
s will rarely fail so I don't want the verbosity of nested match
es.
...
// normalized _path is defined above but left out for brevity...
// server.request is a an associated struct method defined elsewhere as well.
match server.request(&method.to_string(), normalized_path) {
Ok(response) => {
let json: ureq::serde_json::Value = response.into_json().unwrap();
println!("{}", to_string_pretty(&json).unwrap());
},
Err(err) => {
// print error to stderr and exit. The error is probably due to a non-ok HTTP response.
How do I concisely handle the two unwrap
s, exiting non-zero but with a useful error message? The first panic could happen because of some IO error, the second could happen because of malformed JSON. I don't want more nested matches. Do I want to use and_then
since, the first unwrapped value is fed into the next function?