From 65c7f14b201b52c16149113961da88a7febff2f5 Mon Sep 17 00:00:00 2001 From: Igor Date: Thu, 14 Nov 2024 17:48:48 +0400 Subject: [PATCH 1/2] fix(dispatch): handle `cfg` attributes --- src/macros/dispatch.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/macros/dispatch.rs b/src/macros/dispatch.rs index b763d4ba..f1191246 100644 --- a/src/macros/dispatch.rs +++ b/src/macros/dispatch.rs @@ -40,16 +40,37 @@ #[macro_export] #[doc(hidden)] // forced to be visible in intended location macro_rules! dispatch { - ($match_parser: expr; $( $pat:pat $(if $pred:expr)? => $expr: expr ),+ $(,)? ) => { + ($match_parser: expr; $($(#[$meta:meta])* $pat:pat $(if $pred:expr)? => $expr: expr ),+ $(,)? ) => { $crate::combinator::trace("dispatch", move |i: &mut _| { use $crate::Parser; let initial = $match_parser.parse_next(i)?; match initial { $( + $(#[$meta])* $pat $(if $pred)? => $expr.parse_next(i), )* } }) } } + +#[cfg(test)] +mod tests { + use crate::token::any; + use crate::Parser; + use crate::{combinator::fail, error::ContextError}; + + #[test] + fn handle_cfg_attributes() { + assert!(matches!( + dispatch! {any::<_, ContextError>; + #[cfg(any())] // always false + 'a' => crate::combinator::empty, + _ => fail::<_, (), _>, + } + .parse("a"), + Err(_) + )); + } +} From c995969fcb8fc7a18227c9cb973a3899c60598b0 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 19 Nov 2024 13:10:53 +0400 Subject: [PATCH 2/2] fix: clippy --- src/macros/dispatch.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/macros/dispatch.rs b/src/macros/dispatch.rs index f1191246..c71ed1dc 100644 --- a/src/macros/dispatch.rs +++ b/src/macros/dispatch.rs @@ -63,14 +63,12 @@ mod tests { #[test] fn handle_cfg_attributes() { - assert!(matches!( - dispatch! {any::<_, ContextError>; - #[cfg(any())] // always false - 'a' => crate::combinator::empty, - _ => fail::<_, (), _>, - } - .parse("a"), - Err(_) - )); + assert!(dispatch! {any::<_, ContextError>; + #[cfg(any())] // always false + 'a' => crate::combinator::empty, + _ => fail::<_, (), _>, + } + .parse("a") + .is_err()); } }