Implement #[css(derive_debug)]

This makes #[derive(ToCss)] derive Debug with a simple call to the to_css method.
This commit is contained in:
Anthony Ramine 2017-08-26 16:55:42 +02:00
parent 1ace6d4c7f
commit 4faadb489f
2 changed files with 33 additions and 41 deletions

View file

@ -277,6 +277,7 @@ impl ToComputedValue for specified::CalcLengthOrPercentage {
#[allow(missing_docs)] #[allow(missing_docs)]
#[animate(fallback = "Self::animate_fallback")] #[animate(fallback = "Self::animate_fallback")]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[css(derive_debug)]
#[derive(Animate, Clone, Copy, PartialEq, ToAnimatedZero, ToCss)] #[derive(Animate, Clone, Copy, PartialEq, ToAnimatedZero, ToCss)]
pub enum LengthOrPercentage { pub enum LengthOrPercentage {
Length(Au), Length(Au),
@ -391,16 +392,6 @@ impl LengthOrPercentage {
} }
} }
impl fmt::Debug for LengthOrPercentage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LengthOrPercentage::Length(length) => write!(f, "{:?}", length),
LengthOrPercentage::Percentage(percentage) => write!(f, "{}%", percentage.0 * 100.),
LengthOrPercentage::Calc(calc) => write!(f, "{:?}", calc),
}
}
}
impl ToComputedValue for specified::LengthOrPercentage { impl ToComputedValue for specified::LengthOrPercentage {
type ComputedValue = LengthOrPercentage; type ComputedValue = LengthOrPercentage;
@ -440,6 +431,7 @@ impl ToComputedValue for specified::LengthOrPercentage {
#[allow(missing_docs)] #[allow(missing_docs)]
#[animate(fallback = "Self::animate_fallback")] #[animate(fallback = "Self::animate_fallback")]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[css(derive_debug)]
#[derive(Animate, Clone, Copy, PartialEq, ToCss)] #[derive(Animate, Clone, Copy, PartialEq, ToCss)]
pub enum LengthOrPercentageOrAuto { pub enum LengthOrPercentageOrAuto {
Length(Au), Length(Au),
@ -495,17 +487,6 @@ impl LengthOrPercentageOrAuto {
} }
} }
impl fmt::Debug for LengthOrPercentageOrAuto {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LengthOrPercentageOrAuto::Length(length) => write!(f, "{:?}", length),
LengthOrPercentageOrAuto::Percentage(percentage) => write!(f, "{}%", percentage.0 * 100.),
LengthOrPercentageOrAuto::Auto => write!(f, "auto"),
LengthOrPercentageOrAuto::Calc(calc) => write!(f, "{:?}", calc),
}
}
}
impl ToComputedValue for specified::LengthOrPercentageOrAuto { impl ToComputedValue for specified::LengthOrPercentageOrAuto {
type ComputedValue = LengthOrPercentageOrAuto; type ComputedValue = LengthOrPercentageOrAuto;
@ -551,6 +532,7 @@ impl ToComputedValue for specified::LengthOrPercentageOrAuto {
#[allow(missing_docs)] #[allow(missing_docs)]
#[animate(fallback = "Self::animate_fallback")] #[animate(fallback = "Self::animate_fallback")]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[css(derive_debug)]
#[derive(Animate, Clone, Copy, PartialEq, ToCss)] #[derive(Animate, Clone, Copy, PartialEq, ToCss)]
pub enum LengthOrPercentageOrNone { pub enum LengthOrPercentageOrNone {
Length(Au), Length(Au),
@ -603,17 +585,6 @@ impl LengthOrPercentageOrNone {
} }
} }
impl fmt::Debug for LengthOrPercentageOrNone {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LengthOrPercentageOrNone::Length(length) => write!(f, "{:?}", length),
LengthOrPercentageOrNone::Percentage(percentage) => write!(f, "{}%", percentage.0 * 100.),
LengthOrPercentageOrNone::Calc(calc) => write!(f, "{:?}", calc),
LengthOrPercentageOrNone::None => write!(f, "none"),
}
}
}
impl ToComputedValue for specified::LengthOrPercentageOrNone { impl ToComputedValue for specified::LengthOrPercentageOrNone {
type ComputedValue = LengthOrPercentageOrNone; type ComputedValue = LengthOrPercentageOrNone;

View file

@ -3,21 +3,22 @@
* 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;
use quote; use quote::Tokens;
use syn; use syn::DeriveInput;
use synstructure; use synstructure;
pub fn derive(input: syn::DeriveInput) -> quote::Tokens { pub fn derive(input: DeriveInput) -> Tokens {
let name = &input.ident; let name = &input.ident;
let trait_path = &["style_traits", "ToCss"]; let trait_path = &["style_traits", "ToCss"];
let (impl_generics, ty_generics, mut where_clause) = let (impl_generics, ty_generics, mut where_clause) =
cg::trait_parts(&input, trait_path); cg::trait_parts(&input, trait_path);
let input_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
let style = synstructure::BindStyle::Ref.into(); let style = synstructure::BindStyle::Ref.into();
let match_body = synstructure::each_variant(&input, &style, |bindings, variant| { let match_body = synstructure::each_variant(&input, &style, |bindings, variant| {
let mut identifier = to_css_identifier(variant.ident.as_ref()); let mut identifier = to_css_identifier(variant.ident.as_ref());
let css_attrs = cg::parse_variant_attrs::<CssAttrs>(variant); let variant_attrs = cg::parse_variant_attrs::<CssVariantAttrs>(variant);
let separator = if css_attrs.comma { ", " } else { " " }; let separator = if variant_attrs.comma { ", " } else { " " };
let mut expr = if !bindings.is_empty() { let mut expr = if !bindings.is_empty() {
let mut expr = quote! {}; let mut expr = quote! {};
for binding in bindings { for binding in bindings {
@ -37,7 +38,7 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
::std::fmt::Write::write_str(dest, #identifier) ::std::fmt::Write::write_str(dest, #identifier)
} }
}; };
if css_attrs.function { if variant_attrs.function {
identifier.push_str("("); identifier.push_str("(");
expr = quote! { expr = quote! {
::std::fmt::Write::write_str(dest, #identifier)?; ::std::fmt::Write::write_str(dest, #identifier)?;
@ -48,7 +49,7 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
Some(expr) Some(expr)
}); });
quote! { let mut impls = quote! {
impl #impl_generics ::style_traits::ToCss for #name #ty_generics #where_clause { impl #impl_generics ::style_traits::ToCss for #name #ty_generics #where_clause {
#[allow(unused_variables)] #[allow(unused_variables)]
#[inline] #[inline]
@ -61,12 +62,32 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
} }
} }
} }
};
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
} }
#[derive(Default, FromVariant)]
#[darling(attributes(css), default)] #[darling(attributes(css), default)]
struct CssAttrs { #[derive(Default, FromDeriveInput)]
struct CssInputAttrs {
derive_debug: bool,
function: bool,
comma: bool,
}
#[darling(attributes(css), default)]
#[derive(Default, FromVariant)]
struct CssVariantAttrs {
function: bool, function: bool,
comma: bool, comma: bool,
} }