Skip to content
Merged

Untry #160

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'de> Deserialize<'de> for RGBA {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
let (r, g, b, a) = try!(Deserialize::deserialize(deserializer));
let (r, g, b, a) = Deserialize::deserialize(deserializer)?;
Ok(RGBA::new(r, g, b, a))
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ impl Color {
///
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Color, BasicParseError<'i>> {
let token = try!(input.next());
let token = input.next()?;
match token {
Token::Hash(ref value) | Token::IDHash(ref value) => {
Color::parse_hash(value.as_bytes())
Expand All @@ -162,26 +162,26 @@ impl Color {
pub fn parse_hash(value: &[u8]) -> Result<Self, ()> {
match value.len() {
8 => Ok(rgba(
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
try!(from_hex(value[6])) * 16 + try!(from_hex(value[7]))),
from_hex(value[0])? * 16 + from_hex(value[1])?,
from_hex(value[2])? * 16 + from_hex(value[3])?,
from_hex(value[4])? * 16 + from_hex(value[5])?,
from_hex(value[6])? * 16 + from_hex(value[7])?),
),
6 => Ok(rgb(
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5]))),
from_hex(value[0])? * 16 + from_hex(value[1])?,
from_hex(value[2])? * 16 + from_hex(value[3])?,
from_hex(value[4])? * 16 + from_hex(value[5])?),
),
4 => Ok(rgba(
try!(from_hex(value[0])) * 17,
try!(from_hex(value[1])) * 17,
try!(from_hex(value[2])) * 17,
try!(from_hex(value[3])) * 17),
from_hex(value[0])? * 17,
from_hex(value[1])? * 17,
from_hex(value[2])? * 17,
from_hex(value[3])? * 17),
),
3 => Ok(rgb(
try!(from_hex(value[0])) * 17,
try!(from_hex(value[1])) * 17,
try!(from_hex(value[2])) * 17),
from_hex(value[0])? * 17,
from_hex(value[1])? * 17,
from_hex(value[2])? * 17),
),
_ => Err(())
}
Expand Down Expand Up @@ -420,14 +420,14 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R

let alpha = if !arguments.is_exhausted() {
if uses_commas {
try!(arguments.expect_comma());
arguments.expect_comma()?;
} else {
match try!(arguments.next()) {
match arguments.next()? {
Token::Delim('/') => {},
t => return Err(BasicParseError::UnexpectedToken(t)),
};
};
let token = try!(arguments.next());
let token = arguments.next()?;
match token {
Token::Number { value: v, .. } => {
clamp_unit_f32(v)
Expand All @@ -443,7 +443,7 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
255
};

try!(arguments.expect_exhausted());
arguments.expect_exhausted()?;
Ok(rgba(red, green, blue, alpha))
}

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

// Either integers or percentages, but all the same type.
// https://drafts.csswg.org/css-color/#rgb-functions
match try!(arguments.next()) {
match arguments.next()? {
Token::Number { value: v, .. } => {
red = clamp_floor_256_f32(v);
green = clamp_floor_256_f32(match try!(arguments.next()) {
green = clamp_floor_256_f32(match arguments.next()? {
Token::Number { value: v, .. } => v,
Token::Comma => {
uses_commas = true;
try!(arguments.expect_number())
arguments.expect_number()?
}
t => return Err(BasicParseError::UnexpectedToken(t))
});
if uses_commas {
try!(arguments.expect_comma());
arguments.expect_comma()?;
}
blue = clamp_floor_256_f32(try!(arguments.expect_number()));
blue = clamp_floor_256_f32(arguments.expect_number()?);
}
Token::Percentage { unit_value, .. } => {
red = clamp_unit_f32(unit_value);
green = clamp_unit_f32(match try!(arguments.next()) {
green = clamp_unit_f32(match arguments.next()? {
Token::Percentage { unit_value, .. } => unit_value,
Token::Comma => {
uses_commas = true;
try!(arguments.expect_percentage())
arguments.expect_percentage()?
}
t => return Err(BasicParseError::UnexpectedToken(t))
});
if uses_commas {
try!(arguments.expect_comma());
arguments.expect_comma()?;
}
blue = clamp_unit_f32(try!(arguments.expect_percentage()));
blue = clamp_unit_f32(arguments.expect_percentage()?);
}
t => return Err(BasicParseError::UnexpectedToken(t))
};
Expand All @@ -498,7 +498,7 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
let mut uses_commas = false;
// Hue given as an angle
// https://drafts.csswg.org/css-values/#angles
let token = try!(arguments.next());
let token = arguments.next()?;
let hue_degrees = match token {
Token::Number { value: v, .. } => Ok(v),
Token::Dimension { value: v, ref unit, .. } => {
Expand All @@ -512,28 +512,28 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
}
t => return Err(BasicParseError::UnexpectedToken(t))
};
let hue_degrees = try!(hue_degrees.map_err(|()| BasicParseError::UnexpectedToken(token)));
let hue_degrees = hue_degrees.map_err(|()| BasicParseError::UnexpectedToken(token))?;
// Subtract an integer before rounding, to avoid some rounding errors:
let hue_normalized_degrees = hue_degrees - 360. * (hue_degrees / 360.).floor();
let hue = hue_normalized_degrees / 360.;

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

if uses_commas {
try!(arguments.expect_comma());
arguments.expect_comma()?;
}

let lightness = try!(arguments.expect_percentage());
let lightness = arguments.expect_percentage()?;
let lightness = lightness.max(0.).min(1.);

// https://drafts.csswg.org/css-color/#hsl-color
Expand Down
10 changes: 5 additions & 5 deletions src/nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{Token, Parser, BasicParseError};
/// in which case the caller needs to check if the arguments’ parser is exhausted.
/// Return `Ok((A, B))`, or `Err(())` for a syntax error.
pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), BasicParseError<'i>> {
let token = try!(input.next());
let token = input.next()?;
match token {
Token::Number { int_value: Some(b), .. } => {
Ok((0, b))
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn parse_nth<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(i32, i32), Basic
}
}
}
Token::Delim('+') => match try!(input.next_including_whitespace()) {
Token::Delim('+') => match input.next_including_whitespace()? {
Token::Ident(value) => {
match_ignore_ascii_case! { &value,
"n" => Ok(try!(parse_b(input, 1))),
Expand All @@ -61,8 +61,8 @@ fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), Bas
let start_position = input.position();
let token = input.next();
match token {
Ok(Token::Delim('+')) => Ok(try!(parse_signless_b(input, a, 1))),
Ok(Token::Delim('-')) => Ok(try!(parse_signless_b(input, a, -1))),
Ok(Token::Delim('+')) => Ok(parse_signless_b(input, a, 1)?),
Ok(Token::Delim('-')) => Ok(parse_signless_b(input, a, -1)?),
Ok(Token::Number { has_sign: true, int_value: Some(b), .. }) => Ok((a, b)),
_ => {
input.reset(start_position);
Expand All @@ -72,7 +72,7 @@ fn parse_b<'i, 't>(input: &mut Parser<'i, 't>, a: i32) -> Result<(i32, i32), Bas
}

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