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

try_or_wrap_s

? or try! macro, with an additional wrapping of the error in something else

2 unstable releases

0.2.0 Sep 4, 2019
0.1.0 Sep 4, 2019

#1758 in Rust patterns

Download history 431/week @ 2024-03-24 455/week @ 2024-03-31 382/week @ 2024-04-07 424/week @ 2024-04-14 470/week @ 2024-04-21 329/week @ 2024-04-28 235/week @ 2024-05-05 386/week @ 2024-05-12 298/week @ 2024-05-19 362/week @ 2024-05-26 496/week @ 2024-06-02 482/week @ 2024-06-09 453/week @ 2024-06-16 398/week @ 2024-06-23 400/week @ 2024-06-30 373/week @ 2024-07-07

1,637 downloads per month

LGPL-3.0-only

14KB

try_or_wrap

Crates.io License

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

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_or_wrap!(validate_input_with_database(input)?, Ok);
    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 something else.

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_or_wrap!(validate_input_with_database(input)?, Ok);
    Ok(Ok(do_stuff_with_validated_input(validated_input)?))
}

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

No runtime deps