mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Derive HasViewportPercentage 🍷
This commit is contained in:
parent
d1e31f7aa4
commit
90bae7f802
27 changed files with 202 additions and 452 deletions
15
components/style_derive/Cargo.toml
Normal file
15
components/style_derive/Cargo.toml
Normal file
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
name = "style_derive"
|
||||
version = "0.0.1"
|
||||
authors = ["The Servo Project Developers"]
|
||||
license = "MPL-2.0"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
quote = "0.3"
|
||||
syn = "0.11"
|
||||
synstructure = "0.5.2"
|
56
components/style_derive/has_viewport_percentage.rs
Normal file
56
components/style_derive/has_viewport_percentage.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
/* 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 quote;
|
||||
use syn;
|
||||
use synstructure;
|
||||
|
||||
pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
|
||||
let name = &input.ident;
|
||||
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
||||
let mut where_clause = where_clause.clone();
|
||||
for param in &input.generics.ty_params {
|
||||
where_clause.predicates.push(where_predicate(syn::Ty::Path(None, param.ident.clone().into())))
|
||||
}
|
||||
|
||||
let style = synstructure::BindStyle::Ref.into();
|
||||
let match_body = synstructure::each_variant(&input, &style, |bindings, _| {
|
||||
let (first, rest) = match bindings.split_first() {
|
||||
None => return Some(quote!(false)),
|
||||
Some(pair) => pair,
|
||||
};
|
||||
let mut expr = quote!(::style_traits::HasViewportPercentage::has_viewport_percentage(#first));
|
||||
for binding in rest {
|
||||
where_clause.predicates.push(where_predicate(binding.field.ty.clone()));
|
||||
expr = quote!(#expr || ::style_traits::HasViewportPercentage::has_viewport_percentage(#binding));
|
||||
}
|
||||
Some(expr)
|
||||
});
|
||||
|
||||
quote! {
|
||||
impl #impl_generics ::style_traits::HasViewportPercentage for #name #ty_generics #where_clause {
|
||||
#[allow(unused_variables, unused_imports)]
|
||||
#[inline]
|
||||
fn has_viewport_percentage(&self) -> bool {
|
||||
match *self {
|
||||
#match_body
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn where_predicate(ty: syn::Ty) -> syn::WherePredicate {
|
||||
syn::WherePredicate::BoundPredicate(syn::WhereBoundPredicate {
|
||||
bound_lifetimes: vec![],
|
||||
bounded_ty: ty,
|
||||
bounds: vec![syn::TyParamBound::Trait(
|
||||
syn::PolyTraitRef {
|
||||
bound_lifetimes: vec![],
|
||||
trait_ref: syn::parse_path("::style_traits::HasViewportPercentage").unwrap(),
|
||||
},
|
||||
syn::TraitBoundModifier::None
|
||||
)],
|
||||
})
|
||||
}
|
18
components/style_derive/lib.rs
Normal file
18
components/style_derive/lib.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* 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/. */
|
||||
|
||||
extern crate proc_macro;
|
||||
#[macro_use] extern crate quote;
|
||||
extern crate syn;
|
||||
extern crate synstructure;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
|
||||
mod has_viewport_percentage;
|
||||
|
||||
#[proc_macro_derive(HasViewportPercentage)]
|
||||
pub fn derive_has_viewport_percentage(stream: TokenStream) -> TokenStream {
|
||||
let input = syn::parse_derive_input(&stream.to_string()).unwrap();
|
||||
has_viewport_percentage::derive(input).to_string().parse().unwrap()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue