Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive more Parse implementations (fixes #19827) #19903

Merged
merged 7 commits into from Feb 1, 2018

Don't use define_css_keyword_enum in style anymore

  • Loading branch information
nox committed Feb 1, 2018
commit 3d99a4489c21df9a7c227f8d1de69e99921b19af
@@ -67,13 +67,17 @@ impl ToCss for UrlSource {
/// A font-display value for a @font-face rule.
/// The font-display descriptor determines how a font face is displayed based
/// on whether and when it is downloaded and ready to use.
define_css_keyword_enum!(FontDisplay:
"auto" => Auto,
"block" => Block,
"swap" => Swap,
"fallback" => Fallback,
"optional" => Optional);
add_impls_for_keyword_enum!(FontDisplay);
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum FontDisplay {
Auto,
Block,
Swap,
Fallback,
Optional,
}

/// A font-weight value for a @font-face rule.
/// The font-weight CSS property specifies the weight or boldness of the font.
@@ -25,8 +25,6 @@

#![deny(missing_docs)]

#![recursion_limit = "500"] // For define_css_keyword_enum! in -moz-appearance

extern crate app_units;
extern crate arrayvec;
extern crate atomic_refcell;
@@ -72,7 +70,6 @@ extern crate smallbitvec;
extern crate smallvec;
#[macro_use]
extern crate style_derive;
#[macro_use]
extern crate style_traits;
extern crate time;
extern crate uluru;
@@ -65,28 +65,6 @@ macro_rules! try_match_ident_ignore_ascii_case {
}}
}

/// A macro for implementing `ToComputedValue`, and `Parse` traits for
/// the enums defined using `define_css_keyword_enum` macro.
///
/// NOTE: We should either move `Parse` trait to `style_traits`
/// or `define_css_keyword_enum` macro to this crate, but that
/// may involve significant cleanup in both the crates.
macro_rules! add_impls_for_keyword_enum {
($name:ident) => {
impl $crate::parser::Parse for $name {
#[inline]
fn parse<'i, 't>(
_context: &$crate::parser::ParserContext,
input: &mut ::cssparser::Parser<'i, 't>,
) -> Result<Self, ::style_traits::ParseError<'i>> {
$name::parse(input)
}
}

trivial_to_computed_value!($name);
};
}

macro_rules! define_keyword_type {
($name: ident, $css: expr) => {
#[allow(missing_docs)]
@@ -424,20 +424,13 @@
use properties::longhands::system_font::SystemFont;

pub mod computed_value {
use cssparser::Parser;
use parser::{Parse, ParserContext};

use style_traits::ParseError;
define_css_keyword_enum! { T:
% for value in keyword.values_for(product):
"${value}" => ${to_camel_case(value)},
% endfor
}

impl Parse for T {
fn parse<'i, 't>(_: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
T::parse(input)
}
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, Parse)]
#[derive(PartialEq, ToCss)]
pub enum T {
% for value in keyword.values_for(product):
${to_camel_case(value)},
% endfor
}

${gecko_keyword_conversion(keyword, keyword.values_for(product), type="T", cast_to="i32")}
@@ -604,26 +597,32 @@

<%def name="inner_body(keyword, extra_specified=None, needs_conversion=False)">
% if extra_specified or keyword.aliases_for(product):
define_css_keyword_enum! { SpecifiedValue:
values {
% for value in keyword.values_for(product) + (extra_specified or "").split():
"${value}" => ${to_camel_case(value)},
% endfor
}
aliases {
% for alias, value in keyword.aliases_for(product).iteritems():
"${alias}" => ${to_camel_case(value)},
% endfor
}
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum SpecifiedValue {
% for value in keyword.values_for(product) + (extra_specified or "").split():
<%
aliases = []
for alias, v in keyword.aliases_for(product).iteritems():
if value == v:
aliases.append(alias)
%>
% if aliases:
#[css(aliases = "${','.join(aliases)}")]
% endif
${to_camel_case(value)},
% endfor
}
% else:
pub use self::computed_value::T as SpecifiedValue;
% endif
pub mod computed_value {
define_css_keyword_enum! { T:
% for value in data.longhands_by_name[name].keyword.values_for(product):
"${value}" => ${to_camel_case(value)},
% endfor
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum T {
% for value in data.longhands_by_name[name].keyword.values_for(product):
${to_camel_case(value)},
% endfor
}
}
#[inline]
@@ -639,13 +638,6 @@
-> Result<SpecifiedValue, ParseError<'i>> {
SpecifiedValue::parse(input)
}
impl Parse for SpecifiedValue {
#[inline]
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<SpecifiedValue, ParseError<'i>> {
SpecifiedValue::parse(input)
}
}

% if needs_conversion:
<%
@@ -115,11 +115,14 @@ ${helpers.predefined_type("border-image-outset", "LengthOrNumberRect",
pub struct SpecifiedValue(pub RepeatKeyword,
pub Option<RepeatKeyword>);

define_css_keyword_enum!(RepeatKeyword:
"stretch" => Stretch,
"repeat" => Repeat,
"round" => Round,
"space" => Space);
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum RepeatKeyword {
Stretch,
Repeat,
Round,
Space,
}

#[inline]
pub fn get_initial_value() -> computed_value::T {
@@ -279,12 +279,14 @@ ${helpers.predefined_type(
}
}

define_css_keyword_enum!(ShapeKeyword:
"dot" => Dot,
"circle" => Circle,
"double-circle" => DoubleCircle,
"triangle" => Triangle,
"sesame" => Sesame);
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq, ToCss)]
pub enum ShapeKeyword {
Dot,
Circle,
DoubleCircle,
Triangle,
Sesame,
}

impl ShapeKeyword {
pub fn char(&self, fill: bool) -> &str {
@@ -381,14 +383,19 @@ ${helpers.predefined_type(

<%helpers:longhand name="text-emphasis-position" animation_value_type="discrete" products="gecko"
spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-position">
define_css_keyword_enum!(HorizontalWritingModeValue:
"over" => Over,
"under" => Under);
add_impls_for_keyword_enum!(VerticalWritingModeValue);
define_css_keyword_enum!(VerticalWritingModeValue:
"right" => Right,
"left" => Left);
add_impls_for_keyword_enum!(HorizontalWritingModeValue);
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum HorizontalWritingModeValue {
Over,
Under,
}

#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum VerticalWritingModeValue {
Right,
Left,
}

#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss)]
pub struct SpecifiedValue(pub HorizontalWritingModeValue, pub VerticalWritingModeValue);
@@ -30,13 +30,16 @@ pub enum GeometryBox {
pub type FloatAreaShape<BasicShape, Image> = ShapeSource<BasicShape, ShapeBox, Image>;

// https://drafts.csswg.org/css-shapes-1/#typedef-shape-box
define_css_keyword_enum!(ShapeBox:
"margin-box" => MarginBox,
"border-box" => BorderBox,
"padding-box" => PaddingBox,
"content-box" => ContentBox
);
add_impls_for_keyword_enum!(ShapeBox);
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum ShapeBox {
MarginBox,
BorderBox,
PaddingBox,
ContentBox,
}

/// A shape source, for some reference box.
#[allow(missing_docs)]
@@ -117,11 +120,14 @@ pub struct Polygon<LengthOrPercentage> {
// NOTE: Basic shapes spec says that these are the only two values, however
// https://www.w3.org/TR/SVG/painting.html#FillRuleProperty
// says that it can also be `inherit`
define_css_keyword_enum!(FillRule:
"nonzero" => Nonzero,
"evenodd" => Evenodd
);
add_impls_for_keyword_enum!(FillRule);
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum FillRule {
Nonzero,
Evenodd,
}

// FIXME(nox): Implement ComputeSquaredDistance for T types and stop
// using PartialEq here, this will let us derive this impl.
@@ -140,12 +140,15 @@ impl Parse for GridLine<specified::Integer> {
}
}

define_css_keyword_enum!{ TrackKeyword:
"auto" => Auto,
"max-content" => MaxContent,
"min-content" => MinContent
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum TrackKeyword {
Auto,
MaxContent,
MinContent,
}
add_impls_for_keyword_enum!(TrackKeyword);

/// A track breadth for explicit grid track sizing. It's generic solely to
/// avoid re-implementing it for the computed type.
@@ -97,15 +97,18 @@ pub enum Ellipse<LengthOrPercentage> {
}

/// <https://drafts.csswg.org/css-images/#typedef-extent-keyword>
define_css_keyword_enum!(ShapeExtent:
"closest-side" => ClosestSide,
"farthest-side" => FarthestSide,
"closest-corner" => ClosestCorner,
"farthest-corner" => FarthestCorner,
"contain" => Contain,
"cover" => Cover
);
add_impls_for_keyword_enum!(ShapeExtent);
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum ShapeExtent {
ClosestSide,
FarthestSide,
ClosestCorner,
FarthestCorner,
Contain,
Cover,
}

/// A gradient item.
/// <https://drafts.csswg.org/css-images-4/#color-stop-syntax>
@@ -32,14 +32,17 @@ pub mod text;
pub mod transform;

// https://drafts.csswg.org/css-counter-styles/#typedef-symbols-type
define_css_keyword_enum! { SymbolsType:
"cyclic" => Cyclic,
"numeric" => Numeric,
"alphabetic" => Alphabetic,
"symbolic" => Symbolic,
"fixed" => Fixed,
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum SymbolsType {
Cyclic,
Numeric,
Alphabetic,
Symbolic,
Fixed,
}
add_impls_for_keyword_enum!(SymbolsType);

#[cfg(feature = "gecko")]
impl SymbolsType {
@@ -98,20 +98,26 @@ pub enum TimingFunction<Integer, Number> {
Frames(Integer),
}

define_css_keyword_enum! { TimingKeyword:
"linear" => Linear,
"ease" => Ease,
"ease-in" => EaseIn,
"ease-out" => EaseOut,
"ease-in-out" => EaseInOut,
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum TimingKeyword {
Linear,
Ease,
EaseIn,
EaseOut,
EaseInOut,
}
add_impls_for_keyword_enum!(TimingKeyword);

define_css_keyword_enum! { StepPosition:
"start" => Start,
"end" => End,
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, Parse, PartialEq)]
#[derive(ToComputedValue, ToCss)]
pub enum StepPosition {
Start,
End,
}
add_impls_for_keyword_enum!(StepPosition);

impl<H, V, D> TransformOrigin<H, V, D> {
/// Returns a new transform origin.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.