Make use of Either<A, B> for LengthOrNone

This commit is contained in:
Ravi Shankar 2016-11-03 21:29:47 +05:30
parent 5df250bc05
commit c4fc49c559
10 changed files with 21 additions and 116 deletions

View file

@ -7,7 +7,7 @@ use ordered_float::NotNaN;
use std::fmt;
use style_traits::ToCss;
use super::{Number, ToComputedValue, Context};
use values::{CSSFloat, specified};
use values::{CSSFloat, Either, None_, specified};
pub use cssparser::Color as CSSColor;
pub use super::image::{EndingShape as GradientShape, Gradient, GradientKind, Image};
@ -452,61 +452,7 @@ impl ToCss for LengthOrPercentageOrNone {
}
}
#[derive(PartialEq, Clone, Copy)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum LengthOrNone {
Length(Au),
None,
}
impl fmt::Debug for LengthOrNone {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LengthOrNone::Length(length) => write!(f, "{:?}", length),
LengthOrNone::None => write!(f, "none"),
}
}
}
impl ToComputedValue for specified::LengthOrNone {
type ComputedValue = LengthOrNone;
#[inline]
fn to_computed_value(&self, context: &Context) -> LengthOrNone {
match *self {
specified::LengthOrNone::Length(specified::Length::Calc(calc, range)) => {
LengthOrNone::Length(range.clamp(calc.to_computed_value(context).length()))
}
specified::LengthOrNone::Length(value) => {
LengthOrNone::Length(value.to_computed_value(context))
}
specified::LengthOrNone::None => {
LengthOrNone::None
}
}
}
#[inline]
fn from_computed_value(computed: &LengthOrNone) -> Self {
match *computed {
LengthOrNone::Length(au) => {
specified::LengthOrNone::Length(ToComputedValue::from_computed_value(&au))
}
LengthOrNone::None => {
specified::LengthOrNone::None
}
}
}
}
impl ToCss for LengthOrNone {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
LengthOrNone::Length(length) => length.to_css(dest),
LengthOrNone::None => dest.write_str("none"),
}
}
}
pub type LengthOrNone = Either<Length, None_>;
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]

View file

@ -12,6 +12,7 @@ use super::{CSSFloat, specified};
pub use cssparser::Color as CSSColor;
pub use self::image::{EndingShape as GradientShape, Gradient, GradientKind, Image};
pub use self::image::{LengthOrKeyword, LengthOrPercentageOrKeyword};
pub use super::{Either, None_};
pub use super::specified::{Angle, BorderStyle, Time, UrlExtraData, UrlOrNone};
pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNumber, LengthOrPercentage, LengthOrPercentageOrAuto};
pub use self::length::{LengthOrPercentageOrAutoOrContent, LengthOrPercentageOrNone, LengthOrNone};

View file

@ -13,7 +13,7 @@ use std::ops::Mul;
use style_traits::ToCss;
use style_traits::values::specified::AllowedNumericType;
use super::{Angle, Number, SimplifiedValueNode, SimplifiedSumNode, Time};
use values::{CSSFloat, FONT_MEDIUM_PX, HasViewportPercentage, computed};
use values::{CSSFloat, Either, FONT_MEDIUM_PX, HasViewportPercentage, None_, computed};
pub use super::image::{AngleOrCorner, ColorStop, EndingShape as GradientEndingShape, Gradient};
pub use super::image::{GradientKind, HorizontalDirection, Image, LengthOrKeyword, LengthOrPercentageOrKeyword};
@ -908,55 +908,12 @@ impl LengthOrPercentageOrNone {
}
}
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum LengthOrNone {
Length(Length),
None,
}
pub type LengthOrNone = Either<Length, None_>;
impl HasViewportPercentage for LengthOrNone {
fn has_viewport_percentage(&self) -> bool {
match *self {
LengthOrNone::Length(ref length) => length.has_viewport_percentage(),
_ => false
}
}
}
impl ToCss for LengthOrNone {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
LengthOrNone::Length(length) => length.to_css(dest),
LengthOrNone::None => dest.write_str("none"),
}
}
}
impl LengthOrNone {
fn parse_internal(input: &mut Parser, context: AllowedNumericType)
-> Result<LengthOrNone, ()>
{
match try!(input.next()) {
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
Length::parse_dimension(value.value, unit).map(LengthOrNone::Length),
Token::Number(ref value) if value.value == 0. =>
Ok(LengthOrNone::Length(Length::Absolute(Au(0)))),
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") =>
input.parse_nested_block(|input| {
CalcLengthOrPercentage::parse_length(input, context)
}).map(LengthOrNone::Length),
Token::Ident(ref value) if value.eq_ignore_ascii_case("none") =>
Ok(LengthOrNone::None),
_ => Err(())
}
}
#[inline]
pub fn parse(input: &mut Parser) -> Result<LengthOrNone, ()> {
LengthOrNone::parse_internal(input, AllowedNumericType::All)
}
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrNone, ()> {
LengthOrNone::parse_internal(input, AllowedNumericType::NonNegative)
Length::parse_internal(input, AllowedNumericType::NonNegative).map(Either::First)
}
}

View file

@ -83,7 +83,6 @@ impl ToCss for CSSRGBA {
}
}
#[derive(Clone, Debug)]
pub struct SimplifiedSumNode {
values: Vec<SimplifiedValueNode>,