[librsvg: 29/51] FontSizeSpec: simplify parsing
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 29/51] FontSizeSpec: simplify parsing
- Date: Thu, 19 Dec 2019 01:50:27 +0000 (UTC)
commit ca28b2bbcb2f15ad9603cdf492182d55c622e800
Author: Federico Mena Quintero <federico gnome org>
Date: Tue Dec 17 20:07:24 2019 -0600
FontSizeSpec: simplify parsing
rsvg_internals/src/font_props.rs | 48 ++++++++++++++--------------------------
1 file changed, 16 insertions(+), 32 deletions(-)
---
diff --git a/rsvg_internals/src/font_props.rs b/rsvg_internals/src/font_props.rs
index bf714ec4..48903dfb 100644
--- a/rsvg_internals/src/font_props.rs
+++ b/rsvg_internals/src/font_props.rs
@@ -62,38 +62,22 @@ impl FontSizeSpec {
impl Parse for FontSizeSpec {
fn parse(parser: &mut Parser<'_, '_>) -> Result<FontSizeSpec, crate::error::ValueErrorKind> {
- let parser_state = parser.state();
-
- Length::<Both>::parse(parser)
- .and_then(|s| Ok(FontSizeSpec::Value(s)))
- .or_else(|e| {
- parser.reset(&parser_state);
-
- {
- let token = parser.next().map_err(|_| {
- crate::error::ValueErrorKind::parse_error("expected token")
- })?;
-
- if let Token::Ident(ref cow) = token {
- match cow.as_ref() {
- "smaller" => return Ok(FontSizeSpec::Smaller),
- "larger" => return Ok(FontSizeSpec::Larger),
- "xx-small" => return Ok(FontSizeSpec::XXSmall),
- "x-small" => return Ok(FontSizeSpec::XSmall),
- "small" => return Ok(FontSizeSpec::Small),
- "medium" => return Ok(FontSizeSpec::Medium),
- "large" => return Ok(FontSizeSpec::Large),
- "x-large" => return Ok(FontSizeSpec::XLarge),
- "xx-large" => return Ok(FontSizeSpec::XXLarge),
- _ => (),
- };
- }
- }
-
- parser.reset(&parser_state);
-
- Err(e)
- })
+ parser.try_parse(|p| Length::<Both>::parse(p))
+ .and_then(|l| Ok(FontSizeSpec::Value(l)))
+ .or_else(|_| {
+ parse_identifiers!(
+ parser,
+ "smaller" => FontSizeSpec::Smaller,
+ "larger" => FontSizeSpec::Larger,
+ "xx-small" => FontSizeSpec::XXSmall,
+ "x-small" => FontSizeSpec::XSmall,
+ "small" => FontSizeSpec::Small,
+ "medium" => FontSizeSpec::Medium,
+ "large" => FontSizeSpec::Large,
+ "x-large" => FontSizeSpec::XLarge,
+ "xx-large" => FontSizeSpec::XXLarge,
+ )
+ }).map_err(|_: ParseError| ValueErrorKind::parse_error("parse error"))
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]