Attribute Macro hb_macros::context

source ·
#[context]
Expand description

Converts Errors returned by the function into the correct type for the function as well adding a context message provided. There are many different situations that need to be handled. The context message can also have a special format to use variables in the context message.

The context message format

The context message is treated as string, but it also allows the use of curly braces to inject variables into the message. In the example below the msg parameter to the function is injected into the context message.

#[context("could not find message {msg}")]
fn find_msg(msg: String) -> Result<bool, FindError> {...}

Return expressions and Fall Through values`

Return expressions are modified to convert the returned result into the type expected by the function using the hb_error::ConvertInto function. In addition, the error is also given the provided context message.

    return example_error();

into…

    return hb_error::ConvertInto::<Result<(), ExampleError>>::convert(example_error()).map_err(|e| e.msg("some context message"));

Similarly fall through values are handled similarly but with extra scaffolding around it so that the compiler knows what type is expected in the block of the function and can show appropriate error messages.

fn something() -> Result<(), ExampleError> {
    example_error
}

into…

fn something() -> Result<(), ExampleError> {
    #[allow(unreachable_code)]
    let ret: Result<(), ExampleError> = {
        #[warn(unreachable_code)]
        example_error
    };
    #[allow(unreachable_code)]
    ret.map_err(|er| er.make_inner().msg("Some context message."))
}

Try expressions (? operator)

Try expressions are modified to convert the returned result into the type expected by the function using the hb_error::ConvertInto function. In addition, the error is also given the provided context message.

    let a = example_error()?;

into…

    let a = example_error().make_inner().msg("some context message")?;

Special Handling for Ok

The Ok return type needs special handling because the compiler cannot infer the return type with the extra scaffolding that has been put around the main block of the function. To compensate for this the Ok value is given a full path qualifier. As the path syntax changes based on how it is used, a alias is defined at the begining of the function (HbErrorContextType).

    Ok(())

into…

    type HbErrorContextType = Result<(), ExampleError>;
    HbErrorContextType::Ok(())