From 3255bb809ec573011981ef167af8b1f2a4e1b41f Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Sun, 1 May 2016 18:56:28 +1000 Subject: [PATCH] Add a predefined Number type. --- components/style/values.rs | 38 +++++++++++++++++++++++++++++++ ports/geckolib/properties.mako.rs | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/components/style/values.rs b/components/style/values.rs index 9b33b18fee7..77a521469ad 100644 --- a/components/style/values.rs +++ b/components/style/values.rs @@ -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 { + parse_number(input).map(Number) + } + + fn parse_with_minimum(input: &mut Parser, min: CSSFloat) -> Result { + match parse_number(input) { + Ok(value) if value < min => Err(()), + value => value.map(Number), + } + } + + pub fn parse_non_negative(input: &mut Parser) -> Result { + Number::parse_with_minimum(input, 0.0) + } + + pub fn parse_at_least_one(input: &mut Parser) -> Result { + Number::parse_with_minimum(input, 1.0) + } + } + + impl ToComputedValue for Number { + type ComputedValue = CSSFloat; + + #[inline] + fn to_computed_value(&self, _: &Cx) -> CSSFloat { self.0 } + } + + impl ToCss for Number { + fn to_css(&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; } diff --git a/ports/geckolib/properties.mako.rs b/ports/geckolib/properties.mako.rs index 839e072ee2c..47d4b5c7d72 100644 --- a/ports/geckolib/properties.mako.rs +++ b/ports/geckolib/properties.mako.rs @@ -295,7 +295,7 @@ impl Debug for ${style_struct.gecko_ffi_name} { # These are booleans. force_stub += ["page-break-after", "page-break-before"] - simple_types = ["Opacity"] + simple_types = ["Number", "Opacity"] keyword_longhands = [x for x in longhands if x.keyword and not x.name in force_stub] simple_longhands = [x for x in longhands