diff options
Diffstat (limited to 'macros/src/util')
-rw-r--r-- | macros/src/util/error.rs | 48 | ||||
-rw-r--r-- | macros/src/util/mod.rs | 29 |
2 files changed, 28 insertions, 49 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; diff --git a/macros/src/util/mod.rs b/macros/src/util/mod.rs deleted file mode 100644 index 7ab2185..0000000 --- a/macros/src/util/mod.rs +++ /dev/null @@ -1,29 +0,0 @@ -pub mod error; -pub mod item_impl; -pub mod iterator_ext; -pub mod string; -pub mod syn_ext; -pub mod syn_path; -pub mod tokens; - -macro_rules! to_option { - ($($tokens: tt)+) => { - Some($($tokens)+) - }; - - () => { - None - }; -} - -macro_rules! or { - (($($tokens: tt)+) else ($($default: tt)*)) => { - $($tokens)* - }; - - (() else ($($default: tt)*)) => { - $($default)* - }; -} - -pub(crate) use {or, to_option}; |