Add a predefined Number type.

This commit is contained in:
Cameron McCormack 2016-05-01 18:56:28 +10:00
parent d71e5c8b3d
commit 3255bb809e
2 changed files with 39 additions and 1 deletions

View file

@ -1476,6 +1476,43 @@ pub mod specified {
}
}
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, HeapSizeOf)]
pub struct Number(pub CSSFloat);
impl Number {
pub fn parse(input: &mut Parser) -> Result<Number, ()> {
parse_number(input).map(Number)
}
fn parse_with_minimum(input: &mut Parser, min: CSSFloat) -> Result<Number, ()> {
match parse_number(input) {
Ok(value) if value < min => Err(()),
value => value.map(Number),
}
}
pub fn parse_non_negative(input: &mut Parser) -> Result<Number, ()> {
Number::parse_with_minimum(input, 0.0)
}
pub fn parse_at_least_one(input: &mut Parser) -> Result<Number, ()> {
Number::parse_with_minimum(input, 1.0)
}
}
impl ToComputedValue for Number {
type ComputedValue = CSSFloat;
#[inline]
fn to_computed_value<Cx: TContext>(&self, _: &Cx) -> CSSFloat { self.0 }
}
impl ToCss for Number {
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, HeapSizeOf)]
pub struct Opacity(pub CSSFloat);
@ -2117,5 +2154,6 @@ pub mod computed {
}
}
pub type Length = Au;
pub type Number = CSSFloat;
pub type Opacity = CSSFloat;
}