Skip to content

Commit

Permalink
add more support for attributes to impl_error
Browse files Browse the repository at this point in the history
- support individual error attributes.
- support composite error variant attributes.
  • Loading branch information
joseluis committed Jan 12, 2025
1 parent 9a96d52 commit be8e9e2
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/code/util/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,35 @@
macro_rules! impl_error {
(
// Defines a standalone error tuple-struct with elements.
individual: $struct_vis:vis struct $struct_name:ident
individual:
$(#[$attributes:meta])*
$struct_vis:vis struct $struct_name:ident
$(( $($e_vis:vis $e_ty:ty),+ $(,)? ))? $(;$($_a:lifetime)?)? // tuple-struct↓
$({ $($(#[$f_attr:meta])* $f_vis:vis $f_name:ident: $f_ty:ty),+ $(,)? })? // field-struct↑
$DOC_NAME:ident = $doc_str:literal,
$self:ident + $fmt:ident => $display_expr:expr
$(,)?
) => {
$(#[$attributes])*
$crate::CONST! { pub(crate) $DOC_NAME = $doc_str; }

$(#[$attributes])*
#[doc = crate::TAG_ERROR!()]
#[doc = $DOC_NAME!()]
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
$struct_vis struct $struct_name
$(( $($e_vis $e_ty),+ ) )? $(; $($_a)?)? // tuple-struct↓
$({ $( $(#[$f_attr])* $f_vis $f_name: $f_ty),+ })? // field-struct↑

$(#[$attributes])*
impl $crate::Display for $struct_name {
fn fmt(&$self, $fmt: &mut $crate::Formatter<'_>) -> $crate::FmtResult<()> {
$display_expr
}
}
$(#[$attributes])*
impl $crate::Error for $struct_name {}
$(#[$attributes])*
impl $crate::ExtError for $struct_name {
type Kind = ();
fn error_eq(&self, other: &Self) -> bool { self == other }
Expand All @@ -49,7 +56,8 @@ macro_rules! impl_error {
composite: fmt($fmt:ident)
$(#[$enum_attr:meta])*
$vis:vis enum $composite_error_name:ident { $(
$DOC_VAR:ident:
$(#[$variant_attr:meta])*
$DOC_VARIANT:ident:
$variant_name:ident
$(( $($e_name:ident| $e_numb:literal: $e_ty:ty),+ ))? // tuple-struct↓
$({ $($(#[$f_attr:meta])* $f_name:ident: $f_ty:ty),+ })? // field-struct↑
Expand All @@ -62,7 +70,8 @@ macro_rules! impl_error {
$(#[$enum_attr])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
$vis enum $composite_error_name { $(
#[doc = $DOC_VAR!()]
$(#[$variant_attr])*
#[doc = $DOC_VARIANT!()]
$variant_name
$(( $($e_ty),+ ))? // tuple-struct↓
$({ $($(#[$f_attr])* $f_name: $f_ty),+ })? // field-struct↑
Expand All @@ -78,9 +87,10 @@ macro_rules! impl_error {
impl $crate::Display for $composite_error_name {
fn fmt(&self, $fmt: &mut $crate::Formatter<'_>) -> $crate::FmtResult<()> {
match self { $(
$(#[$variant_attr])*
$composite_error_name::$variant_name
$(( $($e_name),+ ))? // tuple-struct|
$({ $($f_name),+ })? // field-struct
$(( $($e_name),+ ))? // tuple-struct
$({ $($f_name),+ })? // field-struct
=>
$crate::Display::fmt(&$individual_error_name
$(( $($e_display_expr),+ ))? // tuple-struct↓
Expand All @@ -92,6 +102,7 @@ macro_rules! impl_error {
// implements From multiple individual errors for a composite error,
// and implements TryFrom in reverse:
$(
$(#[$variant_attr])*
$crate::impl_error! { from(_f): $individual_error_name, for: $composite_error_name
=> $variant_name
$(( $($e_name, $crate::field_of![_f, $e_numb] ),+ ))? // tuple-struct↓
Expand Down

0 comments on commit be8e9e2

Please sign in to comment.