[librsvg] length.rs: Refactor parser to not have Ok() all over the place
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] length.rs: Refactor parser to not have Ok() all over the place
- Date: Mon, 31 Jul 2017 18:09:14 +0000 (UTC)
commit 829b3281d9b927cfdc3db3a7133424b6be6be5ac
Author: Federico Mena Quintero <federico gnome org>
Date: Mon Jul 31 12:57:01 2017 -0500
length.rs: Refactor parser to not have Ok() all over the place
rust/src/length.rs | 107 ++++++++++++++++++++++++++--------------------------
1 files changed, 54 insertions(+), 53 deletions(-)
---
diff --git a/rust/src/length.rs b/rust/src/length.rs
index d40cd72..1ad1d95 100644
--- a/rust/src/length.rs
+++ b/rust/src/length.rs
@@ -105,88 +105,89 @@ impl RsvgLength {
let mut input = ParserInput::new (string);
let mut parser = Parser::new (&mut input);
- let token = parser.next ()
- .map_err (|_| AttributeError::Parse (ParseError::new ("expected number and optional symbol, or
number and percentage")))?;
+ let length = {
+ let token = parser.next ()
+ .map_err (|_| AttributeError::Parse (ParseError::new ("expected number and optional symbol,
or number and percentage")))?;
- match token {
- &Token::Number { value, .. } => Ok (RsvgLength { length: value as f64,
- unit: LengthUnit::Default,
- dir: dir }),
+ match token {
+ &Token::Number { value, .. } => RsvgLength { length: value as f64,
+ unit: LengthUnit::Default,
+ dir: dir },
- &Token::Percentage { unit_value, .. } => Ok (RsvgLength { length: unit_value as f64,
- unit:
LengthUnit::Percent,
- dir: dir }),
+ &Token::Percentage { unit_value, .. } => RsvgLength { length: unit_value as f64,
+ unit: LengthUnit::Percent,
+ dir: dir },
- &Token::Dimension { value, ref unit, .. } => {
- let value = value as f64;
+ &Token::Dimension { value, ref unit, .. } => {
+ let value = value as f64;
- match unit.as_ref () {
- "em" => Ok (RsvgLength { length: value,
+ match unit.as_ref () {
+ "em" => RsvgLength { length: value,
unit: LengthUnit::FontEm,
- dir: dir }),
+ dir: dir },
- "ex" => Ok (RsvgLength { length: value,
+ "ex" => RsvgLength { length: value,
unit: LengthUnit::FontEx,
- dir: dir }),
+ dir: dir },
- "pt" => Ok (RsvgLength { length: value / POINTS_PER_INCH,
+ "pt" => RsvgLength { length: value / POINTS_PER_INCH,
unit: LengthUnit::Inch,
- dir: dir }),
+ dir: dir },
- "in" => Ok (RsvgLength { length: value,
+ "in" => RsvgLength { length: value,
unit: LengthUnit::Inch,
- dir: dir }),
+ dir: dir },
- "cm" => Ok (RsvgLength { length: value / CM_PER_INCH,
+ "cm" => RsvgLength { length: value / CM_PER_INCH,
unit: LengthUnit::Inch,
- dir: dir }),
+ dir: dir },
- "mm" => Ok (RsvgLength { length: value / MM_PER_INCH,
+ "mm" => RsvgLength { length: value / MM_PER_INCH,
unit: LengthUnit::Inch,
- dir: dir }),
+ dir: dir },
- "pc" => Ok (RsvgLength { length: value / PICA_PER_INCH,
+ "pc" => RsvgLength { length: value / PICA_PER_INCH,
unit: LengthUnit::Inch,
- dir: dir }),
+ dir: dir },
- "px" => Ok (RsvgLength { length: value,
+ "px" => RsvgLength { length: value,
unit: LengthUnit::Default,
- dir: dir }),
+ dir: dir },
- _ => return Err (make_err ());
- }
- },
+ _ => return Err (make_err ())
+ }
+ },
- // FIXME: why are the following in Length? They should be in FontSize
- &Token::Ident (ref cow) => match cow.as_ref () {
- "larger" => Ok (RsvgLength { length: 0.0,
+ // FIXME: why are the following in Length? They should be in FontSize
+ &Token::Ident (ref cow) => match cow.as_ref () {
+ "larger" => RsvgLength { length: 0.0,
unit: LengthUnit::RelativeLarger,
- dir: dir }),
+ dir: dir },
- "smaller" => Ok (RsvgLength { length: 0.0,
+ "smaller" => RsvgLength { length: 0.0,
unit: LengthUnit::RelativeSmaller,
- dir: dir }),
-
- "xx-small" |
- "x-small" |
- "small" |
- "medium" |
- "large" |
- "x-large" |
- "xx-large" => Ok (RsvgLength { length: compute_named_size (&*string),
+ dir: dir },
+
+ "xx-small" |
+ "x-small" |
+ "small" |
+ "medium" |
+ "large" |
+ "x-large" |
+ "xx-large" => RsvgLength { length: compute_named_size (&*string),
unit: LengthUnit::Inch,
- dir: dir }),
+ dir: dir },
- _ => return Err (make_err ());
- },
+ _ => return Err (make_err ())
+ },
- _ => return Err (make_err ());
+ _ => return Err (make_err ())
+ }
};
- .and_then (|r|
- parser.expect_exhausted ()
- .map (|_| r)
- .map_err (|_| make_err ()))
+ parser.expect_exhausted ().map_err (|_| make_err ())?;
+
+ Ok (length)
}
pub fn normalize (&self, draw_ctx: *const RsvgDrawingCtx) -> f64 {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]