aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-12-27 19:29:11 +0100
committerHampusM <hampus@hampusmat.com>2023-12-27 19:29:11 +0100
commitc6995dc5b74b69413ea5aeea5dab0620c7d50ab4 (patch)
tree668c5fb70ae178250075f0f03943d61862aaac23
parentc501a5cc770f632eba1529de09bd3ae2d7958de6 (diff)
refactor: improve readability of the diagnostic_error_enum macro
-rw-r--r--macros/src/util/error.rs48
1 files changed, 28 insertions, 20 deletions
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<Diagnostic>,
+}
+
pub(crate) use diagnostic_error_enum;