[librsvg: 11/43] Convert IRI, ClipPath, Filter, Marker*, Mask to CssParseError



commit 02187a6360b79d439722241333b4e7962865c5fa
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Dec 19 21:11:52 2019 -0600

    Convert IRI, ClipPath, Filter, Marker*, Mask to CssParseError

 rsvg_internals/src/iri.rs           | 35 +++++++++++++++++++----------------
 rsvg_internals/src/properties.rs    | 14 +++++++-------
 rsvg_internals/src/property_defs.rs | 14 +++++++-------
 3 files changed, 33 insertions(+), 30 deletions(-)
---
diff --git a/rsvg_internals/src/iri.rs b/rsvg_internals/src/iri.rs
index f4091be0..2984fee0 100644
--- a/rsvg_internals/src/iri.rs
+++ b/rsvg_internals/src/iri.rs
@@ -3,8 +3,8 @@
 use cssparser::Parser;
 
 use crate::allowed_url::{Fragment, Href};
-use crate::error::ValueErrorKind;
-use crate::parsers::Parse;
+use crate::error::*;
+use crate::parsers::ParseToParseError;
 
 /// Used where style properties take a funciri or "none"
 ///
@@ -34,24 +34,27 @@ impl IRI {
     }
 }
 
-impl Parse for IRI {
-    fn parse(parser: &mut Parser<'_, '_>) -> Result<IRI, ValueErrorKind> {
+impl ParseToParseError for IRI {
+    fn parse_to_parse_error<'i>(parser: &mut Parser<'i, '_>) -> Result<IRI, CssParseError<'i>> {
         if parser
             .try_parse(|i| i.expect_ident_matching("none"))
             .is_ok()
         {
             Ok(IRI::None)
         } else {
+            let loc = parser.current_source_location();
+
             let url = parser.expect_url()?;
             parser.expect_exhausted()?;
 
-            let href = Href::parse(&url)
-                .map_err(|_| ValueErrorKind::parse_error("could not parse href"))?;
+            let href = Href::parse(&url).map_err(|_| {
+                loc.new_custom_error(ValueErrorKind::parse_error("could not parse href"))
+            })?;
 
             match href {
-                Href::PlainUrl(_) => Err(ValueErrorKind::parse_error(
+                Href::PlainUrl(_) => Err(loc.new_custom_error(ValueErrorKind::parse_error(
                     "href requires a fragment identifier",
-                ))?,
+                )))?,
                 Href::WithFragment(f) => Ok(IRI::Resource(f)),
             }
         }
@@ -64,18 +67,18 @@ mod tests {
 
     #[test]
     fn parses_none() {
-        assert_eq!(IRI::parse_str("none"), Ok(IRI::None));
+        assert_eq!(IRI::parse_str_to_parse_error("none"), Ok(IRI::None));
     }
 
     #[test]
     fn parses_url() {
         assert_eq!(
-            IRI::parse_str("url(#bar)"),
+            IRI::parse_str_to_parse_error("url(#bar)"),
             Ok(IRI::Resource(Fragment::new(None, "bar".to_string())))
         );
 
         assert_eq!(
-            IRI::parse_str("url(foo#bar)"),
+            IRI::parse_str_to_parse_error("url(foo#bar)"),
             Ok(IRI::Resource(Fragment::new(
                 Some("foo".to_string()),
                 "bar".to_string()
@@ -84,19 +87,19 @@ mod tests {
 
         // be permissive if the closing ) is missing
         assert_eq!(
-            IRI::parse_str("url(#bar"),
+            IRI::parse_str_to_parse_error("url(#bar"),
             Ok(IRI::Resource(Fragment::new(None, "bar".to_string())))
         );
         assert_eq!(
-            IRI::parse_str("url(foo#bar"),
+            IRI::parse_str_to_parse_error("url(foo#bar"),
             Ok(IRI::Resource(Fragment::new(
                 Some("foo".to_string()),
                 "bar".to_string()
             )))
         );
 
-        assert!(IRI::parse_str("").is_err());
-        assert!(IRI::parse_str("foo").is_err());
-        assert!(IRI::parse_str("url(foo)bar").is_err());
+        assert!(IRI::parse_str_to_parse_error("").is_err());
+        assert!(IRI::parse_str_to_parse_error("foo").is_err());
+        assert!(IRI::parse_str_to_parse_error("url(foo)bar").is_err());
     }
 }
diff --git a/rsvg_internals/src/properties.rs b/rsvg_internals/src/properties.rs
index 7dab4883..2d7e7b81 100644
--- a/rsvg_internals/src/properties.rs
+++ b/rsvg_internals/src/properties.rs
@@ -234,7 +234,7 @@ pub fn parse_property<'i>(prop_name: &QualName, input: &mut Parser<'i, '_>, acce
             Ok(ParsedProperty::BaselineShift(parse_input(input)?)),
 
         expanded_name!(svg "clip-path") =>
-            Ok(ParsedProperty::ClipPath(parse_input(input)?)),
+            Ok(ParsedProperty::ClipPath(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "clip-rule") =>
             Ok(ParsedProperty::ClipRule(parse_input_to_parse_error(input)?)),
@@ -264,7 +264,7 @@ pub fn parse_property<'i>(prop_name: &QualName, input: &mut Parser<'i, '_>, acce
             Ok(ParsedProperty::FillRule(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "filter") =>
-            Ok(ParsedProperty::Filter(parse_input(input)?)),
+            Ok(ParsedProperty::Filter(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "flood-color") =>
             Ok(ParsedProperty::FloodColor(parse_input_to_parse_error(input)?)),
@@ -298,23 +298,23 @@ pub fn parse_property<'i>(prop_name: &QualName, input: &mut Parser<'i, '_>, acce
 
         expanded_name!(svg "marker") => {
             if accept_shorthands {
-                Ok(ParsedProperty::Marker(parse_input(input)?))
+                Ok(ParsedProperty::Marker(parse_input_to_parse_error(input)?))
             } else {
                 Err(ValueErrorKind::UnknownProperty)?
             }
         }
 
         expanded_name!(svg "marker-end") =>
-            Ok(ParsedProperty::MarkerEnd(parse_input(input)?)),
+            Ok(ParsedProperty::MarkerEnd(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "marker-mid") =>
-            Ok(ParsedProperty::MarkerMid(parse_input(input)?)),
+            Ok(ParsedProperty::MarkerMid(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "marker-start") =>
-            Ok(ParsedProperty::MarkerStart(parse_input(input)?)),
+            Ok(ParsedProperty::MarkerStart(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "mask") =>
-            Ok(ParsedProperty::Mask(parse_input(input)?)),
+            Ok(ParsedProperty::Mask(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "opacity") =>
             Ok(ParsedProperty::Opacity(parse_input_to_parse_error(input)?)),
diff --git a/rsvg_internals/src/property_defs.rs b/rsvg_internals/src/property_defs.rs
index 178877d9..454ffc29 100644
--- a/rsvg_internals/src/property_defs.rs
+++ b/rsvg_internals/src/property_defs.rs
@@ -66,7 +66,7 @@ make_property!(
     ClipPath,
     default: IRI::None,
     inherits_automatically: false,
-    newtype_parse: IRI,
+    newtype_parse_to_parse_error: IRI,
 );
 
 // https://www.w3.org/TR/SVG/masking.html#ClipRuleProperty
@@ -196,7 +196,7 @@ make_property!(
     Filter,
     default: IRI::None,
     inherits_automatically: false,
-    newtype_parse: IRI,
+    newtype_parse_to_parse_error: IRI,
 );
 
 // https://www.w3.org/TR/SVG/filters.html#FloodColorProperty
@@ -333,7 +333,7 @@ make_property!(
     Marker,
     default: IRI::None,
     inherits_automatically: true,
-    newtype_parse: IRI,
+    newtype_parse_to_parse_error: IRI,
 );
 
 // https://www.w3.org/TR/SVG/painting.html#MarkerEndProperty
@@ -342,7 +342,7 @@ make_property!(
     MarkerEnd,
     default: IRI::None,
     inherits_automatically: true,
-    newtype_parse: IRI,
+    newtype_parse_to_parse_error: IRI,
 );
 
 // https://www.w3.org/TR/SVG/painting.html#MarkerMidProperty
@@ -351,7 +351,7 @@ make_property!(
     MarkerMid,
     default: IRI::None,
     inherits_automatically: true,
-    newtype_parse: IRI,
+    newtype_parse_to_parse_error: IRI,
 );
 
 // https://www.w3.org/TR/SVG/painting.html#MarkerStartProperty
@@ -360,7 +360,7 @@ make_property!(
     MarkerStart,
     default: IRI::None,
     inherits_automatically: true,
-    newtype_parse: IRI,
+    newtype_parse_to_parse_error: IRI,
 );
 
 // https://www.w3.org/TR/SVG/masking.html#MaskProperty
@@ -369,7 +369,7 @@ make_property!(
     Mask,
     default: IRI::None,
     inherits_automatically: false,
-    newtype_parse: IRI,
+    newtype_parse_to_parse_error: IRI,
 );
 
 // https://www.w3.org/TR/SVG/masking.html#OpacityProperty


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