gecko-dev/servo/components/style_derive/to_css.rs
CYBAI 08b5d999f0 servo: Merge #19583 - style: Move will-change outside of mako (from CYBAI:move-will-change-out-of-mako); r=emilio
This is a sub-PR of #19015
r? emilio

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #19582
- [x] These changes do not require tests

Source-Repo: https://github.com/servo/servo
Source-Revision: 8aba41d951d63a09cc6c12cba7ed200ea4396544

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 24b27ac8fd2e837f365610ecd737993588b6ea98
2017-12-16 16:39:37 -06:00

121 lines
3.6 KiB
Rust

/* 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::DeriveInput;
use synstructure;
pub fn derive(input: DeriveInput) -> Tokens {
let name = &input.ident;
let trait_path = &["style_traits", "ToCss"];
let (impl_generics, ty_generics, mut where_clause) =
cg::trait_parts(&input, trait_path);
let input_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
let style = synstructure::BindStyle::Ref.into();
let match_body = synstructure::each_variant(&input, &style, |bindings, variant| {
let mut identifier = cg::to_css_identifier(variant.ident.as_ref());
let variant_attrs = cg::parse_variant_attrs::<CssVariantAttrs>(variant);
let separator = if variant_attrs.comma { ", " } else { " " };
if variant_attrs.dimension {
assert_eq!(bindings.len(), 1);
assert!(!variant_attrs.function, "That makes no sense");
}
let mut expr = if !bindings.is_empty() {
let mut expr = quote! {};
if variant_attrs.iterable {
assert_eq!(bindings.len(), 1);
let binding = &bindings[0];
expr = quote! {
#expr
for item in #binding.iter() {
writer.item(item)?;
}
};
} else {
for binding in bindings {
where_clause.add_trait_bound(&binding.field.ty);
expr = quote! {
#expr
writer.item(#binding)?;
};
}
}
quote! {{
let mut writer = ::style_traits::values::SequenceWriter::new(&mut *dest, #separator);
#expr
Ok(())
}}
} else {
quote! {
::std::fmt::Write::write_str(dest, #identifier)
}
};
if variant_attrs.dimension {
expr = quote! {
#expr?;
::std::fmt::Write::write_str(dest, #identifier)
}
} else if variant_attrs.function {
identifier.push_str("(");
expr = quote! {
::std::fmt::Write::write_str(dest, #identifier)?;
#expr?;
::std::fmt::Write::write_str(dest, ")")
}
}
Some(expr)
});
let mut impls = quote! {
impl #impl_generics ::style_traits::ToCss for #name #ty_generics #where_clause {
#[allow(unused_variables)]
#[inline]
fn to_css<W>(&self, dest: &mut W) -> ::std::fmt::Result
where
W: ::std::fmt::Write
{
match *self {
#match_body
}
}
}
};
if input_attrs.derive_debug {
impls.append(quote! {
impl #impl_generics ::std::fmt::Debug for #name #ty_generics #where_clause {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
::style_traits::ToCss::to_css(self, f)
}
}
});
}
impls
}
#[darling(attributes(css), default)]
#[derive(Default, FromDeriveInput)]
struct CssInputAttrs {
derive_debug: bool,
function: bool,
comma: bool,
}
#[darling(attributes(css), default)]
#[derive(Default, FromVariant)]
struct CssVariantAttrs {
function: bool,
iterable: bool,
comma: bool,
dimension: bool,
}