mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Auto merge of #19665 - emilio:derive-parse-alias, r=Manishearth
style_derive: Support parse-time aliases. This will allow #19659 to use derive on display using: #[parse(aliases = "-webkit-flex")] Flex, #[parse(aliases = "-webkit-inline-flex")] InlineFlex, And such. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19665) <!-- Reviewable:end -->
This commit is contained in:
commit
4ba795081c
2 changed files with 23 additions and 1 deletions
|
@ -39,7 +39,7 @@ pub fn derive_to_animated_value(stream: TokenStream) -> TokenStream {
|
|||
to_animated_value::derive(input).to_string().parse().unwrap()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(Parse)]
|
||||
#[proc_macro_derive(Parse, attributes(parse))]
|
||||
pub fn derive_parse(stream: TokenStream) -> TokenStream {
|
||||
let input = syn::parse_derive_input(&stream.to_string()).unwrap();
|
||||
parse::derive(input).to_string().parse().unwrap()
|
||||
|
|
|
@ -19,11 +19,25 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
|||
"Parse is only supported for single-variant enums for now"
|
||||
);
|
||||
|
||||
let variant_attrs = cg::parse_variant_attrs::<ParseVariantAttrs>(variant);
|
||||
let identifier = cg::to_css_identifier(variant.ident.as_ref());
|
||||
let ident = &variant.ident;
|
||||
|
||||
match_body = quote! {
|
||||
#match_body
|
||||
#identifier => Ok(#name::#ident),
|
||||
};
|
||||
|
||||
let aliases = match variant_attrs.aliases {
|
||||
Some(aliases) => aliases,
|
||||
None => return,
|
||||
};
|
||||
|
||||
for alias in aliases.split(",") {
|
||||
match_body = quote! {
|
||||
#match_body
|
||||
#alias => Ok(#name::#ident),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -73,3 +87,11 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
|||
#methods_impl
|
||||
}
|
||||
}
|
||||
|
||||
#[darling(attributes(parse), default)]
|
||||
#[derive(Default, FromVariant)]
|
||||
struct ParseVariantAttrs {
|
||||
/// The comma-separated list of aliases this variant should be aliased to at
|
||||
/// parse time.
|
||||
aliases: Option<String>,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue