Support unit variants when deriving ToCss

This commit is contained in:
Anthony Ramine 2017-06-07 14:53:31 +02:00
parent 7d09ce0495
commit 45e8b0e8c7
14 changed files with 71 additions and 242 deletions

View file

@ -4,11 +4,8 @@
//! Generic types for CSS values related to backgrounds.
use std::fmt;
use style_traits::ToCss;
/// A generic value for the `background-size` property.
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum BackgroundSize<LengthOrPercentageOrAuto> {
/// `<width> <height>`
@ -32,21 +29,3 @@ impl<L> From<L> for BackgroundSize<L>
BackgroundSize::Explicit { width: value.clone(), height: value }
}
}
impl<L> ToCss for BackgroundSize<L>
where L: ToCss
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
match *self {
BackgroundSize::Explicit { ref width, ref height } => {
width.to_css(dest)?;
dest.write_str(" ")?;
height.to_css(dest)
},
BackgroundSize::Cover => dest.write_str("cover"),
BackgroundSize::Contain => dest.write_str("contain"),
}
}
}

View file

@ -19,7 +19,7 @@ pub type ClippingShape<BasicShape> = ShapeSource<BasicShape, GeometryBox>;
/// https://drafts.fxtf.org/css-masking-1/#typedef-geometry-box
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
pub enum GeometryBox {
FillBox,
StrokeBox,
@ -142,17 +142,6 @@ impl<B: ToCss, T: ToCss> ToCss for ShapeSource<B, T> {
}
}
impl ToCss for GeometryBox {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
GeometryBox::FillBox => dest.write_str("fill-box"),
GeometryBox::StrokeBox => dest.write_str("stroke-box"),
GeometryBox::ViewBox => dest.write_str("view-box"),
GeometryBox::ShapeBox(s) => s.to_css(dest),
}
}
}
impl<L> ToCss for InsetRect<L>
where L: ToCss + PartialEq
{

View file

@ -11,7 +11,7 @@ use values::generics::rect::Rect;
/// A generic value for a single side of a `border-image-width` property.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
pub enum BorderImageSideWidth<LengthOrPercentage, Number> {
/// `<length-or-percentage>`
Length(LengthOrPercentage),
@ -52,20 +52,6 @@ pub struct BorderRadius<LengthOrPercentage> {
/// A generic value for `border-*-radius` longhand properties.
pub struct BorderCornerRadius<L>(pub Size2D<L>);
impl<L, N> ToCss for BorderImageSideWidth<L, N>
where L: ToCss, N: ToCss,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
match *self {
BorderImageSideWidth::Length(ref length) => length.to_css(dest),
BorderImageSideWidth::Number(ref number) => number.to_css(dest),
BorderImageSideWidth::Auto => dest.write_str("auto"),
}
}
}
impl<N> From<N> for BorderImageSlice<N>
where N: Clone,
{

View file

@ -329,8 +329,8 @@ pub fn concat_serialize_idents<W>(prefix: &str, suffix: &str,
/// The initial argument of the `repeat` function.
///
/// https://drafts.csswg.org/css-grid/#typedef-track-repeat
#[derive(Clone, Copy, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
pub enum RepeatCount {
/// A positive integer. This is allowed only for `<track-repeat>` and `<fixed-repeat>`
Number(Integer),
@ -340,16 +340,6 @@ pub enum RepeatCount {
AutoFit,
}
impl ToCss for RepeatCount {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
RepeatCount::Number(ref c) => c.to_css(dest),
RepeatCount::AutoFill => dest.write_str("auto-fill"),
RepeatCount::AutoFit => dest.write_str("auto-fit"),
}
}
}
impl Parse for RepeatCount {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if let Ok(i) = input.try(|i| Integer::parse(context, i)) {

View file

@ -182,8 +182,8 @@ impl<T: Parse> Parse for FontSettingTag<T> {
/// A font settings value for font-variation-settings or font-feature-settings
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, Eq, PartialEq, ToCss)]
pub enum FontSettings<T> {
/// No settings (default)
Normal,
@ -191,15 +191,6 @@ pub enum FontSettings<T> {
Tag(Vec<FontSettingTag<T>>)
}
impl<T: ToCss> ToCss for FontSettings<T> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
FontSettings::Normal => dest.write_str("normal"),
FontSettings::Tag(ref ftvs) => ftvs.to_css(dest)
}
}
}
impl<T: Parse> Parse for FontSettings<T> {
/// https://www.w3.org/TR/css-fonts-3/#propdef-font-feature-settings
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
@ -290,8 +281,8 @@ pub struct SVGPaint<ColorType> {
/// Whereas the spec only allows PaintServer
/// to have a fallback, Gecko lets the context
/// properties have a fallback as well.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToCss)]
pub enum SVGPaintKind<ColorType> {
/// `none`
None,
@ -378,18 +369,6 @@ impl<ColorType: Parse> Parse for SVGPaint<ColorType> {
}
}
impl<ColorType: ToCss> ToCss for SVGPaintKind<ColorType> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
SVGPaintKind::None => dest.write_str("none"),
SVGPaintKind::ContextStroke => dest.write_str("context-stroke"),
SVGPaintKind::ContextFill => dest.write_str("context-fill"),
SVGPaintKind::Color(ref color) => color.to_css(dest),
SVGPaintKind::PaintServer(ref server) => server.to_css(dest),
}
}
}
impl<ColorType: ToCss> ToCss for SVGPaint<ColorType> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.kind.to_css(dest)?;

View file

@ -54,7 +54,7 @@ where
/// A generic spacing value for the `letter-spacing` and `word-spacing` properties.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
pub enum Spacing<Value> {
/// `normal`
Normal,
@ -117,22 +117,9 @@ impl<Value> Animatable for Spacing<Value>
}
}
impl<Value> ToCss for Spacing<Value>
where Value: ToCss,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
match *self {
Spacing::Normal => dest.write_str("normal"),
Spacing::Value(ref value) => value.to_css(dest),
}
}
}
/// A generic value for the `line-height` property.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq)]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToCss)]
pub enum LineHeight<Number, LengthOrPercentage> {
/// `normal`
Normal,
@ -152,19 +139,3 @@ impl<N, L> LineHeight<N, L> {
LineHeight::Normal
}
}
impl<N, L> ToCss for LineHeight<N, L>
where N: ToCss, L: ToCss,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write,
{
match *self {
LineHeight::Normal => dest.write_str("normal"),
#[cfg(feature = "gecko")]
LineHeight::MozBlockHeight => dest.write_str("-moz-block-height"),
LineHeight::Number(ref number) => number.to_css(dest),
LineHeight::Length(ref value) => value.to_css(dest),
}
}
}