try_or_log_err

Macro try_or_log_err 

Source
macro_rules! try_or_log_err {
    { $($tokens:tt)* } => { ... };
}
Expand description

Tries to evaluate a block that returns Result<T, Text>

If the block returns Ok, this macro will resolve to T. If it returns Err, it will log the error with context::error!, then it will return from the function. As an example, this:

use duat::prelude::*;

let ret = try_or_log_err! {
    let value = result_fn()?;
    value
};

fn result_fn() -> Result<usize, Text> {
    Err(txt!(":("))
}

Will expand into:

use duat::prelude::*;

let ret = match (|| -> Result<_, Text> { Ok(result_fn()?) })() {
    Ok(ret) => ret,
    Err(err) => {
        context::error!("{err}");
        return;
    }
};

fn result_fn() -> Result<usize, Text> {
    Err(txt!(":("))
}

Note the Ok wrapping the tokens, so it works like the try keyword in that regard.