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)
- Macro-parse ambiguity. The doctest above is marked
ignorebecause the macro’s internal@with_implarm has two competing repetition blocks ($variantfor wrapped types and$variant_extrafor free-form variants) that the parser cannot disambiguate cleanly.group!works in practice as exercised bytests/, but a top-level doctest invocation trips the ambiguity. The macro will be rewritten with unambiguous tokens in1.0. - Broken
ForgeErrordelegation. The generatedForgeErrorimpl tries to delegatekind/status_code/is_retryableto the wrapped variant’s ownForgeErrorimpl, but the type-erased downcast pattern used internally does not work as intended. In practice every wrapped variant gets the fallback values (stringify!($variant)forkind,500forstatus_code,falseforis_retryable). TheDisplay,Error::source(), andFrom<T>parts work correctly. The delegation will be rewritten in1.0to require: ForgeErroron each wrapped type and call its trait methods directly.