[librsvg: 48/51] Input::FilterOutput(x) make x a CustomIdent, not a String



commit 67d4014b4231d5f6ffbd8d2e89a9cf8c1d23f21f
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Dec 18 13:41:30 2019 -0600

    Input::FilterOutput(x) make x a CustomIdent, not a String
    
    This strongly-types the identifiers for filter results.

 rsvg_internals/src/filters/blend.rs            |  2 +-
 rsvg_internals/src/filters/composite.rs        |  2 +-
 rsvg_internals/src/filters/context.rs          |  6 ++---
 rsvg_internals/src/filters/displacement_map.rs |  2 +-
 rsvg_internals/src/filters/input.rs            | 35 +++++++++++++++-----------
 rsvg_internals/src/filters/merge.rs            |  3 ++-
 rsvg_internals/src/filters/mod.rs              |  8 +++---
 7 files changed, 32 insertions(+), 26 deletions(-)
---
diff --git a/rsvg_internals/src/filters/blend.rs b/rsvg_internals/src/filters/blend.rs
index 54aa826e..2b05cdb0 100755
--- a/rsvg_internals/src/filters/blend.rs
+++ b/rsvg_internals/src/filters/blend.rs
@@ -62,7 +62,7 @@ impl NodeTrait for FeBlend {
         for (attr, value) in pbag.iter() {
             match attr.expanded() {
                 expanded_name!(svg "in2") => {
-                    self.in2 = Some(Input::parse(attr, value)?);
+                    self.in2 = Some(attr.parse(value)?);
                 }
                 expanded_name!(svg "mode") => self.mode = attr.parse(value)?,
                 _ => (),
diff --git a/rsvg_internals/src/filters/composite.rs b/rsvg_internals/src/filters/composite.rs
index 738f074f..ecdbd60d 100644
--- a/rsvg_internals/src/filters/composite.rs
+++ b/rsvg_internals/src/filters/composite.rs
@@ -66,7 +66,7 @@ impl NodeTrait for FeComposite {
 
         for (attr, value) in pbag.iter() {
             match attr.expanded() {
-                expanded_name!(svg "in2") => self.in2 = Some(Input::parse(attr, value)?),
+                expanded_name!(svg "in2") => self.in2 = Some(attr.parse(value)?),
                 expanded_name!(svg "operator") => self.operator = attr.parse(value)?,
                 expanded_name!(svg "k1") => self.k1 = parsers::number(value).attribute(attr)?,
                 expanded_name!(svg "k2") => self.k2 = parsers::number(value).attribute(attr)?,
diff --git a/rsvg_internals/src/filters/context.rs b/rsvg_internals/src/filters/context.rs
index a10b3074..af1459bc 100644
--- a/rsvg_internals/src/filters/context.rs
+++ b/rsvg_internals/src/filters/context.rs
@@ -16,7 +16,7 @@ use crate::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
 use crate::unit_interval::UnitInterval;
 
 use super::error::FilterError;
-use super::input::Input;
+use super::input::{CustomIdent, Input};
 
 /// A filter primitive output.
 #[derive(Debug, Clone)]
@@ -32,7 +32,7 @@ pub struct FilterOutput {
 #[derive(Debug, Clone)]
 pub struct FilterResult {
     /// The name of this result: the value of the `result` attribute.
-    pub name: Option<String>,
+    pub name: Option<CustomIdent>,
 
     /// The output.
     pub output: FilterOutput,
@@ -60,7 +60,7 @@ pub struct FilterContext {
     /// Output of the last filter primitive.
     last_result: Option<FilterOutput>,
     /// Surfaces of the previous filter primitives by name.
-    previous_results: HashMap<String, FilterOutput>,
+    previous_results: HashMap<CustomIdent, FilterOutput>,
     /// The background surface. Computed lazily.
     background_surface: RefCell<Option<Result<SharedImageSurface, FilterError>>>,
     /// The filter effects region.
diff --git a/rsvg_internals/src/filters/displacement_map.rs b/rsvg_internals/src/filters/displacement_map.rs
index 65ae2939..afeadaa7 100644
--- a/rsvg_internals/src/filters/displacement_map.rs
+++ b/rsvg_internals/src/filters/displacement_map.rs
@@ -52,7 +52,7 @@ impl NodeTrait for FeDisplacementMap {
 
         for (attr, value) in pbag.iter() {
             match attr.expanded() {
-                expanded_name!(svg "in2") => self.in2 = Some(Input::parse(attr, value)?),
+                expanded_name!(svg "in2") => self.in2 = Some(attr.parse(value)?),
                 expanded_name!(svg "scale") => {
                     self.scale = parsers::number(value).attribute(attr)?
                 }
diff --git a/rsvg_internals/src/filters/input.rs b/rsvg_internals/src/filters/input.rs
index 8ced997e..ed164269 100644
--- a/rsvg_internals/src/filters/input.rs
+++ b/rsvg_internals/src/filters/input.rs
@@ -1,5 +1,4 @@
 use cssparser::{Parser, Token};
-use markup5ever::QualName;
 
 use crate::error::*;
 use crate::parsers::Parse;
@@ -13,25 +12,31 @@ pub enum Input {
     BackgroundAlpha,
     FillPaint,
     StrokePaint,
-    FilterOutput(String),
+    FilterOutput(CustomIdent),
 }
 
 /// https://www.w3.org/TR/css-values-4/#custom-idents
-#[derive(Debug, PartialEq)]
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub struct CustomIdent(String);
 
-impl Input {
-    pub fn parse(attr: QualName, s: &str) -> Result<Input, NodeError> {
-        match s {
-            "SourceGraphic" => Ok(Input::SourceGraphic),
-            "SourceAlpha" => Ok(Input::SourceAlpha),
-            "BackgroundImage" => Ok(Input::BackgroundImage),
-            "BackgroundAlpha" => Ok(Input::BackgroundAlpha),
-            "FillPaint" => Ok(Input::FillPaint),
-            "StrokePaint" => Ok(Input::StrokePaint),
-            s if !s.is_empty() => Ok(Input::FilterOutput(s.to_string())),
-            _ => Err(ValueErrorKind::parse_error("invalid value")).attribute(attr),
-        }
+impl Parse for Input {
+    fn parse(parser: &mut Parser<'_, '_>) -> Result<Self, ValueErrorKind> {
+        parser
+            .try_parse(|p| {
+                parse_identifiers!(
+                    p,
+                    "SourceGraphic" => Input::SourceGraphic,
+                    "SourceAlpha" => Input::SourceAlpha,
+                    "BackgroundImage" => Input::BackgroundImage,
+                    "BackgroundAlpha" => Input::BackgroundAlpha,
+                    "FillPaint" => Input::FillPaint,
+                    "StrokePaint" => Input::StrokePaint,
+                ).map_err(|_| ValueErrorKind::parse_error("parse error"))
+            })
+            .or_else(|_| {
+                let ident = CustomIdent::parse(parser)?;
+                Ok(Input::FilterOutput(ident))
+            })
     }
 }
 
diff --git a/rsvg_internals/src/filters/merge.rs b/rsvg_internals/src/filters/merge.rs
index de40fc18..eb47e735 100644
--- a/rsvg_internals/src/filters/merge.rs
+++ b/rsvg_internals/src/filters/merge.rs
@@ -3,6 +3,7 @@ use markup5ever::{expanded_name, local_name, namespace_url, ns};
 
 use crate::drawing_ctx::DrawingCtx;
 use crate::node::{NodeResult, NodeTrait, NodeType, RsvgNode};
+use crate::parsers::ParseValue;
 use crate::property_bag::PropertyBag;
 use crate::rect::IRect;
 use crate::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
@@ -46,7 +47,7 @@ impl NodeTrait for FeMergeNode {
     fn set_atts(&mut self, _parent: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
         for (attr, value) in pbag.iter() {
             match attr.expanded() {
-                expanded_name!(svg "in") => self.in_ = Some(Input::parse(attr, value)?),
+                expanded_name!(svg "in") => self.in_ = Some(attr.parse(value)?),
                 _ => (),
             }
         }
diff --git a/rsvg_internals/src/filters/mod.rs b/rsvg_internals/src/filters/mod.rs
index a0417cbe..49c67c85 100644
--- a/rsvg_internals/src/filters/mod.rs
+++ b/rsvg_internals/src/filters/mod.rs
@@ -29,7 +29,7 @@ mod error;
 use self::error::FilterError;
 
 mod input;
-use self::input::Input;
+use self::input::{CustomIdent, Input};
 
 /// A filter primitive interface.
 pub trait FilterEffect: NodeTrait {
@@ -82,7 +82,7 @@ struct Primitive {
     y: Option<Length<Vertical>>,
     width: Option<Length<Horizontal>>,
     height: Option<Length<Vertical>>,
-    result: Option<String>,
+    result: Option<CustomIdent>,
 }
 
 /// The base node for filter primitives which accept input.
@@ -182,7 +182,7 @@ impl NodeTrait for Primitive {
                             check_units_vertical_and_ensure_nonnegative,
                         )?)
                 }
-                expanded_name!(svg "result") => self.result = Some(value.to_string()),
+                expanded_name!(svg "result") => self.result = Some(attr.parse(value)?),
                 _ => (),
             }
         }
@@ -218,7 +218,7 @@ impl NodeTrait for PrimitiveWithInput {
 
         for (attr, value) in pbag.iter() {
             match attr.expanded() {
-                expanded_name!(svg "in") => drop(self.in_ = Some(Input::parse(attr, value)?)),
+                expanded_name!(svg "in") => self.in_ = Some(attr.parse(value)?),
                 _ => (),
             }
         }


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