Derive Macro medea_jason::utils::Caused

source ·
#[derive(Caused)]
{
    // Attributes available to this derive:
    #[cause]
}
Expand description

Generate implementation of Caused trait for errors represented as enum.

How to use

1. Declare custom error and enum for error variants.

The cause() method returns error if nested error has its type declared as an argument of the attribute #[cause(error = path::to::Error)] or the error type is assumed to be imported as Error.

use medea_jason::utils::Caused;

struct MyError;

#[derive(Caused)]
#[cause(error = MyError)]
enum FooError {
    Internal,
    MyError(MyError),
}

let err = FooError::Internal;
assert!(err.cause().is_none());

let err = FooError::MyError(MyError {});
assert!(err.cause().is_some());

If enum variant has attribute #[cause] it will call the cause() method on nested error.

#[derive(Caused)]
#[cause(error = MyError)]
enum BarError {
    Foo(#[cause] FooError),
}

let err = BarError::Foo(FooError::Internal);
assert!(err.cause().is_none());

let err = BarError::Foo(FooError::MyError(MyError {}));
assert!(err.cause().is_some());