style: Implement css(dimension) and derive ToCss for a bunch of stuff.

For css(dimension), it'd be nice to derive(Parse) too, I think...
This commit is contained in:
Emilio Cobos Álvarez 2017-11-11 14:57:12 +01:00
parent 1b533f9bdc
commit 4927786d90
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
10 changed files with 98 additions and 153 deletions

View file

@ -116,9 +116,9 @@ impl From<nsStyleCoord_CalcValue> for LengthOrPercentageOrAuto {
impl From<Angle> for CoordDataValue {
fn from(reference: Angle) -> Self {
match reference {
Angle::Degree(val) => CoordDataValue::Degree(val),
Angle::Gradian(val) => CoordDataValue::Grad(val),
Angle::Radian(val) => CoordDataValue::Radian(val),
Angle::Deg(val) => CoordDataValue::Degree(val),
Angle::Grad(val) => CoordDataValue::Grad(val),
Angle::Rad(val) => CoordDataValue::Radian(val),
Angle::Turn(val) => CoordDataValue::Turn(val),
}
}
@ -128,9 +128,9 @@ impl Angle {
/// Converts Angle struct into (value, unit) pair.
pub fn to_gecko_values(&self) -> (f32, nsCSSUnit) {
match *self {
Angle::Degree(val) => (val, nsCSSUnit::eCSSUnit_Degree),
Angle::Gradian(val) => (val, nsCSSUnit::eCSSUnit_Grad),
Angle::Radian(val) => (val, nsCSSUnit::eCSSUnit_Radian),
Angle::Deg(val) => (val, nsCSSUnit::eCSSUnit_Degree),
Angle::Grad(val) => (val, nsCSSUnit::eCSSUnit_Grad),
Angle::Rad(val) => (val, nsCSSUnit::eCSSUnit_Radian),
Angle::Turn(val) => (val, nsCSSUnit::eCSSUnit_Turn),
}
}
@ -138,9 +138,9 @@ impl Angle {
/// Converts gecko (value, unit) pair into Angle struct
pub fn from_gecko_values(value: f32, unit: nsCSSUnit) -> Angle {
match unit {
nsCSSUnit::eCSSUnit_Degree => Angle::Degree(value),
nsCSSUnit::eCSSUnit_Grad => Angle::Gradian(value),
nsCSSUnit::eCSSUnit_Radian => Angle::Radian(value),
nsCSSUnit::eCSSUnit_Degree => Angle::Deg(value),
nsCSSUnit::eCSSUnit_Grad => Angle::Grad(value),
nsCSSUnit::eCSSUnit_Radian => Angle::Rad(value),
nsCSSUnit::eCSSUnit_Turn => Angle::Turn(value),
_ => panic!("Unexpected unit {:?} for angle", unit),
}

View file

@ -29,7 +29,7 @@ use style_traits::{CSSPixel, DevicePixel};
use style_traits::{ToCss, ParseError, StyleParseErrorKind};
use style_traits::viewport::ViewportConstraints;
use stylesheets::Origin;
use values::{CSSFloat, CustomIdent, serialize_dimension};
use values::{CSSFloat, CustomIdent};
use values::computed::{self, ToComputedValue};
use values::computed::font::FontSize;
use values::specified::Length;
@ -262,7 +262,8 @@ impl PartialEq for Expression {
}
/// A resolution.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, ToCss)]
#[css(dimension)]
pub enum Resolution {
/// Dots per inch.
Dpi(CSSFloat),
@ -303,18 +304,6 @@ impl Resolution {
}
}
impl ToCss for Resolution {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write,
{
match *self {
Resolution::Dpi(v) => serialize_dimension(v, "dpi", dest),
Resolution::Dppx(v) => serialize_dimension(v, "dppx", dest),
Resolution::Dpcm(v) => serialize_dimension(v, "dpcm", dest),
}
}
}
/// A value found or expected in a media expression.
#[derive(Clone, Debug, PartialEq)]
pub enum MediaExpressionValue {

View file

@ -291,9 +291,9 @@ impl GeckoStyleCoordConvertible for Angle {
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
match coord.as_value() {
CoordDataValue::Degree(val) => Some(Angle::Degree(val)),
CoordDataValue::Grad(val) => Some(Angle::Gradian(val)),
CoordDataValue::Radian(val) => Some(Angle::Radian(val)),
CoordDataValue::Degree(val) => Some(Angle::Deg(val)),
CoordDataValue::Grad(val) => Some(Angle::Grad(val)),
CoordDataValue::Radian(val) => Some(Angle::Rad(val)),
CoordDataValue::Turn(val) => Some(Angle::Turn(val)),
_ => None,
}