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;
 
// Define a custom error type for a specific module
#[derive(Debug, thiserror::Error)]
pub enum DatabaseError {
    #[error("Connection failed: {0}")]
    ConnectionFailed(String),
     
    #[error("Query failed: {0}")]
    QueryFailed(String),
}
 
// Group multiple error types into a parent error
group! {
    #[derive(Debug)]
    pub enum ServiceError {
        // Include the AppError
        App(AppError),
         
        // Include io::Error
        Io(io::Error),
         
        // Include the custom DatabaseError
        Database(DatabaseError),
    }
}