Skip to content

Commit d2cae6c

Browse files
committed
Random macro refactoring
1 parent 0ead1b5 commit d2cae6c

File tree

3 files changed

+51
-53
lines changed

3 files changed

+51
-53
lines changed

macros/lib.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn assert_ascii_lowercase(input: proc_macro::TokenStream) -> proc_macro::Tok
2323
"".parse().unwrap()
2424
}
2525

26-
/// and emit a `MAX_LENGTH` constant with the length of the longest string.
26+
/// Emit a `MAX_LENGTH` constant with the length of the longest string.
2727
#[proc_macro_derive(cssparser__max_len)]
2828
pub fn max_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
2929
let input = syn::parse_macro_input(&input.to_string()).unwrap();
@@ -40,16 +40,13 @@ pub fn max_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
4040
}
4141

4242
/// ```
43-
/// impl $Name {
44-
/// fn map() -> &'static ::phf::Map<&'static str, $ValueType> { … }
45-
/// }
43+
/// static MAP: &'static ::phf::Map<&'static str, $ValueType> = …;
4644
/// ```
4745
///
4846
/// Map keys are ASCII-lowercased.
4947
#[proc_macro_derive(cssparser__phf_map)]
5048
pub fn phf_map(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
5149
let input = syn::parse_macro_input(&input.to_string()).unwrap();
52-
let name = &input.ident;
5350

5451
let token_trees = find_smuggled_tokens(&input);
5552
let value_type = &token_trees[0];
@@ -64,28 +61,21 @@ pub fn phf_map(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
6461
map.entry(&**key, &**value);
6562
}
6663

67-
let mut initializer_bytes = Vec::<u8>::new();
68-
let mut initializer_tokens = quote::Tokens::new();
69-
map.build(&mut initializer_bytes).unwrap();
70-
initializer_tokens.append(::std::str::from_utf8(&initializer_bytes).unwrap());
71-
72-
let tokens = quote! {
73-
impl #name {
74-
#[inline]
75-
fn map() -> &'static ::phf::Map<&'static str, #value_type> {
76-
static MAP: ::phf::Map<&'static str, #value_type> = #initializer_tokens;
77-
&MAP
78-
}
79-
}
64+
let mut tokens = quote! {
65+
static MAP: ::phf::Map<&'static str, #value_type> =
8066
};
67+
let mut initializer_bytes = Vec::new();
68+
map.build(&mut initializer_bytes).unwrap();
69+
tokens.append(::std::str::from_utf8(&initializer_bytes).unwrap());
70+
tokens.append(";");
8171

8272
tokens.as_str().parse().unwrap()
8373
}
8474

8575
/// Return the `…` part in:
8676
///
8777
/// ```rust
88-
/// enum $Name {
78+
/// enum Somthing {
8979
/// Input = (0, stringify!(…)).0
9080
/// }
9181
/// ```

src/color.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub fn parse_color_keyword(ident: &str) -> Result<Color, ()> {
180180
}
181181
}
182182
ascii_case_insensitive_phf_map! {
183-
KEYWORDS: Map<Color> = {
183+
keyword -> Color = {
184184
"black" => rgb!(0, 0, 0),
185185
"silver" => rgb!(192, 192, 192),
186186
"gray" => rgb!(128, 128, 128),
@@ -335,7 +335,7 @@ pub fn parse_color_keyword(ident: &str) -> Result<Color, ()> {
335335
"currentcolor" => Color::CurrentColor,
336336
}
337337
}
338-
KEYWORDS::get(ident).cloned().ok_or(())
338+
keyword(ident).cloned().ok_or(())
339339
}
340340

341341

src/lib.rs

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,18 @@ macro_rules! match_ignore_ascii_case {
134134
// finished parsing
135135
(@inner $value:expr, () -> ($(($string:expr => $result:expr))*) $fallback:expr ) => {
136136
{
137-
#[derive(cssparser__assert_ascii_lowercase)]
138-
#[allow(unused)]
139-
enum Dummy {
140-
Input = (0, stringify!( $( $string )+ )).0
137+
_cssparser_internal__pretend_proc_macro! {
138+
cssparser__assert_ascii_lowercase!( $( $string )+ )
141139
}
142140

143-
_cssparser_internal__to_lowercase!($value => lowercase, $($string),+);
144-
match lowercase {
145-
$(
146-
Some($string) => $result,
147-
)+
148-
_ => $fallback
141+
{
142+
_cssparser_internal__to_lowercase!($value => lowercase, $($string),+);
143+
match lowercase {
144+
$(
145+
Some($string) => $result,
146+
)+
147+
_ => $fallback
148+
}
149149
}
150150
}
151151
};
@@ -156,11 +156,11 @@ macro_rules! match_ignore_ascii_case {
156156
};
157157
}
158158

159-
/// Define a placeholder type `$Name`
160-
/// with a method `fn get(input: &str) -> Option<&'static $ValueType>`.
159+
/// Define a function `$name(&str) -> Option<&'static $ValueType>`
161160
///
162-
/// This method uses finds a match for the input string
163-
/// in a [`phf` map](https://github.com/sfackler/rust-phf).
161+
/// The function finds a match for the input string
162+
/// in a [`phf` map](https://github.com/sfackler/rust-phf)
163+
/// and returns a reference to the corresponding value.
164164
/// Matching is case-insensitive in the ASCII range.
165165
///
166166
/// The `phf` and `cssparser_macros` crates must also be imported at the crate root
@@ -176,30 +176,25 @@ macro_rules! match_ignore_ascii_case {
176176
///
177177
/// fn color_rgb(input: &str) -> Option<(u8, u8, u8)> {
178178
/// ascii_case_insensitive_phf_map! {
179-
/// KEYWORDS: Map<(u8, u8, u8)> = {
179+
/// keyword -> (u8, u8, u8) = {
180180
/// "red" => (255, 0, 0),
181181
/// "green" => (0, 255, 0),
182182
/// "blue" => (0, 0, 255),
183183
/// }
184184
/// }
185-
/// KEYWORDS::get(input).cloned()
185+
/// keyword(input).cloned()
186186
/// }
187187
#[macro_export]
188188
macro_rules! ascii_case_insensitive_phf_map {
189-
($Name: ident : Map<$ValueType: ty> = {
190-
$( $key: expr => $value: expr, )*
191-
}) => {
192-
#[derive(cssparser__phf_map)]
193-
#[allow(unused)]
194-
enum $Name {
195-
Input = (0, stringify!( ($ValueType) $( $key ($value) )+ )).0
196-
}
189+
($name: ident -> $ValueType: ty = { $( $key: expr => $value: expr, )* }) => {
190+
fn $name(input: &str) -> Option<&'static $ValueType> {
191+
_cssparser_internal__pretend_proc_macro! {
192+
cssparser__phf_map!( ($ValueType) $( $key ($value) )+ )
193+
}
197194

198-
impl $Name {
199-
#[inline]
200-
fn get(input: &str) -> Option<&'static $ValueType> {
195+
{
201196
_cssparser_internal__to_lowercase!(input => lowercase, $($key),+);
202-
lowercase.and_then(|string| $Name::map().get(string))
197+
lowercase.and_then(|s| MAP.get(s))
203198
}
204199
}
205200
}
@@ -216,10 +211,8 @@ macro_rules! ascii_case_insensitive_phf_map {
216211
#[doc(hidden)]
217212
macro_rules! _cssparser_internal__to_lowercase {
218213
($input: expr => $output: ident, $($string: expr),+) => {
219-
#[derive(cssparser__max_len)]
220-
#[allow(unused)]
221-
enum Dummy2 {
222-
Input = (0, stringify!( $( $string )+ )).0
214+
_cssparser_internal__pretend_proc_macro! {
215+
cssparser__max_len!( $( $string )+ )
223216
}
224217

225218
// mem::uninitialized() is ok because `buffer` is only used in `_internal__to_lowercase`,
@@ -235,6 +228,21 @@ macro_rules! _cssparser_internal__to_lowercase {
235228
}
236229
}
237230

231+
/// Implementation detail of match_ignore_ascii_case! and ascii_case_insensitive_phf_map! macros.
232+
///
233+
/// **This macro is not part of the public API. It can change or be removed between any versions.**
234+
#[macro_export]
235+
#[doc(hidden)]
236+
macro_rules! _cssparser_internal__pretend_proc_macro {
237+
($macro_name: ident ! ( $($tt: tt)* )) => {
238+
#[derive($macro_name)]
239+
#[allow(unused)]
240+
enum Dummy {
241+
Input = (0, stringify!( $( $tt )* )).0
242+
}
243+
}
244+
}
245+
238246
/// Implementation detail of match_ignore_ascii_case! and ascii_case_insensitive_phf_map! macros.
239247
///
240248
/// **This function is not part of the public API. It can change or be removed between any verisons.**

0 commit comments

Comments
 (0)