Skip to content

Commit 1cf263a

Browse files
author
bors-servo
authored
Auto merge of servo#160 - servo:untry, r=emilio
Untry <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/160) <!-- Reviewable:end -->
2 parents 8ab0c08 + f78d7ec commit 1cf263a

File tree

6 files changed

+134
-134
lines changed

6 files changed

+134
-134
lines changed

src/color.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'de> Deserialize<'de> for RGBA {
8888
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
8989
where D: Deserializer<'de>
9090
{
91-
let (r, g, b, a) = try!(Deserialize::deserialize(deserializer));
91+
let (r, g, b, a) = Deserialize::deserialize(deserializer)?;
9292
Ok(RGBA::new(r, g, b, a))
9393
}
9494
}
@@ -141,7 +141,7 @@ impl Color {
141141
///
142142
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
143143
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Color, BasicParseError<'i>> {
144-
let token = try!(input.next());
144+
let token = input.next()?;
145145
match token {
146146
Token::Hash(ref value) | Token::IDHash(ref value) => {
147147
Color::parse_hash(value.as_bytes())
@@ -162,26 +162,26 @@ impl Color {
162162
pub fn parse_hash(value: &[u8]) -> Result<Self, ()> {
163163
match value.len() {
164164
8 => Ok(rgba(
165-
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
166-
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
167-
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
168-
try!(from_hex(value[6])) * 16 + try!(from_hex(value[7]))),
165+
from_hex(value[0])? * 16 + from_hex(value[1])?,
166+
from_hex(value[2])? * 16 + from_hex(value[3])?,
167+
from_hex(value[4])? * 16 + from_hex(value[5])?,
168+
from_hex(value[6])? * 16 + from_hex(value[7])?),
169169
),
170170
6 => Ok(rgb(
171-
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
172-
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
173-
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5]))),
171+
from_hex(value[0])? * 16 + from_hex(value[1])?,
172+
from_hex(value[2])? * 16 + from_hex(value[3])?,
173+
from_hex(value[4])? * 16 + from_hex(value[5])?),
174174
),
175175
4 => Ok(rgba(
176-
try!(from_hex(value[0])) * 17,
177-
try!(from_hex(value[1])) * 17,
178-
try!(from_hex(value[2])) * 17,
179-
try!(from_hex(value[3])) * 17),
176+
from_hex(value[0])? * 17,
177+
from_hex(value[1])? * 17,
178+
from_hex(value[2])? * 17,
179+
from_hex(value[3])? * 17),
180180
),
181181
3 => Ok(rgb(
182-
try!(from_hex(value[0])) * 17,
183-
try!(from_hex(value[1])) * 17,
184-
try!(from_hex(value[2])) * 17),
182+
from_hex(value[0])? * 17,
183+
from_hex(value[1])? * 17,
184+
from_hex(value[2])? * 17),
185185
),
186186
_ => Err(())
187187
}
@@ -420,14 +420,14 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
420420

421421
let alpha = if !arguments.is_exhausted() {
422422
if uses_commas {
423-
try!(arguments.expect_comma());
423+
arguments.expect_comma()?;
424424
} else {
425-
match try!(arguments.next()) {
425+
match arguments.next()? {
426426
Token::Delim('/') => {},
427427
t => return Err(BasicParseError::UnexpectedToken(t)),
428428
};
429429
};
430-
let token = try!(arguments.next());
430+
let token = arguments.next()?;
431431
match token {
432432
Token::Number { value: v, .. } => {
433433
clamp_unit_f32(v)
@@ -443,7 +443,7 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
443443
255
444444
};
445445

446-
try!(arguments.expect_exhausted());
446+
arguments.expect_exhausted()?;
447447
Ok(rgba(red, green, blue, alpha))
448448
}
449449

@@ -457,36 +457,36 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
457457

458458
// Either integers or percentages, but all the same type.
459459
// https://drafts.csswg.org/css-color/#rgb-functions
460-
match try!(arguments.next()) {
460+
match arguments.next()? {
461461
Token::Number { value: v, .. } => {
462462
red = clamp_floor_256_f32(v);
463-
green = clamp_floor_256_f32(match try!(arguments.next()) {
463+
green = clamp_floor_256_f32(match arguments.next()? {
464464
Token::Number { value: v, .. } => v,
465465
Token::Comma => {
466466
uses_commas = true;
467-
try!(arguments.expect_number())
467+
arguments.expect_number()?
468468
}
469469
t => return Err(BasicParseError::UnexpectedToken(t))
470470
});
471471
if uses_commas {
472-
try!(arguments.expect_comma());
472+
arguments.expect_comma()?;
473473
}
474-
blue = clamp_floor_256_f32(try!(arguments.expect_number()));
474+
blue = clamp_floor_256_f32(arguments.expect_number()?);
475475
}
476476
Token::Percentage { unit_value, .. } => {
477477
red = clamp_unit_f32(unit_value);
478-
green = clamp_unit_f32(match try!(arguments.next()) {
478+
green = clamp_unit_f32(match arguments.next()? {
479479
Token::Percentage { unit_value, .. } => unit_value,
480480
Token::Comma => {
481481
uses_commas = true;
482-
try!(arguments.expect_percentage())
482+
arguments.expect_percentage()?
483483
}
484484
t => return Err(BasicParseError::UnexpectedToken(t))
485485
});
486486
if uses_commas {
487-
try!(arguments.expect_comma());
487+
arguments.expect_comma()?;
488488
}
489-
blue = clamp_unit_f32(try!(arguments.expect_percentage()));
489+
blue = clamp_unit_f32(arguments.expect_percentage()?);
490490
}
491491
t => return Err(BasicParseError::UnexpectedToken(t))
492492
};
@@ -498,7 +498,7 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
498498
let mut uses_commas = false;
499499
// Hue given as an angle
500500
// https://drafts.csswg.org/css-values/#angles
501-
let token = try!(arguments.next());
501+
let token = arguments.next()?;
502502
let hue_degrees = match token {
503503
Token::Number { value: v, .. } => Ok(v),
504504
Token::Dimension { value: v, ref unit, .. } => {
@@ -512,28 +512,28 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
512512
}
513513
t => return Err(BasicParseError::UnexpectedToken(t))
514514
};
515-
let hue_degrees = try!(hue_degrees.map_err(|()| BasicParseError::UnexpectedToken(token)));
515+
let hue_degrees = hue_degrees.map_err(|()| BasicParseError::UnexpectedToken(token))?;
516516
// Subtract an integer before rounding, to avoid some rounding errors:
517517
let hue_normalized_degrees = hue_degrees - 360. * (hue_degrees / 360.).floor();
518518
let hue = hue_normalized_degrees / 360.;
519519

520520
// Saturation and lightness are clamped to 0% ... 100%
521521
// https://drafts.csswg.org/css-color/#the-hsl-notation
522-
let saturation = match try!(arguments.next()) {
522+
let saturation = match arguments.next()? {
523523
Token::Percentage { unit_value, .. } => unit_value,
524524
Token::Comma => {
525525
uses_commas = true;
526-
try!(arguments.expect_percentage())
526+
arguments.expect_percentage()?
527527
}
528528
t => return Err(BasicParseError::UnexpectedToken(t))
529529
};
530530
let saturation = saturation.max(0.).min(1.);
531531

532532
if uses_commas {
533-
try!(arguments.expect_comma());
533+
arguments.expect_comma()?;
534534
}
535535

536-
let lightness = try!(arguments.expect_percentage());
536+
let lightness = arguments.expect_percentage()?;
537537
let lightness = lightness.max(0.).min(1.);
538538

539539
// https://drafts.csswg.org/css-color/#hsl-color

src/nth.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::{Token, Parser, BasicParseError};
1212
/// in which case the caller needs to check if the arguments’ parser is exhausted.
1313
/// Return `Ok((A, B))`, or `Err(())` for a syntax error.
1414
pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), BasicParseError<'i>> {
15-
let token = try!(input.next());
15+
let token = input.next()?;
1616
match token {
1717
Token::Number { int_value: Some(b), .. } => {
1818
Ok((0, b))
@@ -42,7 +42,7 @@ pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), Basic
4242
}
4343
}
4444
}
45-
Token::Delim('+') => match try!(input.next_including_whitespace()) {
45+
Token::Delim('+') => match input.next_including_whitespace()? {
4646
Token::Ident(value) => {
4747
match_ignore_ascii_case! { &value,
4848
"n" => Ok(try!(parse_b(input, 1))),
@@ -61,8 +61,8 @@ fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), Bas
6161
let start_position = input.position();
6262
let token = input.next();
6363
match token {
64-
Ok(Token::Delim('+')) => Ok(try!(parse_signless_b(input, a, 1))),
65-
Ok(Token::Delim('-')) => Ok(try!(parse_signless_b(input, a, -1))),
64+
Ok(Token::Delim('+')) => Ok(parse_signless_b(input, a, 1)?),
65+
Ok(Token::Delim('-')) => Ok(parse_signless_b(input, a, -1)?),
6666
Ok(Token::Number { has_sign: true, int_value: Some(b), .. }) => Ok((a, b)),
6767
_ => {
6868
input.reset(start_position);
@@ -72,7 +72,7 @@ fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), Bas
7272
}
7373

7474
fn parse_signless_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32, b_sign: i32) -> Result<(i32, i32), BasicParseError<'i>> {
75-
let token = try!(input.next());
75+
let token = input.next()?;
7676
match token {
7777
Token::Number { has_sign: false, int_value: Some(b), .. } => Ok((a, b_sign * b)),
7878
_ => Err(())

0 commit comments

Comments
 (0)