mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
style: Add a ValueInfo trait for exposing types needed by devtools.
Most of types just derive it using proc_macro directly. Some of value types need manual impl. In my current plan, this new trait will be used in bug 1434130 to expose values as well. Bug: 1455576 Reviewed-by: emilio MozReview-Commit-ID: LI7fy45VkRw
This commit is contained in:
parent
276fb7e04b
commit
7fe7b2ffb1
66 changed files with 615 additions and 249 deletions
|
@ -68,7 +68,7 @@ pub fn derive(mut input: DeriveInput) -> Tokens {
|
|||
}
|
||||
|
||||
fn derive_variant_arm(variant: &VariantInfo) -> Result<Tokens, ()> {
|
||||
let variant_attrs = cg::parse_variant_attrs::<AnimationVariantAttrs>(&variant.ast());
|
||||
let variant_attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
|
||||
if variant_attrs.error {
|
||||
return Err(());
|
||||
}
|
||||
|
|
|
@ -188,7 +188,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse_variant_attrs<A>(variant: &VariantAst) -> A
|
||||
pub fn parse_variant_attrs_from_ast<A>(variant: &VariantAst) -> A
|
||||
where
|
||||
A: FromVariant,
|
||||
{
|
||||
|
@ -198,7 +198,14 @@ where
|
|||
fields: variant.fields.clone(),
|
||||
discriminant: variant.discriminant.clone(),
|
||||
};
|
||||
match A::from_variant(&v) {
|
||||
parse_variant_attrs(&v)
|
||||
}
|
||||
|
||||
pub fn parse_variant_attrs<A>(variant: &Variant) -> A
|
||||
where
|
||||
A: FromVariant
|
||||
{
|
||||
match A::from_variant(variant) {
|
||||
Ok(attrs) => attrs,
|
||||
Err(e) => panic!("failed to parse variant attributes: {}", e),
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ pub fn derive(mut input: DeriveInput) -> Tokens {
|
|||
let mut append_error_clause = s.variants().len() > 1;
|
||||
|
||||
let match_body = s.variants().iter().fold(quote!(), |body, variant| {
|
||||
let attrs = cg::parse_variant_attrs::<AnimationVariantAttrs>(&variant.ast());
|
||||
let attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
|
||||
if attrs.error {
|
||||
append_error_clause = true;
|
||||
return body;
|
||||
|
|
|
@ -16,6 +16,7 @@ mod animate;
|
|||
mod cg;
|
||||
mod compute_squared_distance;
|
||||
mod parse;
|
||||
mod specified_value_info;
|
||||
mod to_animated_value;
|
||||
mod to_animated_zero;
|
||||
mod to_computed_value;
|
||||
|
@ -62,3 +63,9 @@ pub fn derive_to_css(stream: TokenStream) -> TokenStream {
|
|||
let input = syn::parse(stream).unwrap();
|
||||
to_css::derive(input).into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(SpecifiedValueInfo, attributes(css))]
|
||||
pub fn derive_specified_value_info(stream: TokenStream) -> TokenStream {
|
||||
let input = syn::parse(stream).unwrap();
|
||||
specified_value_info::derive(input).into()
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
|||
"Parse is only supported for single-variant enums for now"
|
||||
);
|
||||
|
||||
let variant_attrs = cg::parse_variant_attrs::<CssVariantAttrs>(&variant.ast());
|
||||
let variant_attrs = cg::parse_variant_attrs_from_ast::<CssVariantAttrs>(&variant.ast());
|
||||
let identifier = cg::to_css_identifier(
|
||||
&variant_attrs.keyword.unwrap_or(variant.ast().ident.as_ref().into()),
|
||||
);
|
||||
|
|
68
components/style_derive/specified_value_info.rs
Normal file
68
components/style_derive/specified_value_info.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use cg;
|
||||
use quote::Tokens;
|
||||
use syn::{Data, DeriveInput, Fields};
|
||||
use to_css::{CssFieldAttrs, CssInputAttrs, CssVariantAttrs};
|
||||
|
||||
pub fn derive(mut input: DeriveInput) -> Tokens {
|
||||
let attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
|
||||
let mut types_value = quote!(0);
|
||||
// If the whole value is wrapped in a function, value types of its
|
||||
// fields should not be propagated.
|
||||
if attrs.function.is_none() {
|
||||
let mut where_clause = input.generics.where_clause.take();
|
||||
for param in input.generics.type_params() {
|
||||
cg::add_predicate(
|
||||
&mut where_clause,
|
||||
parse_quote!(#param: ::style_traits::SpecifiedValueInfo),
|
||||
);
|
||||
}
|
||||
input.generics.where_clause = where_clause;
|
||||
|
||||
match input.data {
|
||||
Data::Enum(ref e) => {
|
||||
for v in e.variants.iter() {
|
||||
let attrs = cg::parse_variant_attrs::<CssVariantAttrs>(&v);
|
||||
if attrs.function.is_none() {
|
||||
derive_struct_fields(&v.fields, &mut types_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Data::Struct(ref s) => {
|
||||
derive_struct_fields(&s.fields, &mut types_value)
|
||||
}
|
||||
Data::Union(_) => unreachable!("union is not supported"),
|
||||
}
|
||||
}
|
||||
|
||||
let name = &input.ident;
|
||||
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
||||
quote! {
|
||||
impl #impl_generics ::style_traits::SpecifiedValueInfo for #name #ty_generics
|
||||
#where_clause
|
||||
{
|
||||
const SUPPORTED_TYPES: u8 = #types_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn derive_struct_fields(fields: &Fields, supports_body: &mut Tokens) {
|
||||
let fields = match *fields {
|
||||
Fields::Unit => return,
|
||||
Fields::Named(ref fields) => fields.named.iter(),
|
||||
Fields::Unnamed(ref fields) => fields.unnamed.iter(),
|
||||
};
|
||||
supports_body.append_all(fields.map(|field| {
|
||||
let attrs = cg::parse_field_attrs::<CssFieldAttrs>(field);
|
||||
if attrs.skip {
|
||||
return quote!();
|
||||
}
|
||||
let ty = &field.ty;
|
||||
quote! {
|
||||
| <#ty as ::style_traits::SpecifiedValueInfo>::SUPPORTED_TYPES
|
||||
}
|
||||
}));
|
||||
}
|
|
@ -22,7 +22,7 @@ pub fn derive(mut input: syn::DeriveInput) -> quote::Tokens {
|
|||
}
|
||||
|
||||
let to_body = synstructure::Structure::new(&input).each_variant(|variant| {
|
||||
let attrs = cg::parse_variant_attrs::<AnimationVariantAttrs>(&variant.ast());
|
||||
let attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
|
||||
if attrs.error {
|
||||
return Some(quote! { Err(()) });
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ fn derive_variant_arm(
|
|||
let bindings = variant.bindings();
|
||||
let identifier = cg::to_css_identifier(variant.ast().ident.as_ref());
|
||||
let ast = variant.ast();
|
||||
let variant_attrs = cg::parse_variant_attrs::<CssVariantAttrs>(&ast);
|
||||
let variant_attrs = cg::parse_variant_attrs_from_ast::<CssVariantAttrs>(&ast);
|
||||
let separator = if variant_attrs.comma { ", " } else { " " };
|
||||
|
||||
if variant_attrs.dimension {
|
||||
|
@ -207,12 +207,12 @@ fn derive_single_field_expr(
|
|||
|
||||
#[darling(attributes(css), default)]
|
||||
#[derive(Default, FromDeriveInput)]
|
||||
struct CssInputAttrs {
|
||||
derive_debug: bool,
|
||||
pub struct CssInputAttrs {
|
||||
pub derive_debug: bool,
|
||||
// Here because structs variants are also their whole type definition.
|
||||
function: Option<Override<String>>,
|
||||
pub function: Option<Override<String>>,
|
||||
// Here because structs variants are also their whole type definition.
|
||||
comma: bool,
|
||||
pub comma: bool,
|
||||
}
|
||||
|
||||
#[darling(attributes(css), default)]
|
||||
|
@ -227,10 +227,10 @@ pub struct CssVariantAttrs {
|
|||
|
||||
#[darling(attributes(css), default)]
|
||||
#[derive(Default, FromField)]
|
||||
struct CssFieldAttrs {
|
||||
if_empty: Option<String>,
|
||||
field_bound: bool,
|
||||
iterable: bool,
|
||||
skip: bool,
|
||||
skip_if: Option<Path>,
|
||||
pub struct CssFieldAttrs {
|
||||
pub if_empty: Option<String>,
|
||||
pub field_bound: bool,
|
||||
pub iterable: bool,
|
||||
pub skip: bool,
|
||||
pub skip_if: Option<Path>,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue