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



commit b52da5b626f57426b454cfba60cc8ce9e7497eb4
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Jul 19 13:11:29 2017 -0500

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

 rust/src/length.rs  |   58 +++++++++++++++++++++++++-------------------------
 rust/src/parsers.rs |   24 +++++++++++++--------
 2 files changed, 44 insertions(+), 38 deletions(-)
---
diff --git a/rust/src/length.rs b/rust/src/length.rs
index c0563ce..a3bbb37 100644
--- a/rust/src/length.rs
+++ b/rust/src/length.rs
@@ -102,47 +102,47 @@ fn make_err () -> AttributeError {
 
 impl RsvgLength {
     pub fn parse (string: &str, dir: LengthDir) -> Result <RsvgLength, AttributeError> {
-        let r = parsers::number_and_units (string.as_bytes ()).to_full_result ();
+        let r = parsers::number_and_units (string);
 
         match r {
             Ok ((value, unit)) => {
                 match unit {
-                    b"%" => Ok (RsvgLength { length: value * 0.01, // normalize to [0, 1]
-                                             unit:   LengthUnit::Percent,
-                                             dir:    dir }),
+                    "%" => Ok (RsvgLength { length: value * 0.01, // normalize to [0, 1]
+                                            unit:   LengthUnit::Percent,
+                                            dir:    dir }),
 
-                    b"em" => Ok (RsvgLength { length: value,
-                                              unit:   LengthUnit::FontEm,
-                                              dir:    dir }),
+                    "em" => Ok (RsvgLength { length: value,
+                                             unit:   LengthUnit::FontEm,
+                                             dir:    dir }),
 
-                    b"ex" => Ok (RsvgLength { length: value,
-                                              unit:   LengthUnit::FontEx,
-                                              dir:    dir }),
+                    "ex" => Ok (RsvgLength { length: value,
+                                             unit:   LengthUnit::FontEx,
+                                             dir:    dir }),
 
-                    b"pt" => Ok (RsvgLength { length: value / POINTS_PER_INCH,
-                                              unit:   LengthUnit::Inch,
-                                              dir:    dir }),
+                    "pt" => Ok (RsvgLength { length: value / POINTS_PER_INCH,
+                                             unit:   LengthUnit::Inch,
+                                             dir:    dir }),
 
-                    b"in" => Ok (RsvgLength { length: value,
-                                              unit:   LengthUnit::Inch,
-                                              dir:    dir }),
+                    "in" => Ok (RsvgLength { length: value,
+                                             unit:   LengthUnit::Inch,
+                                             dir:    dir }),
 
-                    b"cm" => Ok (RsvgLength { length: value / CM_PER_INCH,
-                                              unit:   LengthUnit::Inch,
-                                              dir:    dir }),
+                    "cm" => Ok (RsvgLength { length: value / CM_PER_INCH,
+                                             unit:   LengthUnit::Inch,
+                                             dir:    dir }),
 
-                    b"mm" => Ok (RsvgLength { length: value / MM_PER_INCH,
-                                              unit:   LengthUnit::Inch,
-                                              dir:    dir }),
+                    "mm" => Ok (RsvgLength { length: value / MM_PER_INCH,
+                                             unit:   LengthUnit::Inch,
+                                             dir:    dir }),
 
-                    b"pc" => Ok (RsvgLength { length: value / PICA_PER_INCH,
-                                              unit:   LengthUnit::Inch,
-                                              dir:    dir }),
+                    "pc" => Ok (RsvgLength { length: value / PICA_PER_INCH,
+                                             unit:   LengthUnit::Inch,
+                                             dir:    dir }),
 
-                    b"px" |
-                    b"" => Ok (RsvgLength { length: value,
-                                            unit:   LengthUnit::Default,
-                                            dir:    dir }),
+                    "px" |
+                    "" => Ok (RsvgLength { length: value,
+                                           unit:   LengthUnit::Default,
+                                           dir:    dir }),
 
                     _ => Err (make_err ())
                 }
diff --git a/rust/src/parsers.rs b/rust/src/parsers.rs
index c9bf64a..b9c0025 100644
--- a/rust/src/parsers.rs
+++ b/rust/src/parsers.rs
@@ -74,20 +74,26 @@ fn is_alphabetic_or_dash (c: u8) -> bool {
      is_alphabetic (c) || c == '-' as u8 || c == '%' as u8
 }
 
-named! (pub number_and_units<(f64, &[u8])>,
+named! (parse_number_and_units<(f64, &[u8])>,
         tuple! (double,
                 take_while! (is_alphabetic_or_dash)));
 
+pub fn number_and_units (s: &str) -> Result <(f64, &str), ParseError> {
+    parse_number_and_units (s.as_bytes ()).to_full_result ()
+        .map (|(v, slice)| (v, str::from_utf8 (slice).unwrap ()))
+        .map_err (|_| ParseError::new ("expected number and symbol"))
+}
+
 pub fn angle_degrees (s: &str) -> Result <f64, ParseError> {
-    let r = number_and_units (s.as_bytes ()).to_full_result ();
+    let r = number_and_units (s);
 
     match r {
         Ok ((value, unit)) => {
             match unit {
-                b"deg"  => Ok (value),
-                b"grad" => Ok (value * 360.0 / 400.0),
-                b"rad"  => Ok (value * 180.0 / PI),
-                b""     => Ok (value),
+                "deg"  => Ok (value),
+                "grad" => Ok (value * 360.0 / 400.0),
+                "rad"  => Ok (value * 180.0 / PI),
+                ""     => Ok (value),
                 _       => Err (ParseError::new ("expected (\"deg\", \"rad\", \"grad\")? after number"))
             }
         },
@@ -440,9 +446,9 @@ mod tests {
 
     #[test]
     fn parses_number_and_units () {
-        assert_eq! (number_and_units (b"-1"), IResult::Done (&b""[..], (-1.0, &b""[..])));
-        assert_eq! (number_and_units (b"0x"), IResult::Done (&b""[..], (0.0, &b"x"[..])));
-        assert_eq! (number_and_units (b"-55.5x-large"), IResult::Done (&b""[..], (-55.5, &b"x-large"[..])));
+        assert_eq! (number_and_units ("-1"), Ok ((-1.0, "")));
+        assert_eq! (number_and_units ("0x"), Ok ((0.0, "x")));
+        assert_eq! (number_and_units ("-55.5x-large"), Ok ((-55.5, "x-large")));
     }
 
     #[test]


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