[librsvg: 14/43] Convert Dasharray, StrokeDasharray to CssParseError



commit 7a1aa7c6e61771cab570119f259b645ca3f43ea5
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Dec 20 10:21:08 2019 -0600

    Convert Dasharray, StrokeDasharray to CssParseError

 rsvg_internals/src/dasharray.rs     | 52 +++++++++++++++++++------------------
 rsvg_internals/src/properties.rs    |  2 +-
 rsvg_internals/src/property_defs.rs |  2 +-
 3 files changed, 29 insertions(+), 27 deletions(-)
---
diff --git a/rsvg_internals/src/dasharray.rs b/rsvg_internals/src/dasharray.rs
index 56e726b2..d818e5ff 100644
--- a/rsvg_internals/src/dasharray.rs
+++ b/rsvg_internals/src/dasharray.rs
@@ -4,7 +4,7 @@ use cssparser::Parser;
 
 use crate::error::*;
 use crate::length::*;
-use crate::parsers::{optional_comma, Parse};
+use crate::parsers::{optional_comma, Parse, ParseToParseError};
 
 #[derive(Debug, PartialEq, Clone)]
 pub enum Dasharray {
@@ -18,8 +18,10 @@ impl Default for Dasharray {
     }
 }
 
-impl Parse for Dasharray {
-    fn parse(parser: &mut Parser<'_, '_>) -> Result<Dasharray, ValueErrorKind> {
+impl ParseToParseError for Dasharray {
+    fn parse_to_parse_error<'i>(
+        parser: &mut Parser<'i, '_>,
+    ) -> Result<Dasharray, CssParseError<'i>> {
         if parser
             .try_parse(|p| p.expect_ident_matching("none"))
             .is_ok()
@@ -30,7 +32,12 @@ impl Parse for Dasharray {
         let mut dasharray = Vec::new();
 
         loop {
-            let d = Length::<Both>::parse(parser).and_then(Length::<Both>::check_nonnegative)?;
+            let loc = parser.current_source_location();
+
+            let d = Length::<Both>::parse(parser)
+                .map_err(|e| e.into())
+                .and_then(Length::<Both>::check_nonnegative)
+                .map_err(|e| loc.new_custom_error(e))?;
             dasharray.push(d);
 
             if parser.is_exhausted() {
@@ -85,34 +92,29 @@ mod tests {
         let sample_6 = Dasharray::Array(vec![length_parse("5"), length_parse("3.14")]);
         let sample_7 = Dasharray::Array(vec![length_parse("2")]);
 
-        assert_eq!(Dasharray::parse_str("none").unwrap(), Dasharray::None);
-        assert_eq!(Dasharray::parse_str("1 2in,3 4%").unwrap(), expected);
-        assert_eq!(Dasharray::parse_str("10,6").unwrap(), sample_1);
-        assert_eq!(Dasharray::parse_str("5,5,20").unwrap(), sample_2);
-        assert_eq!(Dasharray::parse_str("10px 20px 20px").unwrap(), sample_3);
-        assert_eq!(Dasharray::parse_str("25  5 , 5 5").unwrap(), sample_4);
-        assert_eq!(Dasharray::parse_str("3.1415926,8").unwrap(), sample_5);
-        assert_eq!(Dasharray::parse_str("5, 3.14").unwrap(), sample_6);
-        assert_eq!(Dasharray::parse_str("2").unwrap(), sample_7);
+        assert_eq!(Dasharray::parse_str_to_parse_error("none").unwrap(), Dasharray::None);
+        assert_eq!(Dasharray::parse_str_to_parse_error("1 2in,3 4%").unwrap(), expected);
+        assert_eq!(Dasharray::parse_str_to_parse_error("10,6").unwrap(), sample_1);
+        assert_eq!(Dasharray::parse_str_to_parse_error("5,5,20").unwrap(), sample_2);
+        assert_eq!(Dasharray::parse_str_to_parse_error("10px 20px 20px").unwrap(), sample_3);
+        assert_eq!(Dasharray::parse_str_to_parse_error("25  5 , 5 5").unwrap(), sample_4);
+        assert_eq!(Dasharray::parse_str_to_parse_error("3.1415926,8").unwrap(), sample_5);
+        assert_eq!(Dasharray::parse_str_to_parse_error("5, 3.14").unwrap(), sample_6);
+        assert_eq!(Dasharray::parse_str_to_parse_error("2").unwrap(), sample_7);
 
         // Negative numbers
-        assert_eq!(
-            Dasharray::parse_str("20,40,-20"),
-            Err(ValueErrorKind::Value(String::from(
-                "value must be non-negative"
-            )))
-        );
+        assert!(Dasharray::parse_str_to_parse_error("20,40,-20").is_err());
 
         // Empty dash_array
-        assert!(Dasharray::parse_str("").is_err());
-        assert!(Dasharray::parse_str("\t  \n     ").is_err());
-        assert!(Dasharray::parse_str(",,,").is_err());
-        assert!(Dasharray::parse_str("10,  \t, 20 \n").is_err());
+        assert!(Dasharray::parse_str_to_parse_error("").is_err());
+        assert!(Dasharray::parse_str_to_parse_error("\t  \n     ").is_err());
+        assert!(Dasharray::parse_str_to_parse_error(",,,").is_err());
+        assert!(Dasharray::parse_str_to_parse_error("10,  \t, 20 \n").is_err());
 
         // No trailing commas allowed, parse error
-        assert!(Dasharray::parse_str("10,").is_err());
+        assert!(Dasharray::parse_str_to_parse_error("10,").is_err());
 
         // A comma should be followed by a number
-        assert!(Dasharray::parse_str("20,,10").is_err());
+        assert!(Dasharray::parse_str_to_parse_error("20,,10").is_err());
     }
 }
diff --git a/rsvg_internals/src/properties.rs b/rsvg_internals/src/properties.rs
index b7ff0924..da0eebbf 100644
--- a/rsvg_internals/src/properties.rs
+++ b/rsvg_internals/src/properties.rs
@@ -335,7 +335,7 @@ pub fn parse_property<'i>(prop_name: &QualName, input: &mut Parser<'i, '_>, acce
             Ok(ParsedProperty::Stroke(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "stroke-dasharray") =>
-            Ok(ParsedProperty::StrokeDasharray(parse_input(input)?)),
+            Ok(ParsedProperty::StrokeDasharray(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "stroke-dashoffset") =>
             Ok(ParsedProperty::StrokeDashoffset(parse_input(input)?)),
diff --git a/rsvg_internals/src/property_defs.rs b/rsvg_internals/src/property_defs.rs
index 9b3f3f9a..ebc0a6b5 100644
--- a/rsvg_internals/src/property_defs.rs
+++ b/rsvg_internals/src/property_defs.rs
@@ -442,7 +442,7 @@ make_property!(
     StrokeDasharray,
     default: Dasharray::default(),
     inherits_automatically: true,
-    newtype_parse: Dasharray,
+    newtype_parse_to_parse_error: Dasharray,
 );
 
 // https://www.w3.org/TR/SVG/painting.html#StrokeDashoffsetProperty


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