1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/// Create and cache Regex object
#[macro_export]
macro_rules! regex {
    ($expr:expr) => {{
        static REGEX: ::once_cell::sync::Lazy<::regex::Regex> =
            ::once_cell::sync::Lazy::new(|| ::regex::Regex::new($expr).unwrap());
        &REGEX
    }};
    ($expr:expr,) => {
        regex!($expr)
    };
}

#[macro_export]
macro_rules! select {
    ($selectors:literal) => {{
        static SELECTOR: ::once_cell::sync::Lazy<::scraper::selector::Selector> =
            ::once_cell::sync::Lazy::new(|| {
                ::scraper::selector::Selector::parse($selectors).unwrap()
            });
        &SELECTOR
    }};
    ($selectors:literal,) => {
        selector!($selectors)
    };
}

/// Check if an expression matches a refutable pattern.
///
/// Syntax: `matches!(` *expression* ` => ` *pattern* `)`
///
/// Return a boolean, true if the expression matches the pattern, false otherwise.
///
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate acick_util;
///
/// pub enum Foo<T> {
///     A,
///     B(T),
/// }
///
/// impl<T> Foo<T> {
///     pub fn is_a(&self) -> bool {
///         matches!(*self => Foo::A)
///     }
///
///     pub fn is_b(&self) -> bool {
///         matches!(*self => Foo::B(_))
///     }
/// }
///
/// # fn main() { }
/// ```
#[macro_export]
macro_rules! matches {
    ($expression:expr => $pattern:pat) => {
        match $expression {
            $pattern => true,
            _ => false,
        }
    };
}

/// Assert that an expression matches a refutable pattern.
///
/// Syntax: `assert_matches!(` *expression* ` => ` *pattern* `)`
///
/// Panic with a message that shows the expression if it does not match the
/// pattern.
///
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate acick_util;
///
/// fn main() {
///     let data = [1, 2, 3];
///     assert_matches!(data.get(1) => Some(_));
/// }
/// ```
#[macro_export]
macro_rules! assert_matches {
    ($expression:expr => $pattern:pat) => {
        match $expression {
            $pattern => (),
            ref e => panic!(
                "assertion failed: `{:?}` does not match `{}`",
                e,
                stringify!($pattern)
            ),
        }
    };
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_regex() {
        // check multiple regex! calls creates only one instance and caches it
        let regs: Vec<_> = (0..2).map(|_| regex!(r"\A(hello)+\z")).collect();
        assert_eq!(regs[0] as *const _, regs[1] as *const _);
    }

    #[test]
    fn test_select() {
        // check multiple select! calls creates only one instance and caches it
        let selects: Vec<_> = (0..2).map(|_| select!("div a")).collect();
        assert_eq!(selects[0] as *const _, selects[1] as *const _);
    }
}