mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
style: Improve #[derive(Parse)].
I want to do this so that I can get rid of Either<>. The reasons for getting rid of either are multiple: * It doesn't generate as nice C++ code using cbindgen. * It isn't that nice to use either from Rust. * cbindgen has bugs with zero-sized types. I started using this for ColorOrAuto and a few others, for now. Differential Revision: https://phabricator.services.mozilla.com/D19844
This commit is contained in:
parent
6118e4d993
commit
73d5b82f9f
24 changed files with 238 additions and 362 deletions
|
@ -74,3 +74,26 @@ impl<RGBA> From<RGBA> for Color<RGBA> {
|
|||
Self::rgba(color)
|
||||
}
|
||||
}
|
||||
|
||||
/// Either `<color>` or `auto`.
|
||||
#[derive(
|
||||
Animate,
|
||||
Clone,
|
||||
ComputeSquaredDistance,
|
||||
Copy,
|
||||
Debug,
|
||||
MallocSizeOf,
|
||||
PartialEq,
|
||||
Parse,
|
||||
SpecifiedValueInfo,
|
||||
ToAnimatedValue,
|
||||
ToAnimatedZero,
|
||||
ToComputedValue,
|
||||
ToCss,
|
||||
)]
|
||||
pub enum ColorOrAuto<C> {
|
||||
/// A `<color>
|
||||
Color(C),
|
||||
/// `auto`
|
||||
Auto,
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
Copy,
|
||||
Debug,
|
||||
MallocSizeOf,
|
||||
Parse,
|
||||
PartialEq,
|
||||
SpecifiedValueInfo,
|
||||
ToAnimatedValue,
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
ComputeSquaredDistance,
|
||||
Copy,
|
||||
Debug,
|
||||
Parse,
|
||||
PartialEq,
|
||||
SpecifiedValueInfo,
|
||||
ToAnimatedValue,
|
||||
|
|
|
@ -165,6 +165,7 @@ impl<LengthPercentage> MaxSize<LengthPercentage> {
|
|||
Copy,
|
||||
Debug,
|
||||
MallocSizeOf,
|
||||
Parse,
|
||||
PartialEq,
|
||||
SpecifiedValueInfo,
|
||||
ToAnimatedValue,
|
||||
|
@ -174,29 +175,17 @@ impl<LengthPercentage> MaxSize<LengthPercentage> {
|
|||
)]
|
||||
#[repr(C, u8)]
|
||||
pub enum GenericLengthOrNumber<L, N> {
|
||||
/// A number.
|
||||
///
|
||||
/// NOTE: Numbers need to be before lengths, in order to parse them
|
||||
/// first, since `0` should be a number, not the `0px` length.
|
||||
Number(N),
|
||||
/// A length.
|
||||
Length(L),
|
||||
/// A number.
|
||||
Number(N),
|
||||
}
|
||||
|
||||
pub use self::GenericLengthOrNumber as LengthOrNumber;
|
||||
|
||||
impl<L: Parse, N: Parse> Parse for LengthOrNumber<L, N> {
|
||||
fn parse<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
if let Ok(number) = input.try(|i| N::parse(context, i)) {
|
||||
// Numbers need to be parsed first because `0` must be recognised
|
||||
// as the number `0` and not the length `0px`.
|
||||
return Ok(LengthOrNumber::Number(number));
|
||||
}
|
||||
|
||||
Ok(LengthOrNumber::Length(L::parse(context, input)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, N> LengthOrNumber<L, N> {
|
||||
/// Returns `0`.
|
||||
pub fn zero() -> Self
|
||||
|
|
|
@ -119,27 +119,25 @@ impl Parse for CounterStyleOrNone {
|
|||
if input.try(|i| i.expect_ident_matching("none")).is_ok() {
|
||||
return Ok(CounterStyleOrNone::None);
|
||||
}
|
||||
if input.try(|i| i.expect_function_matching("symbols")).is_ok() {
|
||||
return input.parse_nested_block(|input| {
|
||||
let symbols_type = input
|
||||
.try(|i| SymbolsType::parse(i))
|
||||
.unwrap_or(SymbolsType::Symbolic);
|
||||
let symbols = Symbols::parse(context, input)?;
|
||||
// There must be at least two symbols for alphabetic or
|
||||
// numeric system.
|
||||
if (symbols_type == SymbolsType::Alphabetic ||
|
||||
symbols_type == SymbolsType::Numeric) && symbols.0.len() < 2
|
||||
{
|
||||
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||
}
|
||||
// Identifier is not allowed in symbols() function.
|
||||
if symbols.0.iter().any(|sym| !sym.is_allowed_in_symbols()) {
|
||||
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||
}
|
||||
Ok(CounterStyleOrNone::Symbols(symbols_type, symbols))
|
||||
});
|
||||
}
|
||||
Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
|
||||
input.expect_function_matching("symbols")?;
|
||||
input.parse_nested_block(|input| {
|
||||
let symbols_type = input
|
||||
.try(SymbolsType::parse)
|
||||
.unwrap_or(SymbolsType::Symbolic);
|
||||
let symbols = Symbols::parse(context, input)?;
|
||||
// There must be at least two symbols for alphabetic or
|
||||
// numeric system.
|
||||
if (symbols_type == SymbolsType::Alphabetic ||
|
||||
symbols_type == SymbolsType::Numeric) && symbols.0.len() < 2
|
||||
{
|
||||
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||
}
|
||||
// Identifier is not allowed in symbols() function.
|
||||
if symbols.0.iter().any(|sym| !sym.is_allowed_in_symbols()) {
|
||||
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||
}
|
||||
Ok(CounterStyleOrNone::Symbols(symbols_type, symbols))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -136,6 +136,7 @@ impl<ColorType: Parse, UrlPaintServer: Parse> Parse for SVGPaint<ColorType, UrlP
|
|||
Debug,
|
||||
MallocSizeOf,
|
||||
PartialEq,
|
||||
Parse,
|
||||
SpecifiedValueInfo,
|
||||
ToAnimatedValue,
|
||||
ToAnimatedZero,
|
||||
|
@ -143,28 +144,26 @@ impl<ColorType: Parse, UrlPaintServer: Parse> Parse for SVGPaint<ColorType, UrlP
|
|||
ToCss,
|
||||
)]
|
||||
pub enum SvgLengthPercentageOrNumber<LengthPercentage, Number> {
|
||||
/// <number>
|
||||
///
|
||||
/// Note that this needs to be before, so it gets parsed before the length,
|
||||
/// to handle `0` correctly as a number instead of a `<length>`.
|
||||
Number(Number),
|
||||
/// <length> | <percentage>
|
||||
LengthPercentage(LengthPercentage),
|
||||
/// <number>
|
||||
Number(Number),
|
||||
}
|
||||
|
||||
/// Parsing the SvgLengthPercentageOrNumber. At first, we need to parse number
|
||||
/// since prevent converting to the length.
|
||||
impl<LengthPercentageType: Parse, NumberType: Parse> Parse
|
||||
for SvgLengthPercentageOrNumber<LengthPercentageType, NumberType>
|
||||
{
|
||||
fn parse<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
if let Ok(num) = input.try(|i| NumberType::parse(context, i)) {
|
||||
return Ok(SvgLengthPercentageOrNumber::Number(num));
|
||||
}
|
||||
/// Whether the `context-value` value is enabled.
|
||||
#[cfg(feature = "gecko")]
|
||||
pub fn is_context_value_enabled(_: &ParserContext) -> bool {
|
||||
use crate::gecko_bindings::structs::mozilla;
|
||||
unsafe { mozilla::StaticPrefs_sVarCache_gfx_font_rendering_opentype_svg_enabled }
|
||||
}
|
||||
|
||||
let lp = LengthPercentageType::parse(context, input)?;
|
||||
Ok(SvgLengthPercentageOrNumber::LengthPercentage(lp))
|
||||
}
|
||||
/// Whether the `context-value` value is enabled.
|
||||
#[cfg(not(feature = "gecko"))]
|
||||
pub fn is_context_value_enabled(_: &ParserContext) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// An SVG length value supports `context-value` in addition to length.
|
||||
|
@ -175,6 +174,7 @@ impl<LengthPercentageType: Parse, NumberType: Parse> Parse
|
|||
Debug,
|
||||
MallocSizeOf,
|
||||
PartialEq,
|
||||
Parse,
|
||||
SpecifiedValueInfo,
|
||||
ToAnimatedValue,
|
||||
ToAnimatedZero,
|
||||
|
@ -185,6 +185,7 @@ pub enum SVGLength<LengthType> {
|
|||
/// `<length> | <percentage> | <number>`
|
||||
Length(LengthType),
|
||||
/// `context-value`
|
||||
#[parse(condition = "is_context_value_enabled")]
|
||||
ContextValue,
|
||||
}
|
||||
|
||||
|
@ -216,6 +217,7 @@ pub enum SVGStrokeDashArray<LengthType> {
|
|||
Debug,
|
||||
MallocSizeOf,
|
||||
PartialEq,
|
||||
Parse,
|
||||
SpecifiedValueInfo,
|
||||
ToAnimatedZero,
|
||||
ToComputedValue,
|
||||
|
|
|
@ -4,10 +4,6 @@
|
|||
|
||||
//! Generic types for url properties.
|
||||
|
||||
use crate::parser::{Parse, ParserContext};
|
||||
use cssparser::Parser;
|
||||
use style_traits::ParseError;
|
||||
|
||||
/// An image url or none, used for example in list-style-image
|
||||
#[derive(
|
||||
Animate,
|
||||
|
@ -16,6 +12,7 @@ use style_traits::ParseError;
|
|||
Debug,
|
||||
MallocSizeOf,
|
||||
PartialEq,
|
||||
Parse,
|
||||
SpecifiedValueInfo,
|
||||
ToAnimatedValue,
|
||||
ToAnimatedZero,
|
||||
|
@ -35,19 +32,3 @@ impl<Url> UrlOrNone<Url> {
|
|||
UrlOrNone::None
|
||||
}
|
||||
}
|
||||
|
||||
impl<Url> Parse for UrlOrNone<Url>
|
||||
where
|
||||
Url: Parse,
|
||||
{
|
||||
fn parse<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<UrlOrNone<Url>, ParseError<'i>> {
|
||||
if let Ok(url) = input.try(|input| Url::parse(context, input)) {
|
||||
return Ok(UrlOrNone::Url(url));
|
||||
}
|
||||
input.expect_ident_matching("none")?;
|
||||
Ok(UrlOrNone::None)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue