mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Extract variant code to its own function in style_derive::to_css
This commit is contained in:
parent
14dd4217fa
commit
529a30a682
1 changed files with 84 additions and 77 deletions
|
@ -2,11 +2,11 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use cg;
|
use cg::{self, WhereClause};
|
||||||
use darling::util::Override;
|
use darling::util::Override;
|
||||||
use quote::Tokens;
|
use quote::Tokens;
|
||||||
use syn::{self, Ident};
|
use syn::{self, Ident};
|
||||||
use synstructure;
|
use synstructure::{Structure, VariantInfo};
|
||||||
|
|
||||||
pub fn derive(input: syn::DeriveInput) -> Tokens {
|
pub fn derive(input: syn::DeriveInput) -> Tokens {
|
||||||
let name = &input.ident;
|
let name = &input.ident;
|
||||||
|
@ -15,9 +15,50 @@ pub fn derive(input: syn::DeriveInput) -> Tokens {
|
||||||
cg::trait_parts(&input, &trait_path);
|
cg::trait_parts(&input, &trait_path);
|
||||||
|
|
||||||
let input_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
|
let input_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
|
||||||
let s = synstructure::Structure::new(&input);
|
let s = Structure::new(&input);
|
||||||
|
|
||||||
let match_body = s.each_variant(|variant| {
|
let match_body = s.each_variant(|variant| {
|
||||||
|
derive_variant_arm(variant, &mut where_clause)
|
||||||
|
});
|
||||||
|
|
||||||
|
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 ::style_traits::CssWriter<W>,
|
||||||
|
) -> ::std::fmt::Result
|
||||||
|
where
|
||||||
|
W: ::std::fmt::Write
|
||||||
|
{
|
||||||
|
match *self {
|
||||||
|
#match_body
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if input_attrs.derive_debug {
|
||||||
|
impls.append_all(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,
|
||||||
|
&mut ::style_traits::CssWriter::new(f),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
impls
|
||||||
|
}
|
||||||
|
|
||||||
|
fn derive_variant_arm(
|
||||||
|
variant: &VariantInfo,
|
||||||
|
where_clause: &mut WhereClause,
|
||||||
|
) -> Tokens {
|
||||||
let bindings = variant.bindings();
|
let bindings = variant.bindings();
|
||||||
let identifier = cg::to_css_identifier(variant.ast().ident.as_ref());
|
let identifier = cg::to_css_identifier(variant.ast().ident.as_ref());
|
||||||
let ast = variant.ast();
|
let ast = variant.ast();
|
||||||
|
@ -91,41 +132,7 @@ pub fn derive(input: syn::DeriveInput) -> Tokens {
|
||||||
::std::fmt::Write::write_str(dest, ")")
|
::std::fmt::Write::write_str(dest, ")")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(expr)
|
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 ::style_traits::CssWriter<W>,
|
|
||||||
) -> ::std::fmt::Result
|
|
||||||
where
|
|
||||||
W: ::std::fmt::Write
|
|
||||||
{
|
|
||||||
match *self {
|
|
||||||
#match_body
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if input_attrs.derive_debug {
|
|
||||||
impls.append_all(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,
|
|
||||||
&mut ::style_traits::CssWriter::new(f),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
impls
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[darling(attributes(css), default)]
|
#[darling(attributes(css), default)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue