[librsvg: 3/8] filters - replace Option<Input> with an Input::Unspecified variant




commit 87f3c6011f044079f9bf3d83aaf2131aadc455ca
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Mar 10 17:23:20 2021 -0600

    filters - replace Option<Input> with an Input::Unspecified variant
    
    This will make it easier to impl Default for all filter primitives.

 src/filters/blend.rs            |  6 +++---
 src/filters/composite.rs        |  6 +++---
 src/filters/context.rs          | 24 ++++++++++++------------
 src/filters/displacement_map.rs |  6 +++---
 src/filters/merge.rs            | 19 ++++++++++---------
 src/filters/mod.rs              | 22 +++++++++++++++-------
 6 files changed, 46 insertions(+), 37 deletions(-)
---
diff --git a/src/filters/blend.rs b/src/filters/blend.rs
index f93329f2..df94721d 100755
--- a/src/filters/blend.rs
+++ b/src/filters/blend.rs
@@ -38,7 +38,7 @@ enum_default!(Mode, Mode::Normal);
 /// The `feBlend` filter primitive.
 pub struct FeBlend {
     base: PrimitiveWithInput,
-    in2: Option<Input>,
+    in2: Input,
     mode: Mode,
 }
 
@@ -48,7 +48,7 @@ impl Default for FeBlend {
     fn default() -> FeBlend {
         FeBlend {
             base: PrimitiveWithInput::new(),
-            in2: None,
+            in2: Default::default(),
             mode: Mode::default(),
         }
     }
@@ -79,7 +79,7 @@ impl FilterRender for FeBlend {
         draw_ctx: &mut DrawingCtx,
     ) -> Result<FilterResult, FilterError> {
         let input = self.base.get_input(ctx, acquired_nodes, draw_ctx)?;
-        let input_2 = ctx.get_input(acquired_nodes, draw_ctx, self.in2.as_ref())?;
+        let input_2 = ctx.get_input(acquired_nodes, draw_ctx, &self.in2)?;
         let bounds = self
             .base
             .get_bounds(ctx)?
diff --git a/src/filters/composite.rs b/src/filters/composite.rs
index 7bd10314..1d081b6d 100644
--- a/src/filters/composite.rs
+++ b/src/filters/composite.rs
@@ -26,7 +26,7 @@ enum Operator {
 /// The `feComposite` filter primitive.
 pub struct FeComposite {
     base: PrimitiveWithInput,
-    in2: Option<Input>,
+    in2: Input,
     operator: Operator,
     k1: f64,
     k2: f64,
@@ -40,7 +40,7 @@ impl Default for FeComposite {
     fn default() -> FeComposite {
         FeComposite {
             base: PrimitiveWithInput::new(),
-            in2: None,
+            in2: Default::default(),
             operator: Operator::Over,
             k1: 0.0,
             k2: 0.0,
@@ -79,7 +79,7 @@ impl FilterRender for FeComposite {
         draw_ctx: &mut DrawingCtx,
     ) -> Result<FilterResult, FilterError> {
         let input = self.base.get_input(ctx, acquired_nodes, draw_ctx)?;
-        let input_2 = ctx.get_input(acquired_nodes, draw_ctx, self.in2.as_ref())?;
+        let input_2 = ctx.get_input(acquired_nodes, draw_ctx, &self.in2)?;
         let bounds = self
             .base
             .get_bounds(ctx)?
diff --git a/src/filters/context.rs b/src/filters/context.rs
index e9787b26..c5bf35be 100644
--- a/src/filters/context.rs
+++ b/src/filters/context.rs
@@ -316,20 +316,20 @@ impl FilterContext {
         &self,
         acquired_nodes: &mut AcquiredNodes<'_>,
         draw_ctx: &mut DrawingCtx,
-        in_: Option<&Input>,
+        in_: &Input,
     ) -> Result<FilterInput, FilterError> {
-        if in_.is_none() {
-            // No value => use the last result.
-            // As per the SVG spec, if the filter primitive is the first in the chain, return the
-            // source graphic.
-            if let Some(output) = self.last_result.as_ref() {
-                return Ok(FilterInput::PrimitiveOutput(output.clone()));
-            } else {
-                return Ok(FilterInput::StandardInput(self.source_graphic().clone()));
+        match *in_ {
+            Input::Unspecified => {
+                // No value => use the last result.
+                // As per the SVG spec, if the filter primitive is the first in the chain, return the
+                // source graphic.
+                if let Some(output) = self.last_result.as_ref() {
+                    Ok(FilterInput::PrimitiveOutput(output.clone()))
+                } else {
+                    Ok(FilterInput::StandardInput(self.source_graphic().clone()))
+                }
             }
-        }
 
-        match *in_.unwrap() {
             Input::SourceGraphic => Ok(FilterInput::StandardInput(self.source_graphic().clone())),
 
             Input::SourceAlpha => self
@@ -373,7 +373,7 @@ impl FilterContext {
         &self,
         acquired_nodes: &mut AcquiredNodes<'_>,
         draw_ctx: &mut DrawingCtx,
-        in_: Option<&Input>,
+        in_: &Input,
     ) -> Result<FilterInput, FilterError> {
         let raw = self.get_input_raw(acquired_nodes, draw_ctx, in_)?;
 
diff --git a/src/filters/displacement_map.rs b/src/filters/displacement_map.rs
index 0498c66a..ddae001e 100644
--- a/src/filters/displacement_map.rs
+++ b/src/filters/displacement_map.rs
@@ -25,7 +25,7 @@ enum ColorChannel {
 /// The `feDisplacementMap` filter primitive.
 pub struct FeDisplacementMap {
     base: PrimitiveWithInput,
-    in2: Option<Input>,
+    in2: Input,
     scale: f64,
     x_channel_selector: ColorChannel,
     y_channel_selector: ColorChannel,
@@ -37,7 +37,7 @@ impl Default for FeDisplacementMap {
     fn default() -> FeDisplacementMap {
         FeDisplacementMap {
             base: PrimitiveWithInput::new(),
-            in2: None,
+            in2: Default::default(),
             scale: 0.0,
             x_channel_selector: ColorChannel::A,
             y_channel_selector: ColorChannel::A,
@@ -76,7 +76,7 @@ impl FilterRender for FeDisplacementMap {
         draw_ctx: &mut DrawingCtx,
     ) -> Result<FilterResult, FilterError> {
         let input = self.base.get_input(ctx, acquired_nodes, draw_ctx)?;
-        let displacement_input = ctx.get_input(acquired_nodes, draw_ctx, self.in2.as_ref())?;
+        let displacement_input = ctx.get_input(acquired_nodes, draw_ctx, &self.in2)?;
         let bounds = self
             .base
             .get_bounds(ctx)?
diff --git a/src/filters/merge.rs b/src/filters/merge.rs
index dbfcb07c..478a6510 100644
--- a/src/filters/merge.rs
+++ b/src/filters/merge.rs
@@ -20,7 +20,7 @@ pub struct FeMerge {
 /// The `<feMergeNode>` element.
 #[derive(Clone, Debug, Default, PartialEq)]
 pub struct FeMergeNode {
-    in1: Option<Input>,
+    in1: Input,
 }
 
 impl Default for FeMerge {
@@ -42,10 +42,11 @@ impl SetAttributes for FeMerge {
 impl SetAttributes for FeMergeNode {
     #[inline]
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
-        self.in1 = attrs
-            .iter()
-            .find(|(attr, _)| attr.expanded() == expanded_name!("", "in"))
-            .and_then(|(attr, value)| attr.parse(value).ok());
+        for (attr, value) in attrs.iter() {
+            if let expanded_name!("", "in") = attr.expanded() {
+                self.in1 = attr.parse(value)?;
+            }
+        }
 
         Ok(())
     }
@@ -62,7 +63,7 @@ impl FeMergeNode {
         bounds: IRect,
         output_surface: Option<SharedImageSurface>,
     ) -> Result<SharedImageSurface, FilterError> {
-        let input = ctx.get_input(acquired_nodes, draw_ctx, self.in1.as_ref())?;
+        let input = ctx.get_input(acquired_nodes, draw_ctx, &self.in1)?;
 
         if output_surface.is_none() {
             return Ok(input.surface().clone());
@@ -88,7 +89,7 @@ impl FilterRender for FeMerge {
         // Compute the filter bounds, taking each feMergeNode's input into account.
         let mut bounds = self.base.get_bounds(ctx)?;
         for merge_node in &parameters {
-            let input = ctx.get_input(acquired_nodes, draw_ctx, merge_node.in1.as_ref())?;
+            let input = ctx.get_input(acquired_nodes, draw_ctx, &merge_node.in1)?;
             bounds = bounds.add_input(&input);
         }
 
@@ -172,10 +173,10 @@ mod tests {
             &params[..],
             vec![
                 FeMergeNode {
-                    in1: Some(Input::SourceGraphic)
+                    in1: Input::SourceGraphic
                 },
                 FeMergeNode {
-                    in1: Some(Input::SourceAlpha)
+                    in1: Input::SourceAlpha
                 },
             ]
         );
diff --git a/src/filters/mod.rs b/src/filters/mod.rs
index 1c08c3a9..d6286720 100644
--- a/src/filters/mod.rs
+++ b/src/filters/mod.rs
@@ -85,6 +85,7 @@ struct Primitive {
 /// An enumeration of possible inputs for a filter primitive.
 #[derive(Debug, Clone, Eq, PartialEq, Hash)]
 pub enum Input {
+    Unspecified,
     SourceGraphic,
     SourceAlpha,
     BackgroundImage,
@@ -94,6 +95,12 @@ pub enum Input {
     FilterOutput(CustomIdent),
 }
 
+impl Default for Input {
+    fn default() -> Self {
+        Input::Unspecified
+    }
+}
+
 impl Parse for Input {
     fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> {
         parser
@@ -118,7 +125,7 @@ impl Parse for Input {
 /// The base node for filter primitives which accept input.
 struct PrimitiveWithInput {
     base: Primitive,
-    in1: Option<Input>,
+    in1: Input,
 }
 
 impl Primitive {
@@ -195,7 +202,7 @@ impl PrimitiveWithInput {
     fn new() -> PrimitiveWithInput {
         PrimitiveWithInput {
             base: Primitive::new(),
-            in1: None,
+            in1: Default::default(),
         }
     }
 
@@ -207,7 +214,7 @@ impl PrimitiveWithInput {
         acquired_nodes: &mut AcquiredNodes<'_>,
         draw_ctx: &mut DrawingCtx,
     ) -> Result<FilterInput, FilterError> {
-        ctx.get_input(acquired_nodes, draw_ctx, self.in1.as_ref())
+        ctx.get_input(acquired_nodes, draw_ctx, &self.in1)
     }
 }
 
@@ -215,10 +222,11 @@ impl SetAttributes for PrimitiveWithInput {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
         self.base.set_attributes(attrs)?;
 
-        self.in1 = attrs
-            .iter()
-            .find(|(attr, _)| attr.expanded() == expanded_name!("", "in"))
-            .and_then(|(attr, value)| attr.parse(value).ok());
+        for (attr, value) in attrs.iter() {
+            if let expanded_name!("", "in") = attr.expanded() {
+                self.in1 = attr.parse(value)?;
+            }
+        }
 
         Ok(())
     }


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