[librsvg] parsers::view_box() - Deal in &str and Result, not in &[u8] and nom::IResult



commit 401c323270f307be0774780f85db605921929f96
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Jul 19 13:37:01 2017 -0500

    parsers::view_box() - Deal in &str and Result, not in &[u8] and nom::IResult

 rust/src/parsers.rs |   20 ++++++++++----------
 rust/src/viewbox.rs |    6 +++---
 2 files changed, 13 insertions(+), 13 deletions(-)
---
diff --git a/rust/src/parsers.rs b/rust/src/parsers.rs
index 69abd86..c0a3423 100644
--- a/rust/src/parsers.rs
+++ b/rust/src/parsers.rs
@@ -111,7 +111,7 @@ pub fn angle_degrees (s: &str) -> Result <f64, ParseError> {
 //
 // Where w and h must be nonnegative.
 
-named! (pub view_box<(f64, f64, f64, f64)>,
+named! (parse_view_box<(f64, f64, f64, f64)>,
         ws! (do_parse! (x: double    >>
                         opt! (comma) >>
                         y: double    >>
@@ -122,6 +122,11 @@ named! (pub view_box<(f64, f64, f64, f64)>,
                         eof! ()      >>
                         (x, y, w, h))));
 
+pub fn view_box (s: &str) -> Result <(f64, f64, f64, f64), ParseError> {
+    parse_view_box (s.as_bytes ()).to_full_result ()
+        .map_err (|_| ParseError::new ("string does not match 'x [,] y [,] w [,] h'"))
+}
+
 // Coordinate pairs, separated by optional (whitespace-and/or comma)
 //
 // All of these yield (1, -2): "1 -2", "1, -2", "1-2"
@@ -318,16 +323,11 @@ mod tests {
 
     #[test]
     fn parses_view_box () {
-        assert_eq! (view_box (b"1 2 3 4"), IResult::Done (&b""[..], (1.0, 2.0, 3.0, 4.0)));
-        assert_eq! (view_box (b"1,2,3 4"), IResult::Done (&b""[..], (1.0, 2.0, 3.0, 4.0)));
-        assert_eq! (view_box (b" 1,2,3 4 "), IResult::Done (&b""[..], (1.0, 2.0, 3.0, 4.0)));
-
-        let result = view_box (b"1 2 3 4 5");
+        assert_eq! (view_box ("1 2 3 4"), Ok ((1.0, 2.0, 3.0, 4.0)));
+        assert_eq! (view_box ("1,2,3 4"), Ok ((1.0, 2.0, 3.0, 4.0)));
+        assert_eq! (view_box (" 1,2,3 4 "), Ok ((1.0, 2.0, 3.0, 4.0)));
 
-        match result {
-            IResult::Error (_) => { },
-            _ => { panic! ("{:?} should be an invalid viewBox", result); }
-        }
+        assert! (view_box ("1 2 3 4 5").is_err ());
     }
 
     #[test]
diff --git a/rust/src/viewbox.rs b/rust/src/viewbox.rs
index 48a0cfd..99d248c 100644
--- a/rust/src/viewbox.rs
+++ b/rust/src/viewbox.rs
@@ -50,7 +50,7 @@ impl FromStr for RsvgViewBox {
     type Err = AttributeError;
 
     fn from_str (s: &str) -> Result<RsvgViewBox, AttributeError> {
-        let result = parsers::view_box (s.trim ().as_bytes ()).to_full_result ();
+        let result = parsers::view_box (s.trim ());
 
         match result {
             Ok ((x, y, w, h)) => {
@@ -65,8 +65,8 @@ impl FromStr for RsvgViewBox {
                 }
             },
 
-            Err (_) => {
-                Err (AttributeError::Parse (ParseError::new ("string does not match 'x [,] y [,] w [,] h'")))
+            Err (e) => {
+                Err (AttributeError::Parse (e))
             }
         }
     }


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