[librsvg] AspectRatio: don't use a helper function to parse



commit 6d7e2a5a4481ffb85a2842d8d89a3e330f7adfa5
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Sep 14 11:29:22 2018 -0500

    AspectRatio: don't use a helper function to parse

 rsvg_internals/src/aspect_ratio.rs | 58 +++++++++++++++++---------------------
 1 file changed, 26 insertions(+), 32 deletions(-)
---
diff --git a/rsvg_internals/src/aspect_ratio.rs b/rsvg_internals/src/aspect_ratio.rs
index 1b31adc6..b4c61ebd 100644
--- a/rsvg_internals/src/aspect_ratio.rs
+++ b/rsvg_internals/src/aspect_ratio.rs
@@ -150,33 +150,10 @@ impl AspectRatio {
             }
         }
     }
-
-    fn parse_input<'i, 't>(p: &mut Parser<'i, 't>) -> Result<AspectRatio, ()> {
-        let defer = p.try(|p| p.expect_ident_matching("defer")).is_ok();
-
-        let align_xy = p.try(|p| {
-            p.expect_ident()
-                .map_err(|_| ())
-                .and_then(|ident| Align::parse_xy(ident))
-        })?;
-
-        let fit = p
-            .try(|p| {
-                p.expect_ident()
-                    .map_err(|_| ())
-                    .and_then(|ident| FitMode::parse(ident))
-            }).unwrap_or(FitMode::default());
-
-        p.expect_exhausted().map_err(|_| ())?;
-
-        let align = align_xy.map(|(x, y)| Align { x, y, fit });
-
-        Ok(AspectRatio { defer, align })
-    }
 }
 
 impl Align {
-    fn parse_xy(s: &str) -> Result<Option<(X, Y)>, ()> {
+    fn parse_xy(s: &str) -> Result<Option<(X, Y)>, ValueErrorKind> {
         use self::Align1D::*;
 
         match s {
@@ -194,17 +171,17 @@ impl Align {
             "xMidYMax" => Ok(Some((X(Mid), Y(Max)))),
             "xMaxYMax" => Ok(Some((X(Max), Y(Max)))),
 
-            _ => Err(()),
+            _ => Err(ValueErrorKind::Parse(ParseError::new("invalid alignment"))),
         }
     }
 }
 
 impl FitMode {
-    fn parse(s: &str) -> Result<FitMode, ()> {
+    fn parse(s: &str) -> Result<FitMode, ValueErrorKind> {
         match s {
             "meet" => Ok(FitMode::Meet),
             "slice" => Ok(FitMode::Slice),
-            _ => Err(()),
+            _ => Err(ValueErrorKind::Parse(ParseError::new("invalid fit mode"))),
         }
     }
 }
@@ -214,11 +191,28 @@ impl Parse for AspectRatio {
     type Err = ValueErrorKind;
 
     fn parse(parser: &mut Parser<'_, '_>, _: ()) -> Result<AspectRatio, ValueErrorKind> {
-        AspectRatio::parse_input(parser).map_err(|_| {
-            ValueErrorKind::Parse(ParseError::new(
-                "expected \"[defer] <align> [meet | slice]\"",
-            ))
-        })
+        let defer = parser.try(|p| p.expect_ident_matching("defer")).is_ok();
+
+        let align_xy = parser.try(|p| {
+            p.expect_ident()
+                .map_err(|_| ValueErrorKind::Parse(ParseError::new("expected identifier")))
+                .and_then(|ident| Align::parse_xy(ident))
+        })?;
+
+        let fit = parser
+            .try(|p| {
+                p.expect_ident()
+                    .map_err(|_| ValueErrorKind::Parse(ParseError::new("expected identifier")))
+                    .and_then(|ident| FitMode::parse(ident))
+            }).unwrap_or(FitMode::default());
+
+        parser
+            .expect_exhausted()
+            .map_err(|_| ValueErrorKind::Parse(ParseError::new("extra data in AspectRatio")))?;
+
+        let align = align_xy.map(|(x, y)| Align { x, y, fit });
+
+        Ok(AspectRatio { defer, align })
     }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]