StackError

Derive Macro StackError 

Source
#[derive(StackError)]
{
    // Attributes available to this derive:
    #[error]
}
Expand description

Derive macro that implements StackError, Display, Debug and std::error::Error and generates From<T> impls for fields/variants configured via #[error(..)]. Derive macro for stack errors.

This derive macro can be applied to structs and enums. Unit, tuple and named-field variants are supported equally.

The macro will expand to implementations of StackError, Display, Debug and Error. It will also add From impls for fields configured via the error attribute.

Items with the derive macro applied accept an #[error(args)] attribute, where args is a comma-separated list of arguments. The supported arguments vary by on the kind of item:

  • on enums:
    • #[error(from_sources)]: Creates From impls for the source types of all variants. Will fail to compile if multiple sources have the same type.
    • #[error(std_sources)]: Defaults all sources to be std errors instead of stack errors.
  • on structs and enum variants:
    • #[error("format {field}: {}", a + b)]: Sets the display formatting. You can refer to named fields by their names, and to tuple fields by _0, _1 etc.
    • #[error(transparent)]: Directly forwards the display implementation to the error source, and omits the outer error in the source chain when reporting errors.
  • on fields:
    • #[error(from)]: Creates a From impl for the field’s type to the error type.
    • #[error(source)]: The error’s source method returns a reference to whatever field is named source, or has the source attribute set.
    • #[error(std_err)]: Only on on source fields. Marks the error as a std error. Source fields not marked as std_err need to implement StackError.
    • #[error(stack_error)]: Only on source fields. Marks the error as a StackError. This is the default unless std_sources is set on the top-level item.
    • #[error(meta)]: Sets a field as the meta field for this error or variant. Must have type [::n0_error::Error`]