#error #macro #try #wrapping #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

#21 in #ok

Download history 368/week @ 2024-07-16 378/week @ 2024-07-23 322/week @ 2024-07-30 224/week @ 2024-08-06 216/week @ 2024-08-13 274/week @ 2024-08-20 323/week @ 2024-08-27 295/week @ 2024-09-03 319/week @ 2024-09-10 27/week @ 2024-09-17 22/week @ 2024-09-24 2/week @ 2024-10-01

1,517 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