Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5
//! Various macro helpers.
6
7
macro_rules! exclusive_value {
8
(($value:ident, $set:expr) => $ident:path) => {
9
if $value.intersects($set) {
10
return Err(());
11
} else {
12
$ident
13
}
14
};
15
}
16
17
#[cfg(feature = "gecko")]
18
macro_rules! impl_gecko_keyword_conversions {
19
($name:ident, $utype:ty) => {
20
impl From<$utype> for $name {
21
fn from(bits: $utype) -> $name {
22
$name::from_gecko_keyword(bits)
23
}
24
}
25
26
impl From<$name> for $utype {
27
fn from(v: $name) -> $utype {
28
v.to_gecko_keyword()
29
}
30
}
31
};
32
}
33
34
macro_rules! trivial_to_computed_value {
35
($name:ty) => {
36
impl $crate::values::computed::ToComputedValue for $name {
37
type ComputedValue = $name;
38
39
fn to_computed_value(&self, _: &$crate::values::computed::Context) -> Self {
40
self.clone()
41
}
42
43
fn from_computed_value(other: &Self) -> Self {
44
other.clone()
45
}
46
}
47
};
48
}
49
50
/// A macro to parse an identifier, or return an `UnexpectedIdent` error
51
/// otherwise.
52
///
53
/// FIXME(emilio): The fact that `UnexpectedIdent` is a `SelectorParseError`
54
/// doesn't make a lot of sense to me.
55
macro_rules! try_match_ident_ignore_ascii_case {
56
($input:expr, $( $match_body:tt )*) => {{
57
let location = $input.current_source_location();
58
let ident = $input.expect_ident_cloned()?;
59
match_ignore_ascii_case! { &ident,
60
$( $match_body )*
61
_ => return Err(location.new_custom_error(
62
::selectors::parser::SelectorParseErrorKind::UnexpectedIdent(ident.clone())
63
))
64
}
65
}}
66
}
67
68
macro_rules! define_keyword_type {
69
($name:ident, $css:expr) => {
70
#[allow(missing_docs)]
71
#[derive(
72
Animate,
73
Clone,
74
ComputeSquaredDistance,
75
Copy,
76
MallocSizeOf,
77
PartialEq,
78
SpecifiedValueInfo,
79
ToAnimatedValue,
80
ToAnimatedZero,
81
ToComputedValue,
82
ToCss,
83
ToResolvedValue,
84
ToShmem,
85
)]
86
pub struct $name;
87
88
impl fmt::Debug for $name {
89
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90
f.write_str($css)
91
}
92
}
93
94
impl $crate::parser::Parse for $name {
95
fn parse<'i, 't>(
96
_context: &$crate::parser::ParserContext,
97
input: &mut ::cssparser::Parser<'i, 't>,
98
) -> Result<$name, ::style_traits::ParseError<'i>> {
99
input
100
.expect_ident_matching($css)
101
.map(|_| $name)
102
.map_err(|e| e.into())
103
}
104
}
105
};
106
}
107
108
/// Place a Gecko profiler label on the stack.
109
///
110
/// The `label_type` argument must be the name of a variant of `ProfilerLabel`.
111
#[cfg(feature = "gecko_profiler")]
112
#[macro_export]
113
macro_rules! profiler_label {
114
($label_type:ident) => {
115
let mut _profiler_label =
116
::std::mem::MaybeUninit::<$crate::gecko_bindings::structs::AutoProfilerLabel>::uninit();
117
let _profiler_label = if $crate::gecko::profiler::profiler_is_active() {
118
unsafe {
119
Some($crate::gecko::profiler::AutoProfilerLabel::new(
120
&mut _profiler_label,
121
$crate::gecko::profiler::ProfilerLabel::$label_type,
122
))
123
}
124
} else {
125
None
126
};
127
};
128
}
129
130
/// No-op when the Gecko profiler is not available.
131
#[cfg(not(feature = "gecko_profiler"))]
132
#[macro_export]
133
macro_rules! profiler_label {
134
($label_type:ident) => {};
135
}