From 9240ab9fc16e16ef711ee0d8ca95ce729ad8bc3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 10 Mar 2017 00:15:26 +0100 Subject: [PATCH] macros: Make bindings work as expected. --- procedural-masquerade/lib.rs | 2 +- src/macros.rs | 14 ++++++++++---- src/tests.rs | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/procedural-masquerade/lib.rs b/procedural-masquerade/lib.rs index c2bd6b48..5a9a3ca0 100644 --- a/procedural-masquerade/lib.rs +++ b/procedural-masquerade/lib.rs @@ -137,7 +137,7 @@ //! ## For users //! //! Users of `libfoo` don’t need to worry about any of these implementation details. -//! They can use the `foo_stringify` macro as if it were a simle `macro_rules` macro: +//! They can use the `foo_stringify` macro as if it were a simple `macro_rules` macro: //! //! ```rust //! #[macro_use] extern crate libfoo; diff --git a/src/macros.rs b/src/macros.rs index 6b75d46b..fc2d1182 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -40,11 +40,17 @@ macro_rules! match_ignore_ascii_case { } { + let __input = $input; + // MAX_LENGTH is generated by cssparser_internal__assert_ascii_lowercase__max_len - cssparser_internal__to_lowercase!($input, MAX_LENGTH => lowercase); - // "A" is a short string that we know is different for every string pattern, - // since we’ve verified that none of them include ASCII upper case letters. - match lowercase.unwrap_or("A") { + cssparser_internal__to_lowercase!(__input, MAX_LENGTH => lowercase); + + // We know __input will never match if it reaches the + // `unwrap_or`, but this is needed to make pattern bindings work + // as expected, see: + // + // https://github.com/servo/rust-cssparser/issues/126 + match lowercase.unwrap_or(__input) { $( $match_body )* } } diff --git a/src/tests.rs b/src/tests.rs index 84199438..6a142d26 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -843,3 +843,21 @@ fn procedural_masquerade_whitespace() { _ => panic!("4"), } } + +#[test] +fn known_not_matching() { + match_ignore_ascii_case! { "fo", + "bar" => {}, + value => assert_eq!(value, "fo"), + } + + match_ignore_ascii_case! { "foo", + "bar" => {}, + value => assert_eq!(value, "foo"), + } + + match_ignore_ascii_case! { "fooo", + "bar" => {}, + value => assert_eq!(value, "fooo"), + } +}