refactor(counter_style): parse int via parse_non_negative

This commit is contained in:
OJ Kwon 2018-03-20 12:03:14 -07:00
parent 1c2bed5a69
commit c8592ead6f
No known key found for this signature in database
GPG key ID: 6C23A45602A44DA6
2 changed files with 13 additions and 19 deletions

View file

@ -299,7 +299,7 @@ counter_style_descriptors! {
/// <https://drafts.csswg.org/css-counter-styles/#counter-style-pad> /// <https://drafts.csswg.org/css-counter-styles/#counter-style-pad>
"pad" pad / eCSSCounterDesc_Pad: Pad = { "pad" pad / eCSSCounterDesc_Pad: Pad = {
Pad(0, Symbol::String("".to_owned())) Pad(Integer::new(0), Symbol::String("".to_owned()))
} }
/// <https://drafts.csswg.org/css-counter-styles/#counter-style-fallback> /// <https://drafts.csswg.org/css-counter-styles/#counter-style-fallback>
@ -336,14 +336,14 @@ pub enum System {
/// 'fixed <integer>?' /// 'fixed <integer>?'
Fixed { Fixed {
/// '<integer>?' /// '<integer>?'
first_symbol_value: Option<i32> first_symbol_value: Option<Integer>
}, },
/// 'extends <counter-style-name>' /// 'extends <counter-style-name>'
Extends(CustomIdent), Extends(CustomIdent),
} }
impl Parse for System { impl Parse for System {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
try_match_ident_ignore_ascii_case! { input, try_match_ident_ignore_ascii_case! { input,
"cyclic" => Ok(System::Cyclic), "cyclic" => Ok(System::Cyclic),
"numeric" => Ok(System::Numeric), "numeric" => Ok(System::Numeric),
@ -351,7 +351,7 @@ impl Parse for System {
"symbolic" => Ok(System::Symbolic), "symbolic" => Ok(System::Symbolic),
"additive" => Ok(System::Additive), "additive" => Ok(System::Additive),
"fixed" => { "fixed" => {
let first_symbol_value = input.try(|i| i.expect_integer()).ok(); let first_symbol_value = input.try(|i| Integer::parse(context, i)).ok();
Ok(System::Fixed { first_symbol_value: first_symbol_value }) Ok(System::Fixed { first_symbol_value: first_symbol_value })
} }
"extends" => { "extends" => {
@ -515,17 +515,14 @@ where
/// <https://drafts.csswg.org/css-counter-styles/#counter-style-pad> /// <https://drafts.csswg.org/css-counter-styles/#counter-style-pad>
#[derive(Clone, Debug, ToCss)] #[derive(Clone, Debug, ToCss)]
pub struct Pad(pub u32, pub Symbol); pub struct Pad(pub Integer, pub Symbol);
impl Parse for Pad { impl Parse for Pad {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
let pad_with = input.try(|input| Symbol::parse(context, input)); let pad_with = input.try(|input| Symbol::parse(context, input));
let min_length = input.expect_integer()?; let min_length = Integer::parse_non_negative(context, input)?;
if min_length < 0 {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
}
let pad_with = pad_with.or_else(|_| Symbol::parse(context, input))?; let pad_with = pad_with.or_else(|_| Symbol::parse(context, input))?;
Ok(Pad(min_length as u32, pad_with)) Ok(Pad(min_length, pad_with))
} }
} }
@ -580,7 +577,7 @@ impl Parse for AdditiveSymbols {
#[derive(Clone, Debug, ToCss)] #[derive(Clone, Debug, ToCss)]
pub struct AdditiveTuple { pub struct AdditiveTuple {
/// <integer> /// <integer>
pub weight: u32, pub weight: Integer,
/// <symbol> /// <symbol>
pub symbol: Symbol, pub symbol: Symbol,
} }
@ -592,13 +589,10 @@ impl OneOrMoreSeparated for AdditiveTuple {
impl Parse for AdditiveTuple { impl Parse for AdditiveTuple {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
let symbol = input.try(|input| Symbol::parse(context, input)); let symbol = input.try(|input| Symbol::parse(context, input));
let weight = input.expect_integer()?; let weight = Integer::parse_non_negative(context, input)?;
if weight < 0 {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
}
let symbol = symbol.or_else(|_| Symbol::parse(context, input))?; let symbol = symbol.or_else(|_| Symbol::parse(context, input))?;
Ok(AdditiveTuple { Ok(AdditiveTuple {
weight: weight as u32, weight: weight,
symbol: symbol, symbol: symbol,
}) })
} }

View file

@ -281,7 +281,7 @@ impl ToNsCssValue for counter_style::System {
let mut a = nsCSSValue::null(); let mut a = nsCSSValue::null();
let mut b = nsCSSValue::null(); let mut b = nsCSSValue::null();
a.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_FIXED as i32); a.set_enum(structs::NS_STYLE_COUNTER_SYSTEM_FIXED as i32);
b.set_integer(first_symbol_value.unwrap_or(1)); b.set_integer(first_symbol_value.map_or(1, |v| v.value()));
nscssvalue.set_pair(&a, &b); nscssvalue.set_pair(&a, &b);
} }
Extends(other) => { Extends(other) => {
@ -345,7 +345,7 @@ impl ToNsCssValue for counter_style::Pad {
fn convert(self, nscssvalue: &mut nsCSSValue) { fn convert(self, nscssvalue: &mut nsCSSValue) {
let mut min_length = nsCSSValue::null(); let mut min_length = nsCSSValue::null();
let mut pad_with = nsCSSValue::null(); let mut pad_with = nsCSSValue::null();
min_length.set_integer(self.0 as i32); min_length.set_integer(self.0.value());
pad_with.set_from(self.1); pad_with.set_from(self.1);
nscssvalue.set_pair(&min_length, &pad_with); nscssvalue.set_pair(&min_length, &pad_with);
} }
@ -372,7 +372,7 @@ impl ToNsCssValue for counter_style::AdditiveSymbols {
nscssvalue.set_pair_list(self.0.into_iter().map(|tuple| { nscssvalue.set_pair_list(self.0.into_iter().map(|tuple| {
let mut weight = nsCSSValue::null(); let mut weight = nsCSSValue::null();
let mut symbol = nsCSSValue::null(); let mut symbol = nsCSSValue::null();
weight.set_integer(tuple.weight as i32); weight.set_integer(tuple.weight.value());
symbol.set_from(tuple.symbol); symbol.set_from(tuple.symbol);
(weight, symbol) (weight, symbol)
})); }));