[librsvg] parsers.rs: New parser for the viewBox attribute



commit c6d8a57928534b4eb0e7e95fea6e7ec2b757d549
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Feb 21 20:18:15 2017 -0600

    parsers.rs: New parser for the viewBox attribute
    
    It just returns whatever default errors there are in Nom.  We should
    translate them to meaningful errors at some point.

 rust/Cargo.lock     |    4 ++--
 rust/Cargo.toml     |    4 +++-
 rust/src/parsers.rs |   29 ++++++++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 4 deletions(-)
---
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 46e9cfd..848b400 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -92,7 +92,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 [[package]]
 name = "nom"
 version = "2.1.0"
-source = "git+https://github.com/Geal/nom#fd9674c7207c8c14548c3f99a05c6c2d15c5ce26";
+source = "git+https://github.com/federicomenaquintero/nom.git#c5733d4779f513a3eb7f39c09de1c2155da4d6e2";
 
 [[package]]
 name = "pkg-config"
@@ -115,6 +115,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum glib-sys 0.3.2 (git+https://github.com/gtk-rs/sys)" = "<none>"
 "checksum gobject-sys 0.3.2 (git+https://github.com/gtk-rs/sys)" = "<none>"
 "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = 
"684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5"
-"checksum nom 2.1.0 (git+https://github.com/Geal/nom)" = "<none>"
+"checksum nom 2.1.0 (git+https://github.com/federicomenaquintero/nom.git)" = "<none>"
 "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = 
"167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 217df1b..9f9b6f6 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -24,7 +24,9 @@ git = "https://github.com/gtk-rs/glib";
 version = "0.1.1"
 
 [dependencies.nom]
-git = "https://github.com/Geal/nom";
+#git = "https://github.com/Geal/nom";
+git = "https://github.com/federicomenaquintero/nom.git";
+features = ["verbose-errors"]
 
 [lib]
 name = "rsvg_internals"
diff --git a/rust/src/parsers.rs b/rust/src/parsers.rs
index 053e185..364eacc 100644
--- a/rust/src/parsers.rs
+++ b/rust/src/parsers.rs
@@ -1,4 +1,4 @@
-use nom::{IResult, is_digit};
+use nom::{IResult, is_digit, double, ErrorKind};
 use std::str;
 
 #[derive(Debug, PartialEq, Eq)]
@@ -48,6 +48,29 @@ named! (pub floating_point_constant<FloatingPointConstant>,
                          ((Some (d), None), Some (e))))
 );
 
+named! (pub comma,
+        tag! (b","));
+
+// Parse a viewBox attribute
+// https://www.w3.org/TR/SVG/coords.html#ViewBoxAttribute
+//
+// viewBox: double [,] double [,] double [,] double [,]
+//
+// x, y, w, h
+//
+// Where w and h must be nonnegative.
+
+named! (pub view_box<(f64, f64, f64, f64)>,
+        verify! (ws! (do_parse! (x: double >>
+                                 opt! (comma) >>
+                                 y: double >>
+                                 opt! (comma) >>
+                                 w: double >>
+                                 opt! (comma) >>
+                                 h: double >>
+                                 (x, y, w, h))),
+                 |(x, y, w, h)| w >= 0.0 && h >= 0.0));
+
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -65,6 +88,10 @@ mod tests {
         assert_eq! (fractional_constant (b"1.23"), IResult::Done (&b""[..], (Some (&b"1"[..]), Some 
(&b"23"[..]))));
         assert_eq! (fractional_constant (b"1."), IResult::Done (&b""[..], (Some (&b"1"[..]), None)));
         assert_eq! (fractional_constant (b".23"), IResult::Done (&b""[..], (None, Some (&b"23"[..]))));
+    }
 
+    #[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)));
     }
 }


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