[librsvg: 13/43] Convert PaintServer, Fill, Stroke to CssParseError



commit 5ea031c4eae7d358c010aa0e7aa45563119b40cb
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Dec 20 10:13:32 2019 -0600

    Convert PaintServer, Fill, Stroke to CssParseError

 rsvg_internals/src/paint_server.rs  | 42 ++++++++++++++++++++-----------------
 rsvg_internals/src/properties.rs    |  4 ++--
 rsvg_internals/src/property_defs.rs |  6 +++---
 3 files changed, 28 insertions(+), 24 deletions(-)
---
diff --git a/rsvg_internals/src/paint_server.rs b/rsvg_internals/src/paint_server.rs
index 8e80b66d..20739b83 100644
--- a/rsvg_internals/src/paint_server.rs
+++ b/rsvg_internals/src/paint_server.rs
@@ -7,7 +7,7 @@ use crate::bbox::BoundingBox;
 use crate::drawing_ctx::DrawingCtx;
 use crate::error::*;
 use crate::node::{CascadedValues, RsvgNode};
-use crate::parsers::Parse;
+use crate::parsers::ParseToParseError;
 use crate::properties::ComputedValues;
 use crate::unit_interval::UnitInterval;
 
@@ -21,14 +21,18 @@ pub enum PaintServer {
     SolidColor(cssparser::Color),
 }
 
-impl Parse for PaintServer {
-    fn parse(parser: &mut Parser<'_, '_>) -> Result<PaintServer, ValueErrorKind> {
+impl ParseToParseError for PaintServer {
+    fn parse_to_parse_error<'i>(
+        parser: &mut Parser<'i, '_>,
+    ) -> Result<PaintServer, CssParseError<'i>> {
         if parser
             .try_parse(|i| i.expect_ident_matching("none"))
             .is_ok()
         {
             Ok(PaintServer::None)
         } else if let Ok(url) = parser.try_parse(|i| i.expect_url()) {
+            let loc = parser.current_source_location();
+
             let alternate = if !parser.is_exhausted() {
                 if parser
                     .try_parse(|i| i.expect_ident_matching("none"))
@@ -43,13 +47,13 @@ impl Parse for PaintServer {
             };
 
             Ok(PaintServer::Iri {
-                iri: Fragment::parse(&url)?,
+                iri: Fragment::parse(&url)
+                    .map_err(|e: HrefError| -> ValueErrorKind { e.into() })
+                    .map_err(|e| loc.new_custom_error(e))?,
                 alternate,
             })
         } else {
-            cssparser::Color::parse(parser)
-                .map(PaintServer::SolidColor)
-                .map_err(ValueErrorKind::from)
+            Ok(cssparser::Color::parse(parser).map(PaintServer::SolidColor)?)
         }
     }
 }
@@ -124,27 +128,27 @@ mod tests {
 
     #[test]
     fn catches_invalid_syntax() {
-        assert!(PaintServer::parse_str("").is_err());
-        assert!(PaintServer::parse_str("42").is_err());
-        assert!(PaintServer::parse_str("invalid").is_err());
+        assert!(PaintServer::parse_str_to_parse_error("").is_err());
+        assert!(PaintServer::parse_str_to_parse_error("42").is_err());
+        assert!(PaintServer::parse_str_to_parse_error("invalid").is_err());
     }
 
     #[test]
     fn parses_none() {
-        assert_eq!(PaintServer::parse_str("none"), Ok(PaintServer::None));
+        assert_eq!(PaintServer::parse_str_to_parse_error("none"), Ok(PaintServer::None));
     }
 
     #[test]
     fn parses_solid_color() {
         assert_eq!(
-            PaintServer::parse_str("rgb(255, 128, 64, 0.5)"),
+            PaintServer::parse_str_to_parse_error("rgb(255, 128, 64, 0.5)"),
             Ok(PaintServer::SolidColor(cssparser::Color::RGBA(
                 cssparser::RGBA::new(255, 128, 64, 128)
             )))
         );
 
         assert_eq!(
-            PaintServer::parse_str("currentColor"),
+            PaintServer::parse_str_to_parse_error("currentColor"),
             Ok(PaintServer::SolidColor(cssparser::Color::CurrentColor))
         );
     }
@@ -152,7 +156,7 @@ mod tests {
     #[test]
     fn parses_iri() {
         assert_eq!(
-            PaintServer::parse_str("url(#link)"),
+            PaintServer::parse_str_to_parse_error("url(#link)"),
             Ok(PaintServer::Iri {
                 iri: Fragment::new(None, "link".to_string()),
                 alternate: None,
@@ -160,7 +164,7 @@ mod tests {
         );
 
         assert_eq!(
-            PaintServer::parse_str("url(foo#link) none"),
+            PaintServer::parse_str_to_parse_error("url(foo#link) none"),
             Ok(PaintServer::Iri {
                 iri: Fragment::new(Some("foo".to_string()), "link".to_string()),
                 alternate: None,
@@ -168,7 +172,7 @@ mod tests {
         );
 
         assert_eq!(
-            PaintServer::parse_str("url(#link) #ff8040"),
+            PaintServer::parse_str_to_parse_error("url(#link) #ff8040"),
             Ok(PaintServer::Iri {
                 iri: Fragment::new(None, "link".to_string()),
                 alternate: Some(cssparser::Color::RGBA(cssparser::RGBA::new(
@@ -178,7 +182,7 @@ mod tests {
         );
 
         assert_eq!(
-            PaintServer::parse_str("url(#link) rgb(255, 128, 64, 0.5)"),
+            PaintServer::parse_str_to_parse_error("url(#link) rgb(255, 128, 64, 0.5)"),
             Ok(PaintServer::Iri {
                 iri: Fragment::new(None, "link".to_string()),
                 alternate: Some(cssparser::Color::RGBA(cssparser::RGBA::new(
@@ -188,13 +192,13 @@ mod tests {
         );
 
         assert_eq!(
-            PaintServer::parse_str("url(#link) currentColor"),
+            PaintServer::parse_str_to_parse_error("url(#link) currentColor"),
             Ok(PaintServer::Iri {
                 iri: Fragment::new(None, "link".to_string()),
                 alternate: Some(cssparser::Color::CurrentColor),
             },)
         );
 
-        assert!(PaintServer::parse_str("url(#link) invalid").is_err());
+        assert!(PaintServer::parse_str_to_parse_error("url(#link) invalid").is_err());
     }
 }
diff --git a/rsvg_internals/src/properties.rs b/rsvg_internals/src/properties.rs
index 2d7e7b81..b7ff0924 100644
--- a/rsvg_internals/src/properties.rs
+++ b/rsvg_internals/src/properties.rs
@@ -255,7 +255,7 @@ pub fn parse_property<'i>(prop_name: &QualName, input: &mut Parser<'i, '_>, acce
             Ok(ParsedProperty::EnableBackground(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "fill") =>
-            Ok(ParsedProperty::Fill(parse_input(input)?)),
+            Ok(ParsedProperty::Fill(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "fill-opacity") =>
             Ok(ParsedProperty::FillOpacity(parse_input_to_parse_error(input)?)),
@@ -332,7 +332,7 @@ pub fn parse_property<'i>(prop_name: &QualName, input: &mut Parser<'i, '_>, acce
             Ok(ParsedProperty::StopOpacity(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "stroke") =>
-            Ok(ParsedProperty::Stroke(parse_input(input)?)),
+            Ok(ParsedProperty::Stroke(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "stroke-dasharray") =>
             Ok(ParsedProperty::StrokeDasharray(parse_input(input)?)),
diff --git a/rsvg_internals/src/property_defs.rs b/rsvg_internals/src/property_defs.rs
index 454ffc29..9b3f3f9a 100644
--- a/rsvg_internals/src/property_defs.rs
+++ b/rsvg_internals/src/property_defs.rs
@@ -164,9 +164,9 @@ make_property!(
 make_property!(
     ComputedValues,
     Fill,
-    default: PaintServer::parse_str("#000").unwrap(),
+    default: PaintServer::parse_str_to_parse_error("#000").unwrap(),
     inherits_automatically: true,
-    newtype_parse: PaintServer,
+    newtype_parse_to_parse_error: PaintServer,
 );
 
 // https://www.w3.org/TR/SVG/painting.html#FillOpacityProperty
@@ -433,7 +433,7 @@ make_property!(
     Stroke,
     default: PaintServer::None,
     inherits_automatically: true,
-    newtype_parse: PaintServer,
+    newtype_parse_to_parse_error: PaintServer,
 );
 
 // https://www.w3.org/TR/SVG/painting.html#StrokeDasharrayProperty


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