#wrapping #try #macro #ok #wrap #input #error

try_wrap_ok

? or try! macro, with an additional Ok wrapping

1 unstable release

0.1.1 Aug 20, 2019
0.1.0 Aug 20, 2019

#24 in #ok

LGPL-3.0-only

14KB

try_wrap_ok

Crates.io License

This crate provides a macro similar to the old try! macro, or to the ?, except it wraps the error in an additional Ok.

This is useful if you want to use a ? for easily returning say, invalid input errors, but you can't do so because you have an additional Result level for handling internal errors of a different nature.

This macro allows you to do so:

fn foo(input: Input) -> Result<Result<FinalOutput, InvalidInputError>, DatabaseError> {
    let validated_input: ValidatedInput = try_wrap_ok!(validate_input_with_database(input)?);
    Ok(Ok(do_stuff_with_validated_input(validated_input)?))
}

fn validate_input_with_database(input: Input) -> Result<Result<ValidatedInput, InvalidInputError>, DatabaseError>;

lib.rs:

This crate provides a macro similar to the old try! macro, or to the ?, except it wraps the error in an additional Ok.

This is useful if you want to use a ? for easily returning say, invalid input errors, but you can't do so because you have an additional Result level for handling internal errors of a different nature.

This macro allows you to do so:

fn foo(input: Input) -> Result<Result<FinalOutput, InvalidInputError>, DatabaseError> {
    let validated_input: ValidatedInput = try_wrap_ok!(validate_input_with_database(input)?);
    Ok(Ok(do_stuff_with_validated_input(validated_input)?))
}

fn validate_input_with_database(input: Input) -> Result<Result<ValidatedInput, InvalidInputError>, DatabaseError>;

No runtime deps