This can be used in any case where your function:
- calls a function which returns a
Result
- the success can be returned immediately
- the error can be handled before returning from the function
For example:
use std::path::Path;
use std::io::Result;
use std::io::ErrorKind;
use std::fs::File;
/// [r]eturn [s]uccess
macro_rules! rs {
($t:expr) => {
// <omitted>
// NOTE: do we `Ok` wrap the success??
}
}
fn open_or_create_file(path: impl AsRef<Path>) -> Result<File> {
let error = rs!(File::open(&path));
match error.kind() {
ErrorKind::NotFound => File::create(path),
_ => Err(error),
}
}