Skip to main content

group

Macro group 

Source
macro_rules! group {
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident {
            $(
                $(#[$vmeta:meta])*
                $variant:ident($source_type:ty)
            ),* $(,)?
        }
    ) => { ... };
    (@with_impl
        $(#[$meta:meta])* $vis:vis enum $name:ident {
            $(
                $(#[$vmeta:meta])*
                $variant:ident($source_type:ty),
            )*
            $(
                $(#[$vmeta_extra:meta])*
                $variant_extra:ident $({$($field:ident: $field_type:ty),*})?,
            )*
        }
        $($impl_variant:ident $impl_type:ty)*
    ) => { ... };
}
Expand description

Macro for composing multi-error enums with automatic From<OtherError> conversions.

This macro allows you to create a parent error type that can wrap multiple other error types, automatically implementing From conversions for each of them.

§Example

use error_forge::{group, AppError};
use std::io;

group! {
    #[derive(Debug)]
    pub enum ServiceError {
        App(AppError),
        Io(io::Error),
    }
}

§Known limitations (scheduled for 1.0)

  1. Macro-parse ambiguity. The doctest above is marked ignore because the macro’s internal @with_impl arm has two competing repetition blocks ($variant for wrapped types and $variant_extra for free-form variants) that the parser cannot disambiguate cleanly. group! works in practice as exercised by tests/, but a top-level doctest invocation trips the ambiguity. The macro will be rewritten with unambiguous tokens in 1.0.
  2. Broken ForgeError delegation. The generated ForgeError impl tries to delegate kind / status_code / is_retryable to the wrapped variant’s own ForgeError impl, but the type-erased downcast pattern used internally does not work as intended. In practice every wrapped variant gets the fallback values (stringify!($variant) for kind, 500 for status_code, false for is_retryable). The Display, Error::source(), and From<T> parts work correctly. The delegation will be rewritten in 1.0 to require : ForgeError on each wrapped type and call its trait methods directly.