Skip to content

Commit

Permalink
style: Switch all callsites of try() to try_parse() in the style crate.
Browse files Browse the repository at this point in the history
Fully automated via:

  $ rg -l '\.try\(' | xargs sed -i 's/\.try(/.try_parse(/g'
  $ cd servo/components/style && cargo +nightly fmt

Differential Revision: https://phabricator.services.mozilla.com/D80099
  • Loading branch information
emilio committed Jun 18, 2020
1 parent 5af0d7c commit 685e749
Show file tree
Hide file tree
Showing 56 changed files with 469 additions and 403 deletions.
16 changes: 8 additions & 8 deletions components/style/counter_style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl Parse for System {
"symbolic" => Ok(System::Symbolic),
"additive" => Ok(System::Additive),
"fixed" => {
let first_symbol_value = input.try(|i| Integer::parse(context, i)).ok();
let first_symbol_value = input.try_parse(|i| Integer::parse(context, i)).ok();
Ok(System::Fixed { first_symbol_value })
},
"extends" => {
Expand Down Expand Up @@ -458,7 +458,7 @@ impl Parse for Negative {
) -> Result<Self, ParseError<'i>> {
Ok(Negative(
Symbol::parse(context, input)?,
input.try(|input| Symbol::parse(context, input)).ok(),
input.try_parse(|input| Symbol::parse(context, input)).ok(),
))
}
}
Expand Down Expand Up @@ -494,7 +494,7 @@ impl Parse for CounterRanges {
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if input
.try(|input| input.expect_ident_matching("auto"))
.try_parse(|input| input.expect_ident_matching("auto"))
.is_ok()
{
return Ok(CounterRanges(Default::default()));
Expand All @@ -519,7 +519,7 @@ fn parse_bound<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<CounterBound, ParseError<'i>> {
if let Ok(integer) = input.try(|input| Integer::parse(context, input)) {
if let Ok(integer) = input.try_parse(|input| Integer::parse(context, input)) {
return Ok(CounterBound::Integer(integer));
}
input.expect_ident_matching("infinite")?;
Expand All @@ -535,7 +535,7 @@ impl Parse for Pad {
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_parse(|input| Symbol::parse(context, input));
let min_length = Integer::parse_non_negative(context, input)?;
let pad_with = pad_with.or_else(|_| Symbol::parse(context, input))?;
Ok(Pad(min_length, pad_with))
Expand Down Expand Up @@ -568,7 +568,7 @@ impl Parse for Symbols {
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let mut symbols = Vec::new();
while let Ok(s) = input.try(|input| Symbol::parse(context, input)) {
while let Ok(s) = input.try_parse(|input| Symbol::parse(context, input)) {
symbols.push(s);
}
if symbols.is_empty() {
Expand Down Expand Up @@ -618,7 +618,7 @@ impl Parse for AdditiveTuple {
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let symbol = input.try(|input| Symbol::parse(context, input));
let symbol = input.try_parse(|input| Symbol::parse(context, input));
let weight = Integer::parse_non_negative(context, input)?;
let symbol = symbol.or_else(|_| Symbol::parse(context, input))?;
Ok(Self { weight, symbol })
Expand Down Expand Up @@ -648,7 +648,7 @@ impl Parse for SpeakAs {
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let mut is_spell_out = false;
let result = input.try(|input| {
let result = input.try_parse(|input| {
let ident = input.expect_ident().map_err(|_| ())?;
match_ignore_ascii_case! { &*ident,
"auto" => Ok(SpeakAs::Auto),
Expand Down
4 changes: 2 additions & 2 deletions components/style/custom_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ fn parse_var_function<'i, 't>(
let name = parse_name(&name).map_err(|()| {
input.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(name.clone()))
})?;
if input.try(|input| input.expect_comma()).is_ok() {
if input.try_parse(|input| input.expect_comma()).is_ok() {
parse_fallback(input)?;
}
if let Some(refs) = references {
Expand All @@ -505,7 +505,7 @@ fn parse_env_function<'i, 't>(
// TODO(emilio): This should be <custom-ident> per spec, but no other
// browser does that, see https://github.com/w3c/csswg-drafts/issues/3262.
input.expect_ident()?;
if input.try(|input| input.expect_comma()).is_ok() {
if input.try_parse(|input| input.expect_comma()).is_ok() {
parse_fallback(input)?;
}
if let Some(references) = references {
Expand Down
8 changes: 4 additions & 4 deletions components/style/font_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ macro_rules! impl_range {
) -> Result<Self, ParseError<'i>> {
let first = $component::parse(context, input)?;
let second = input
.try(|input| $component::parse(context, input))
.try_parse(|input| $component::parse(context, input))
.unwrap_or_else(|_| first.clone());
Ok($range(first, second))
}
Expand Down Expand Up @@ -236,7 +236,7 @@ impl Parse for FontStyle {
GenericFontStyle::Italic => FontStyle::Italic,
GenericFontStyle::Oblique(angle) => {
let second_angle = input
.try(|input| SpecifiedFontStyle::parse_angle(context, input))
.try_parse(|input| SpecifiedFontStyle::parse_angle(context, input))
.unwrap_or_else(|_| angle.clone());

FontStyle::Oblique(angle, second_angle)
Expand Down Expand Up @@ -383,7 +383,7 @@ impl Parse for Source {
input: &mut Parser<'i, 't>,
) -> Result<Source, ParseError<'i>> {
if input
.try(|input| input.expect_function_matching("local"))
.try_parse(|input| input.expect_function_matching("local"))
.is_ok()
{
return input
Expand All @@ -395,7 +395,7 @@ impl Parse for Source {

// Parsing optional format()
let format_hints = if input
.try(|input| input.expect_function_matching("format"))
.try_parse(|input| input.expect_function_matching("format"))
.is_ok()
{
input.parse_nested_block(|input| {
Expand Down
6 changes: 3 additions & 3 deletions components/style/media_queries/media_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl MediaCondition {

// ParenthesisBlock.
let first_condition = Self::parse_paren_block(context, input)?;
let operator = match input.try(Operator::parse) {
let operator = match input.try_parse(Operator::parse) {
Ok(op) => op,
Err(..) => return Ok(first_condition),
};
Expand All @@ -133,7 +133,7 @@ impl MediaCondition {
};

loop {
if input.try(|i| i.expect_ident_matching(delim)).is_err() {
if input.try_parse(|i| i.expect_ident_matching(delim)).is_err() {
return Ok(MediaCondition::Operation(
conditions.into_boxed_slice(),
operator,
Expand All @@ -159,7 +159,7 @@ impl MediaCondition {
) -> Result<Self, ParseError<'i>> {
input.parse_nested_block(|input| {
// Base case.
if let Ok(inner) = input.try(|i| Self::parse(context, i)) {
if let Ok(inner) = input.try_parse(|i| Self::parse(context, i)) {
return Ok(MediaCondition::InParens(Box::new(inner)));
}
let expr = MediaFeatureExpression::parse_in_parenthesis_block(context, input)?;
Expand Down
6 changes: 3 additions & 3 deletions components/style/media_queries/media_feature_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ fn consume_operation_or_colon(input: &mut Parser) -> Result<Option<Operator>, ()
Ok(Some(match first_delim {
'=' => Operator::Equal,
'>' => {
if input.try(|i| i.expect_delim('=')).is_ok() {
if input.try_parse(|i| i.expect_delim('=')).is_ok() {
Operator::GreaterThanEqual
} else {
Operator::GreaterThan
}
},
'<' => {
if input.try(|i| i.expect_delim('=')).is_ok() {
if input.try_parse(|i| i.expect_delim('=')).is_ok() {
Operator::LessThanEqual
} else {
Operator::LessThan
Expand Down Expand Up @@ -314,7 +314,7 @@ impl MediaFeatureExpression {
));
}

let operator = input.try(consume_operation_or_colon);
let operator = input.try_parse(consume_operation_or_colon);
let operator = match operator {
Err(..) => {
// If there's no colon, this is a media query of the
Expand Down
6 changes: 3 additions & 3 deletions components/style/media_queries/media_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ impl MediaQuery {
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let (qualifier, explicit_media_type) = input
.try(|input| -> Result<_, ()> {
let qualifier = input.try(Qualifier::parse).ok();
.try_parse(|input| -> Result<_, ()> {
let qualifier = input.try_parse(Qualifier::parse).ok();
let ident = input.expect_ident().map_err(|_| ())?;
let media_type = MediaQueryType::parse(&ident)?;
Ok((qualifier, Some(media_type)))
Expand All @@ -135,7 +135,7 @@ impl MediaQuery {

let condition = if explicit_media_type.is_none() {
Some(MediaCondition::parse(context, input)?)
} else if input.try(|i| i.expect_ident_matching("and")).is_ok() {
} else if input.try_parse(|i| i.expect_ident_matching("and")).is_ok() {
Some(MediaCondition::parse_disallow_or(context, input)?)
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion components/style/properties/declaration_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ impl<'a, 'b, 'i> DeclarationParser<'i> for PropertyDeclarationParser<'a, 'b> {
input.parse_until_before(Delimiter::Bang, |input| {
PropertyDeclaration::parse_into(self.declarations, id, self.context, input)
})?;
let importance = match input.try(parse_important) {
let importance = match input.try_parse(parse_important) {
Ok(()) => Importance::Important,
Err(_) => Importance::Normal,
};
Expand Down
4 changes: 2 additions & 2 deletions components/style/properties/helpers.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
use style_traits::Separator;

% if allow_empty:
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
if input.try_parse(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(SpecifiedValue(Default::default()))
}
% endif
Expand Down Expand Up @@ -994,7 +994,7 @@

let first = parse_one(context, input)?;
let second =
input.try(|input| parse_one(context, input)).unwrap_or_else(|_| first.clone());
input.try_parse(|input| parse_one(context, input)).unwrap_or_else(|_| first.clone());
Ok(expanded! {
${to_rust_ident(first_property)}: first,
${to_rust_ident(second_property)}: second,
Expand Down
8 changes: 4 additions & 4 deletions components/style/properties/properties.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ impl UnparsedValue {
let mut input = ParserInput::new(&css);
let mut input = Parser::new(&mut input);
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
if let Ok(keyword) = input.try(CSSWideKeyword::parse) {
if let Ok(keyword) = input.try_parse(CSSWideKeyword::parse) {
return PropertyDeclaration::css_wide_keyword(longhand_id, keyword);
}

Expand Down Expand Up @@ -2380,7 +2380,7 @@ impl PropertyDeclaration {
// FIXME: fully implement https://github.com/w3c/csswg-drafts/issues/774
// before adding skip_whitespace here.
// This probably affects some test results.
let value = match input.try(CSSWideKeyword::parse) {
let value = match input.try_parse(CSSWideKeyword::parse) {
Ok(keyword) => CustomDeclarationValue::CSSWideKeyword(keyword),
Err(()) => CustomDeclarationValue::Value(
crate::custom_properties::SpecifiedValue::parse(input)?
Expand All @@ -2395,7 +2395,7 @@ impl PropertyDeclaration {
PropertyId::LonghandAlias(id, _) |
PropertyId::Longhand(id) => {
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
input.try(CSSWideKeyword::parse).map(|keyword| {
input.try_parse(CSSWideKeyword::parse).map(|keyword| {
PropertyDeclaration::css_wide_keyword(id, keyword)
}).or_else(|()| {
input.look_for_var_or_env_functions();
Expand Down Expand Up @@ -2425,7 +2425,7 @@ impl PropertyDeclaration {
PropertyId::ShorthandAlias(id, _) |
PropertyId::Shorthand(id) => {
input.skip_whitespace(); // Unnecessary for correctness, but may help try() rewind less.
if let Ok(keyword) = input.try(CSSWideKeyword::parse) {
if let Ok(keyword) = input.try_parse(CSSWideKeyword::parse) {
if id == ShorthandId::All {
declarations.all_shorthand = AllShorthand::CSSWideKeyword(keyword)
} else {
Expand Down
8 changes: 4 additions & 4 deletions components/style/properties/shorthands/background.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@
% endfor
loop {
if background_color.is_none() {
if let Ok(value) = input.try(|i| Color::parse(context, i)) {
if let Ok(value) = input.try_parse(|i| Color::parse(context, i)) {
background_color = Some(value);
continue
}
}
if position.is_none() {
if let Ok(value) = input.try(|input| {
if let Ok(value) = input.try_parse(|input| {
Position::parse_three_value_quirky(context, input, AllowQuirks::No)
}) {
position = Some(value);

// Parse background size, if applicable.
size = input.try(|input| {
size = input.try_parse(|input| {
input.expect_delim('/')?;
background_size::single_value::parse(context, input)
}).ok();
Expand All @@ -81,7 +81,7 @@
}
% for name in "image repeat attachment origin clip".split():
if ${name}.is_none() {
if let Ok(value) = input.try(|input| background_${name}::single_value
if let Ok(value) = input.try_parse(|input| background_${name}::single_value
::parse(context, input)) {
${name} = Some(value);
continue
Expand Down
20 changes: 10 additions & 10 deletions components/style/properties/shorthands/border.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ pub fn parse_border<'i, 't>(
let mut any = false;
loop {
if width.is_none() {
if let Ok(value) = input.try(|i| BorderSideWidth::parse(context, i)) {
if let Ok(value) = input.try_parse(|i| BorderSideWidth::parse(context, i)) {
width = Some(value);
any = true;
}
}
if style.is_none() {
if let Ok(value) = input.try(BorderStyle::parse) {
if let Ok(value) = input.try_parse(BorderStyle::parse) {
style = Some(value);
any = true;
continue
}
}
if color.is_none() {
if let Ok(value) = input.try(|i| Color::parse(context, i)) {
if let Ok(value) = input.try_parse(|i| Color::parse(context, i)) {
color = Some(value);
any = true;
continue
Expand Down Expand Up @@ -301,24 +301,24 @@ pub fn parse_border<'i, 't>(
let mut border_image_${name} = border_image_${name}::get_initial_specified_value();
% endfor

let result: Result<_, ParseError> = input.try(|input| {
let result: Result<_, ParseError> = input.try_parse(|input| {
% for name in "outset repeat slice source width".split():
let mut ${name} = None;
% endfor
loop {
if slice.is_none() {
if let Ok(value) = input.try(|input| border_image_slice::parse(context, input)) {
if let Ok(value) = input.try_parse(|input| border_image_slice::parse(context, input)) {
slice = Some(value);
// Parse border image width and outset, if applicable.
let maybe_width_outset: Result<_, ParseError> = input.try(|input| {
let maybe_width_outset: Result<_, ParseError> = input.try_parse(|input| {
input.expect_delim('/')?;

// Parse border image width, if applicable.
let w = input.try(|input|
let w = input.try_parse(|input|
border_image_width::parse(context, input)).ok();

// Parse border image outset if applicable.
let o = input.try(|input| {
let o = input.try_parse(|input| {
input.expect_delim('/')?;
border_image_outset::parse(context, input)
}).ok();
Expand All @@ -339,7 +339,7 @@ pub fn parse_border<'i, 't>(
}
% for name in "source repeat".split():
if ${name}.is_none() {
if let Ok(value) = input.try(|input| border_image_${name}::parse(context, input)) {
if let Ok(value) = input.try_parse(|input| border_image_${name}::parse(context, input)) {
${name} = Some(value);
continue
}
Expand Down Expand Up @@ -407,7 +407,7 @@ pub fn parse_border<'i, 't>(
) -> Result<Longhands, ParseError<'i>> {
let start_value = border_${axis}_start_${prop}::parse(context, input)?;
let end_value =
input.try(|input| border_${axis}_start_${prop}::parse(context, input))
input.try_parse(|input| border_${axis}_start_${prop}::parse(context, input))
.unwrap_or_else(|_| start_value.clone());

Ok(expanded! {
Expand Down

0 comments on commit 685e749

Please sign in to comment.