Auto merge of #17152 - servo:derive-all-the-things, r=emilio

Derive the most trivial ToCss implementations 🌋

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17152)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-06-04 10:02:28 -07:00 committed by GitHub
commit 942fce3a0b
18 changed files with 113 additions and 263 deletions

View file

@ -1074,8 +1074,6 @@
predefined_type=length_type, predefined_type=length_type,
logical=logical, logical=logical,
**kwargs)"> **kwargs)">
use std::fmt;
use style_traits::ToCss;
% if not logical: % if not logical:
use values::specified::AllowQuirks; use values::specified::AllowQuirks;
% endif % endif
@ -1085,8 +1083,8 @@
pub type T = ::values::computed::${length_type}; pub type T = ::values::computed::${length_type};
} }
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToCss)]
pub struct SpecifiedValue(pub ${length_type}); pub struct SpecifiedValue(pub ${length_type});
% if length_type == "MozLength": % if length_type == "MozLength":
@ -1131,12 +1129,6 @@
ret.map(SpecifiedValue) ret.map(SpecifiedValue)
} }
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
impl ToComputedValue for SpecifiedValue { impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T; type ComputedValue = computed_value::T;
#[inline] #[inline]

View file

@ -222,8 +222,8 @@ ${helpers.predefined_type("border-image-outset", "LengthOrNumberRect",
pub mod computed_value { pub mod computed_value {
pub use super::RepeatKeyword; pub use super::RepeatKeyword;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Debug, Clone, PartialEq, ToCss)]
pub struct T(pub RepeatKeyword, pub RepeatKeyword); pub struct T(pub RepeatKeyword, pub RepeatKeyword);
} }
@ -238,14 +238,6 @@ ${helpers.predefined_type("border-image-outset", "LengthOrNumberRect",
"round" => Round, "round" => Round,
"space" => Space); "space" => Space);
impl ToCss for computed_value::T {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.0.to_css(dest));
try!(dest.write_str(" "));
self.1.to_css(dest)
}
}
impl ToCss for SpecifiedValue { impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.0.to_css(dest)); try!(self.0.to_css(dest));

View file

@ -13,8 +13,6 @@
ignored_when_colors_disabled="True" ignored_when_colors_disabled="True"
spec="https://drafts.csswg.org/css-color/#color"> spec="https://drafts.csswg.org/css-color/#color">
use cssparser::RGBA; use cssparser::RGBA;
use std::fmt;
use style_traits::ToCss;
use values::specified::{AllowQuirks, Color, CSSColor}; use values::specified::{AllowQuirks, Color, CSSColor};
impl ToComputedValue for SpecifiedValue { impl ToComputedValue for SpecifiedValue {
@ -31,17 +29,11 @@
} }
} }
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToCss)]
pub struct SpecifiedValue(pub CSSColor); pub struct SpecifiedValue(pub CSSColor);
no_viewport_percentage!(SpecifiedValue); no_viewport_percentage!(SpecifiedValue);
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
pub mod computed_value { pub mod computed_value {
use cssparser; use cssparser;
pub type T = cssparser::RGBA; pub type T = cssparser::RGBA;
@ -91,6 +83,9 @@
%> %>
use gecko_bindings::bindings::Gecko_GetLookAndFeelSystemColor; use gecko_bindings::bindings::Gecko_GetLookAndFeelSystemColor;
use gecko_bindings::structs::root::mozilla::LookAndFeel_ColorID; use gecko_bindings::structs::root::mozilla::LookAndFeel_ColorID;
use std::fmt;
use style_traits::ToCss;
pub type SystemColor = LookAndFeel_ColorID; pub type SystemColor = LookAndFeel_ColorID;
impl ToCss for SystemColor { impl ToCss for SystemColor {

View file

@ -2149,18 +2149,15 @@ ${helpers.single_keyword("-moz-math-variant",
internal="True" disable_when_testing="True"> internal="True" disable_when_testing="True">
use app_units::Au; use app_units::Au;
use gecko_bindings::structs::NS_MATHML_DEFAULT_SCRIPT_MIN_SIZE_PT; use gecko_bindings::structs::NS_MATHML_DEFAULT_SCRIPT_MIN_SIZE_PT;
use std::fmt;
use style_traits::ToCss;
use values::specified::length::{AU_PER_PT, FontBaseSize, NoCalcLength}; use values::specified::length::{AU_PER_PT, FontBaseSize, NoCalcLength};
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)] #[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToCss)]
pub struct SpecifiedValue(pub NoCalcLength); pub struct SpecifiedValue(pub NoCalcLength);
pub mod computed_value { pub mod computed_value {
pub type T = super::Au; pub type T = super::Au;
} }
impl ToComputedValue for SpecifiedValue { impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T; type ComputedValue = computed_value::T;
@ -2185,12 +2182,6 @@ ${helpers.single_keyword("-moz-math-variant",
} }
} }
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
#[inline] #[inline]
pub fn get_initial_value() -> computed_value::T { pub fn get_initial_value() -> computed_value::T {
Au((NS_MATHML_DEFAULT_SCRIPT_MIN_SIZE_PT as f32 * AU_PER_PT) as i32) Au((NS_MATHML_DEFAULT_SCRIPT_MIN_SIZE_PT as f32 * AU_PER_PT) as i32)

View file

@ -31,8 +31,8 @@ ${helpers.single_keyword("caption-side", "top bottom",
use app_units::Au; use app_units::Au;
use properties::animated_properties::Animatable; use properties::animated_properties::Animatable;
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
pub struct T { pub struct T {
pub horizontal: Au, pub horizontal: Au,
pub vertical: Au, pub vertical: Au,
@ -92,14 +92,6 @@ ${helpers.single_keyword("caption-side", "top bottom",
} }
} }
impl ToCss for computed_value::T {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.horizontal.to_css(dest));
try!(dest.write_str(" "));
self.vertical.to_css(dest)
}
}
impl ToComputedValue for SpecifiedValue { impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T; type ComputedValue = computed_value::T;

View file

@ -443,8 +443,8 @@ ${helpers.predefined_type("word-spacing",
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct T(pub SmallVec<[TextShadow; 1]>); pub struct T(pub SmallVec<[TextShadow; 1]>);
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, PartialEq, Debug, ToCss)]
pub struct TextShadow { pub struct TextShadow {
pub offset_x: Au, pub offset_x: Au,
pub offset_y: Au, pub offset_y: Au,
@ -468,18 +468,6 @@ ${helpers.predefined_type("word-spacing",
} }
} }
impl ToCss for computed_value::TextShadow {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.offset_x.to_css(dest)?;
dest.write_str(" ")?;
self.offset_y.to_css(dest)?;
dest.write_str(" ")?;
self.blur_radius.to_css(dest)?;
dest.write_str(" ")?;
self.color.to_css(dest)
}
}
impl ToCss for SpecifiedValue { impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
let mut iter = self.0.iter(); let mut iter = self.0.iter();
@ -767,7 +755,6 @@ ${helpers.predefined_type("word-spacing",
<%helpers:longhand name="text-emphasis-position" animation_value_type="none" products="gecko" <%helpers:longhand name="text-emphasis-position" animation_value_type="none" products="gecko"
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-position"> spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-position">
use std::fmt;
use values::computed::ComputedValueAsSpecified; use values::computed::ComputedValueAsSpecified;
use style_traits::ToCss; use style_traits::ToCss;
@ -778,8 +765,8 @@ ${helpers.predefined_type("word-spacing",
"right" => Right, "right" => Right,
"left" => Left); "left" => Left);
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Debug, Clone, PartialEq, ToCss)]
pub struct SpecifiedValue(pub HorizontalWritingModeValue, pub VerticalWritingModeValue); pub struct SpecifiedValue(pub HorizontalWritingModeValue, pub VerticalWritingModeValue);
pub mod computed_value { pub mod computed_value {
@ -804,15 +791,6 @@ ${helpers.predefined_type("word-spacing",
} }
} }
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
let SpecifiedValue(horizontal, vertical) = *self;
try!(horizontal.to_css(dest));
try!(dest.write_char(' '));
vertical.to_css(dest)
}
}
% if product == "gecko": % if product == "gecko":
impl SpecifiedValue { impl SpecifiedValue {
pub fn from_gecko_keyword(kw: u32) -> Self { pub fn from_gecko_keyword(kw: u32) -> Self {

View file

@ -114,17 +114,15 @@ ${helpers.single_keyword("list-style-position", "outside inside", animation_valu
<%helpers:longhand name="list-style-image" animation_value_type="none" <%helpers:longhand name="list-style-image" animation_value_type="none"
boxed="${product == 'gecko'}" boxed="${product == 'gecko'}"
spec="https://drafts.csswg.org/css-lists/#propdef-list-style-image"> spec="https://drafts.csswg.org/css-lists/#propdef-list-style-image">
use std::fmt;
use values::computed::ComputedValueAsSpecified; use values::computed::ComputedValueAsSpecified;
use values::specified::UrlOrNone; use values::specified::UrlOrNone;
pub use self::computed_value::T as SpecifiedValue; pub use self::computed_value::T as SpecifiedValue;
use style_traits::ToCss;
pub mod computed_value { pub mod computed_value {
use values::specified::UrlOrNone; use values::specified::UrlOrNone;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Debug, Clone, PartialEq, ToCss)]
pub struct T(pub UrlOrNone); pub struct T(pub UrlOrNone);
} }
@ -132,12 +130,6 @@ ${helpers.single_keyword("list-style-position", "outside inside", animation_valu
impl ComputedValueAsSpecified for SpecifiedValue {} impl ComputedValueAsSpecified for SpecifiedValue {}
no_viewport_percentage!(SpecifiedValue); no_viewport_percentage!(SpecifiedValue);
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
#[inline] #[inline]
pub fn get_initial_value() -> computed_value::T { pub fn get_initial_value() -> computed_value::T {
computed_value::T(Either::Second(None_)) computed_value::T(Either::Second(None_))

View file

@ -623,9 +623,9 @@ pub type LengthOrNormal = Either<Length, Normal>;
/// A value suitable for a `min-width`, `min-height`, `width` or `height` property. /// A value suitable for a `min-width`, `min-height`, `width` or `height` property.
/// See specified/values/length.rs for more details. /// See specified/values/length.rs for more details.
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
pub enum MozLength { pub enum MozLength {
LengthOrPercentageOrAuto(LengthOrPercentageOrAuto), LengthOrPercentageOrAuto(LengthOrPercentageOrAuto),
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
@ -665,22 +665,11 @@ impl ToComputedValue for specified::MozLength {
} }
} }
impl ToCss for MozLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
MozLength::LengthOrPercentageOrAuto(lopoa) =>
lopoa.to_css(dest),
MozLength::ExtremumLength(ext) =>
ext.to_css(dest),
}
}
}
/// A value suitable for a `max-width` or `max-height` property. /// A value suitable for a `max-width` or `max-height` property.
/// See specified/values/length.rs for more details. /// See specified/values/length.rs for more details.
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
pub enum MaxLength { pub enum MaxLength {
LengthOrPercentageOrNone(LengthOrPercentageOrNone), LengthOrPercentageOrNone(LengthOrPercentageOrNone),
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
@ -718,14 +707,3 @@ impl ToComputedValue for specified::MaxLength {
} }
} }
} }
impl ToCss for MaxLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
MaxLength::LengthOrPercentageOrNone(lopon) =>
lopon.to_css(dest),
MaxLength::ExtremumLength(ext) =>
ext.to_css(dest),
}
}
}

View file

@ -481,9 +481,9 @@ pub struct Shadow {
/// A `<number>` value. /// A `<number>` value.
pub type Number = CSSFloat; pub type Number = CSSFloat;
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
pub enum NumberOrPercentage { pub enum NumberOrPercentage {
Percentage(Percentage), Percentage(Percentage),
Number(Number), Number(Number),
@ -512,15 +512,6 @@ impl ToComputedValue for specified::NumberOrPercentage {
} }
} }
impl ToCss for NumberOrPercentage {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
NumberOrPercentage::Percentage(percentage) => percentage.to_css(dest),
NumberOrPercentage::Number(number) => number.to_css(dest),
}
}
}
/// A type used for opacity. /// A type used for opacity.
pub type Opacity = CSSFloat; pub type Opacity = CSSFloat;

View file

@ -53,7 +53,7 @@ pub enum ShapeSource<BasicShape, ReferenceBox> {
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToComputedValue)] #[derive(Clone, Debug, PartialEq, ToComputedValue, ToCss)]
pub enum BasicShape<H, V, LengthOrPercentage> { pub enum BasicShape<H, V, LengthOrPercentage> {
Inset(InsetRect<LengthOrPercentage>), Inset(InsetRect<LengthOrPercentage>),
Circle(Circle<H, V, LengthOrPercentage>), Circle(Circle<H, V, LengthOrPercentage>),
@ -153,23 +153,6 @@ impl ToCss for GeometryBox {
} }
} }
impl<H, V, L> ToCss for BasicShape<H, V, L>
where H: ToCss,
V: ToCss,
L: PartialEq + ToCss,
Circle<H, V, L>: ToCss,
Ellipse<H, V, L>: ToCss,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
BasicShape::Inset(ref rect) => rect.to_css(dest),
BasicShape::Circle(ref circle) => circle.to_css(dest),
BasicShape::Ellipse(ref ellipse) => ellipse.to_css(dest),
BasicShape::Polygon(ref polygon) => polygon.to_css(dest),
}
}
}
impl<L> ToCss for InsetRect<L> impl<L> ToCss for InsetRect<L>
where L: ToCss + PartialEq where L: ToCss + PartialEq
{ {

View file

@ -108,8 +108,8 @@ impl ComputedValueAsSpecified for ShapeExtent {}
/// A gradient item. /// A gradient item.
/// https://drafts.csswg.org/css-images-4/#color-stop-syntax /// https://drafts.csswg.org/css-images-4/#color-stop-syntax
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
pub enum GradientItem<Color, LengthOrPercentage> { pub enum GradientItem<Color, LengthOrPercentage> {
/// A color stop. /// A color stop.
ColorStop(ColorStop<Color, LengthOrPercentage>), ColorStop(ColorStop<Color, LengthOrPercentage>),
@ -288,17 +288,6 @@ impl<L, LoP> ToCss for EndingShape<L, LoP>
} }
} }
impl<C, L> ToCss for GradientItem<C, L>
where C: ToCss, L: ToCss,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
GradientItem::ColorStop(ref stop) => stop.to_css(dest),
GradientItem::InterpolationHint(ref hint) => hint.to_css(dest),
}
}
}
impl<C, L> fmt::Debug for ColorStop<C, L> impl<C, L> fmt::Debug for ColorStop<C, L>
where C: fmt::Debug, L: fmt::Debug, where C: fmt::Debug, L: fmt::Debug,
{ {

View file

@ -4,12 +4,9 @@
//! Generic types for CSS values that are related to transformations. //! Generic types for CSS values that are related to transformations.
use std::fmt;
use style_traits::ToCss;
/// A generic transform origin. /// A generic transform origin.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)] #[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
pub struct TransformOrigin<H, V, Depth> { pub struct TransformOrigin<H, V, Depth> {
/// The horizontal origin. /// The horizontal origin.
pub horizontal: H, pub horizontal: H,
@ -29,17 +26,3 @@ impl<H, V, D> TransformOrigin<H, V, D> {
} }
} }
} }
impl<H, V, D> ToCss for TransformOrigin<H, V, D>
where H: ToCss, V: ToCss, D: ToCss,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write,
{
self.horizontal.to_css(dest)?;
dest.write_str(" ")?;
self.vertical.to_css(dest)?;
dest.write_str(" ")?;
self.depth.to_css(dest)
}
}

View file

@ -34,9 +34,9 @@ define_keyword_type!(None_, "none");
define_keyword_type!(Auto, "auto"); define_keyword_type!(Auto, "auto");
define_keyword_type!(Normal, "normal"); define_keyword_type!(Normal, "normal");
#[derive(Clone, Copy, HasViewportPercentage, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
/// A struct representing one of two kinds of values. /// A struct representing one of two kinds of values.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, HasViewportPercentage, PartialEq, ToCss)]
pub enum Either<A, B> { pub enum Either<A, B> {
/// The first value. /// The first value.
First(A), First(A),
@ -53,15 +53,6 @@ impl<A: Debug, B: Debug> Debug for Either<A, B> {
} }
} }
impl<A: ToCss, B: ToCss> ToCss for Either<A, B> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
Either::First(ref v) => v.to_css(dest),
Either::Second(ref v) => v.to_css(dest),
}
}
}
impl<A: Parse, B: Parse> Parse for Either<A, B> { impl<A: Parse, B: Parse> Parse for Either<A, B> {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Either<A, B>, ()> { fn parse(context: &ParserContext, input: &mut Parser) -> Result<Either<A, B>, ()> {
if let Ok(v) = input.try(|i| A::parse(context, i)) { if let Ok(v) = input.try(|i| A::parse(context, i)) {

View file

@ -209,7 +209,7 @@ impl Parse for AlignJustifyContent {
/// Value of the `align-self` or `justify-self` property. /// Value of the `align-self` or `justify-self` property.
/// ///
/// https://drafts.csswg.org/css-align/#self-alignment /// https://drafts.csswg.org/css-align/#self-alignment
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq, ToCss)]
pub struct AlignJustifySelf(pub AlignFlags); pub struct AlignJustifySelf(pub AlignFlags);
impl AlignJustifySelf { impl AlignJustifySelf {
@ -228,12 +228,6 @@ impl AlignJustifySelf {
no_viewport_percentage!(AlignJustifySelf); no_viewport_percentage!(AlignJustifySelf);
impl ToCss for AlignJustifySelf {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
impl Parse for AlignJustifySelf { impl Parse for AlignJustifySelf {
// auto | normal | stretch | <baseline-position> | // auto | normal | stretch | <baseline-position> |
// [ <overflow-position>? && <self-position> ] // [ <overflow-position>? && <self-position> ]
@ -253,7 +247,7 @@ impl Parse for AlignJustifySelf {
/// Value of the `align-items` property /// Value of the `align-items` property
/// ///
/// https://drafts.csswg.org/css-align/#self-alignment /// https://drafts.csswg.org/css-align/#self-alignment
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq, ToCss)]
pub struct AlignItems(pub AlignFlags); pub struct AlignItems(pub AlignFlags);
impl AlignItems { impl AlignItems {
@ -272,12 +266,6 @@ impl AlignItems {
no_viewport_percentage!(AlignItems); no_viewport_percentage!(AlignItems);
impl ToCss for AlignItems {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
impl Parse for AlignItems { impl Parse for AlignItems {
// normal | stretch | <baseline-position> | // normal | stretch | <baseline-position> |
// [ <overflow-position>? && <self-position> ] // [ <overflow-position>? && <self-position> ]
@ -297,7 +285,7 @@ impl Parse for AlignItems {
/// Value of the `justify-items` property /// Value of the `justify-items` property
/// ///
/// https://drafts.csswg.org/css-align/#justify-items-property /// https://drafts.csswg.org/css-align/#justify-items-property
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq, ToCss)]
pub struct JustifyItems(pub AlignFlags); pub struct JustifyItems(pub AlignFlags);
impl JustifyItems { impl JustifyItems {
@ -316,12 +304,6 @@ impl JustifyItems {
no_viewport_percentage!(JustifyItems); no_viewport_percentage!(JustifyItems);
impl ToCss for JustifyItems {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
impl Parse for JustifyItems { impl Parse for JustifyItems {
// auto | normal | stretch | <baseline-position> | // auto | normal | stretch | <baseline-position> |
// [ <overflow-position>? && <self-position> ] // [ <overflow-position>? && <self-position> ]

View file

@ -531,8 +531,8 @@ impl NoCalcLength {
/// This is commonly used for the `<length>` values. /// This is commonly used for the `<length>` values.
/// ///
/// https://drafts.csswg.org/css-values/#lengths /// https://drafts.csswg.org/css-values/#lengths
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToCss)]
pub enum Length { pub enum Length {
/// The internal length type that cannot parse `calc` /// The internal length type that cannot parse `calc`
NoCalc(NoCalcLength), NoCalc(NoCalcLength),
@ -549,15 +549,6 @@ impl From<NoCalcLength> for Length {
} }
} }
impl ToCss for Length {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
Length::NoCalc(ref inner) => inner.to_css(dest),
Length::Calc(ref calc) => calc.to_css(dest),
}
}
}
impl Mul<CSSFloat> for Length { impl Mul<CSSFloat> for Length {
type Output = Length; type Output = Length;
@ -758,9 +749,9 @@ impl Parse for Percentage {
impl ComputedValueAsSpecified for Percentage {} impl ComputedValueAsSpecified for Percentage {}
/// A length or a percentage value. /// A length or a percentage value.
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToCss)]
pub enum LengthOrPercentage { pub enum LengthOrPercentage {
Length(NoCalcLength), Length(NoCalcLength),
Percentage(Percentage), Percentage(Percentage),
@ -790,16 +781,6 @@ impl From<Percentage> for LengthOrPercentage {
} }
} }
impl ToCss for LengthOrPercentage {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
LengthOrPercentage::Length(ref length) => length.to_css(dest),
LengthOrPercentage::Percentage(percentage) => percentage.to_css(dest),
LengthOrPercentage::Calc(ref calc) => calc.to_css(dest),
}
}
}
impl LengthOrPercentage { impl LengthOrPercentage {
#[inline] #[inline]
/// Returns a `zero` length. /// Returns a `zero` length.
@ -1213,25 +1194,14 @@ impl LengthOrNumber {
/// A value suitable for a `min-width` or `min-height` property. /// A value suitable for a `min-width` or `min-height` property.
/// Unlike `max-width` or `max-height` properties, a MozLength can be /// Unlike `max-width` or `max-height` properties, a MozLength can be
/// `auto`, and cannot be `none`. /// `auto`, and cannot be `none`.
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToCss)]
pub enum MozLength { pub enum MozLength {
LengthOrPercentageOrAuto(LengthOrPercentageOrAuto), LengthOrPercentageOrAuto(LengthOrPercentageOrAuto),
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
} }
impl ToCss for MozLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
MozLength::LengthOrPercentageOrAuto(ref lopoa) =>
lopoa.to_css(dest),
MozLength::ExtremumLength(ref ext) =>
ext.to_css(dest),
}
}
}
impl Parse for MozLength { impl Parse for MozLength {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> { fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
MozLength::parse_quirky(context, input, AllowQuirks::No) MozLength::parse_quirky(context, input, AllowQuirks::No)
@ -1250,25 +1220,14 @@ impl MozLength {
} }
/// A value suitable for a `max-width` or `max-height` property. /// A value suitable for a `max-width` or `max-height` property.
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToCss)]
pub enum MaxLength { pub enum MaxLength {
LengthOrPercentageOrNone(LengthOrPercentageOrNone), LengthOrPercentageOrNone(LengthOrPercentageOrNone),
ExtremumLength(ExtremumLength), ExtremumLength(ExtremumLength),
} }
impl ToCss for MaxLength {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
MaxLength::LengthOrPercentageOrNone(ref lopon) =>
lopon.to_css(dest),
MaxLength::ExtremumLength(ref ext) =>
ext.to_css(dest),
}
}
}
impl Parse for MaxLength { impl Parse for MaxLength {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> { fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
MaxLength::parse_quirky(context, input, AllowQuirks::No) MaxLength::parse_quirky(context, input, AllowQuirks::No)

View file

@ -652,9 +652,9 @@ impl ToCss for Number {
/// <number-percentage> /// <number-percentage>
/// Accepts only non-negative numbers. /// Accepts only non-negative numbers.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
pub enum NumberOrPercentage { pub enum NumberOrPercentage {
Percentage(Percentage), Percentage(Percentage),
Number(Number), Number(Number),
@ -686,18 +686,9 @@ impl Parse for NumberOrPercentage {
} }
} }
impl ToCss for NumberOrPercentage {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
NumberOrPercentage::Percentage(percentage) => percentage.to_css(dest),
NumberOrPercentage::Number(number) => number.to_css(dest),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, ToCss)]
pub struct Opacity(Number); pub struct Opacity(Number);
no_viewport_percentage!(Opacity); no_viewport_percentage!(Opacity);
@ -722,12 +713,6 @@ impl ToComputedValue for Opacity {
} }
} }
impl ToCss for Opacity {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)] #[allow(missing_docs)]

View file

@ -11,6 +11,7 @@ use proc_macro::TokenStream;
mod has_viewport_percentage; mod has_viewport_percentage;
mod to_computed_value; mod to_computed_value;
mod to_css;
#[proc_macro_derive(HasViewportPercentage)] #[proc_macro_derive(HasViewportPercentage)]
pub fn derive_has_viewport_percentage(stream: TokenStream) -> TokenStream { pub fn derive_has_viewport_percentage(stream: TokenStream) -> TokenStream {
@ -23,3 +24,9 @@ pub fn derive_to_computed_value(stream: TokenStream) -> TokenStream {
let input = syn::parse_derive_input(&stream.to_string()).unwrap(); let input = syn::parse_derive_input(&stream.to_string()).unwrap();
to_computed_value::derive(input).to_string().parse().unwrap() to_computed_value::derive(input).to_string().parse().unwrap()
} }
#[proc_macro_derive(ToCss)]
pub fn derive_to_css(stream: TokenStream) -> TokenStream {
let input = syn::parse_derive_input(&stream.to_string()).unwrap();
to_css::derive(input).to_string().parse().unwrap()
}

View file

@ -0,0 +1,70 @@
/* 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, _| {
if bindings.is_empty() {
panic!("unit variants are not yet supported");
}
let (first, rest) = bindings.split_first().expect("unit variants are not yet supported");
where_clause.predicates.push(where_predicate(first.field.ty.clone()));
let mut expr = quote! {
::style_traits::ToCss::to_css(#first, dest)
};
for binding in rest {
where_clause.predicates.push(where_predicate(binding.field.ty.clone()));
expr = quote! {
#expr?;
dest.write_str(" ")?;
::style_traits::ToCss::to_css(#binding, dest)
};
}
Some(expr)
});
quote! {
impl #impl_generics ::style_traits::ToCss for #name #ty_generics #where_clause {
#[allow(unused_variables, unused_imports)]
#[inline]
fn to_css<W>(&self, dest: &mut W) -> ::std::fmt::Result
where
W: ::std::fmt::Write
{
match *self {
#match_body
}
}
}
}
}
/// `#ty: ::style_traits::ToCss`
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::Path {
global: true,
segments: vec!["style_traits".into(), "ToCss".into()],
},
},
syn::TraitBoundModifier::None
)],
})
}