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
#[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());
®EX
}};
($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)
};
}
#[macro_export]
macro_rules! matches {
($expression:expr => $pattern:pat) => {
match $expression {
$pattern => true,
_ => false,
}
};
}
#[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() {
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() {
let selects: Vec<_> = (0..2).map(|_| select!("div a")).collect();
assert_eq!(selects[0] as *const _, selects[1] as *const _);
}
}