From c6995dc5b74b69413ea5aeea5dab0620c7d50ab4 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 27 Dec 2023 19:29:11 +0100 Subject: refactor: improve readability of the diagnostic_error_enum macro --- macros/src/util/error.rs | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) (limited to 'macros/src') diff --git a/macros/src/util/error.rs b/macros/src/util/error.rs index d068661..b9f67c4 100644 --- a/macros/src/util/error.rs +++ b/macros/src/util/error.rs @@ -1,3 +1,6 @@ +use proc_macro2::Span; +use proc_macro_error::Diagnostic; + /// Used to create a error enum that converts into a [`Diagnostic`]. /// /// [`Diagnostic`]: proc_macro_error::Diagnostic @@ -30,22 +33,17 @@ macro_rules! diagnostic_error_enum { #[must_use] fn from(err: $name) -> Self { - let (error, span, notes, helps, errs, source): ( - String, - ::proc_macro2::Span, - Vec<(String, ::proc_macro2::Span)>, - Vec<(String, ::proc_macro2::Span)>, - Vec<(String, ::proc_macro2::Span)>, - Option<::proc_macro_error::Diagnostic> - ) = match err { + use $crate::util::error::DiagnosticErrorVariantInfo; + + let DiagnosticErrorVariantInfo { + error, span, notes, helps, errs, source + } = match err { $( - $name::$variant { - $($variant_field),* - } => { - ( - format!($($error)*), - $error_span, - vec![$( + $name::$variant { $($variant_field),* } => { + DiagnosticErrorVariantInfo { + error: format!($($error)*), + span: $error_span, + notes: vec![$( ( format!($($note)*), $crate::util::or!( @@ -54,7 +52,7 @@ macro_rules! diagnostic_error_enum { ) ) ),*], - vec![$( + helps: vec![$( ( format!($($help)*), $crate::util::or!( @@ -63,7 +61,7 @@ macro_rules! diagnostic_error_enum { ) ) ),*], - vec![$( + errs: vec![$( ( format!($($err)*), $crate::util::or!( @@ -72,8 +70,8 @@ macro_rules! diagnostic_error_enum { ) ) ),*], - $crate::util::to_option!($($source.into())?) - ) + source: $crate::util::to_option!($($source.into())?) + } } ),* }; @@ -109,8 +107,18 @@ macro_rules! diagnostic_error_enum { diagnostic } } - }; } +/// Used by [`diagnostic_error_enum`]. +pub struct DiagnosticErrorVariantInfo +{ + pub error: String, + pub span: Span, + pub notes: Vec<(String, Span)>, + pub helps: Vec<(String, Span)>, + pub errs: Vec<(String, Span)>, + pub source: Option, +} + pub(crate) use diagnostic_error_enum; -- cgit v1.2.3-18-g5258