Simplify machinery to serialise optional parts of CSS values

We simply implement ToCss for Option<T>, printing nothing if the value is None,
and we then use SequenceWriter to skip writing of separators around empty parts.
This commit is contained in:
Anthony Ramine 2017-06-21 10:15:31 +02:00
parent cedd5222d2
commit 39e29f557e
8 changed files with 152 additions and 131 deletions

View file

@ -43,7 +43,7 @@ add_impls_for_keyword_enum!(ShapeBox);
/// A shape source, for some reference box.
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToComputedValue)]
#[derive(Clone, Debug, PartialEq, ToComputedValue, ToCss)]
pub enum ShapeSource<BasicShape, ReferenceBox> {
Url(SpecifiedUrl),
Shape(BasicShape, Option<ReferenceBox>),
@ -126,22 +126,6 @@ impl<B, T> HasViewportPercentage for ShapeSource<B, T> {
fn has_viewport_percentage(&self) -> bool { false }
}
impl<B: ToCss, T: ToCss> ToCss for ShapeSource<B, T> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
ShapeSource::Url(ref url) => url.to_css(dest),
ShapeSource::Shape(ref shape, Some(ref ref_box)) => {
shape.to_css(dest)?;
dest.write_str(" ")?;
ref_box.to_css(dest)
},
ShapeSource::Shape(ref shape, None) => shape.to_css(dest),
ShapeSource::Box(ref val) => val.to_css(dest),
ShapeSource::None => dest.write_str("none"),
}
}
}
impl<L> ToCss for InsetRect<L>
where L: ToCss + PartialEq
{

View file

@ -123,7 +123,7 @@ pub enum GradientItem<Color, LengthOrPercentage> {
/// A color stop.
/// https://drafts.csswg.org/css-images/#typedef-color-stop-list
#[derive(Clone, Copy, HasViewportPercentage, PartialEq, ToComputedValue)]
#[derive(Clone, Copy, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct ColorStop<Color, LengthOrPercentage> {
/// The color of this stop.
@ -321,19 +321,6 @@ impl<C, L> fmt::Debug for ColorStop<C, L>
}
}
impl<C, L> ToCss for ColorStop<C, L>
where C: ToCss, L: ToCss,
{
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.color.to_css(dest)?;
if let Some(ref position) = self.position {
dest.write_str(" ")?;
position.to_css(dest)?;
}
Ok(())
}
}
impl<C> ToCss for ImageRect<C>
where C: ToCss,
{