[librsvg] Remove property_bag::length_or_*(); use parse_or_() instead



commit 9a2a5e516281ca99bb53a0d194fc970e4c12136f
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Sep 4 21:29:06 2017 -0500

    Remove property_bag::length_or_*(); use parse_or_() instead
    
    Now that we have impl Parse for RsvgLength, this works.

 rust/src/gradient.rs     |   18 +++++++++---------
 rust/src/marker.rs       |   28 ++++++++++++++--------------
 rust/src/pattern.rs      |    8 ++++----
 rust/src/property_bag.rs |   31 -------------------------------
 rust/src/shapes.rs       |   40 ++++++++++++++++++++--------------------
 rust/src/stop.rs         |    2 +-
 rust/src/structure.rs    |   16 ++++++++--------
 7 files changed, 56 insertions(+), 87 deletions(-)
---
diff --git a/rust/src/gradient.rs b/rust/src/gradient.rs
index d5a7885..3150fc7 100644
--- a/rust/src/gradient.rs
+++ b/rust/src/gradient.rs
@@ -568,20 +568,20 @@ impl NodeTrait for NodeGradient {
         match node.get_type () {
             NodeType::LinearGradient => {
                 g.variant = GradientVariant::Linear {
-                    x1: property_bag::length_or_none (pbag, "x1", LengthDir::Horizontal)?,
-                    y1: property_bag::length_or_none (pbag, "y1", LengthDir::Vertical)?,
-                    x2: property_bag::length_or_none (pbag, "x2", LengthDir::Horizontal)?,
-                    y2: property_bag::length_or_none (pbag, "y2", LengthDir::Vertical)?
+                    x1: property_bag::parse_or_none (pbag, "x1", LengthDir::Horizontal)?,
+                    y1: property_bag::parse_or_none (pbag, "y1", LengthDir::Vertical)?,
+                    x2: property_bag::parse_or_none (pbag, "x2", LengthDir::Horizontal)?,
+                    y2: property_bag::parse_or_none (pbag, "y2", LengthDir::Vertical)?
                 };
             },
 
             NodeType::RadialGradient => {
                 g.variant = GradientVariant::Radial {
-                    cx: property_bag::length_or_none (pbag, "cx", LengthDir::Horizontal)?,
-                    cy: property_bag::length_or_none (pbag, "cy", LengthDir::Vertical)?,
-                    r:  property_bag::length_or_none (pbag, "r",  LengthDir::Both)?,
-                    fx: property_bag::length_or_none (pbag, "fx", LengthDir::Horizontal)?,
-                    fy: property_bag::length_or_none (pbag, "fy", LengthDir::Vertical)?
+                    cx: property_bag::parse_or_none (pbag, "cx", LengthDir::Horizontal)?,
+                    cy: property_bag::parse_or_none (pbag, "cy", LengthDir::Vertical)?,
+                    r:  property_bag::parse_or_none (pbag, "r",  LengthDir::Both)?,
+                    fx: property_bag::parse_or_none (pbag, "fx", LengthDir::Horizontal)?,
+                    fy: property_bag::parse_or_none (pbag, "fy", LengthDir::Vertical)?
                 };
             },
 
diff --git a/rust/src/marker.rs b/rust/src/marker.rs
index 927455c..f4c0164 100644
--- a/rust/src/marker.rs
+++ b/rust/src/marker.rs
@@ -97,17 +97,17 @@ impl NodeMarker {
             units:  Cell::new (MarkerUnits::default ()),
             ref_x:  Cell::new (RsvgLength::default ()),
             ref_y:  Cell::new (RsvgLength::default ()),
-            width:  Cell::new (RsvgLength::parse (NodeMarker::get_default_size (), 
LengthDir::Horizontal).unwrap ()),
-            height: Cell::new (RsvgLength::parse (NodeMarker::get_default_size (), 
LengthDir::Vertical).unwrap ()),
+            width:  Cell::new (NodeMarker::get_default_size (LengthDir::Horizontal)),
+            height: Cell::new (NodeMarker::get_default_size (LengthDir::Vertical)),
             orient: Cell::new (MarkerOrient::default ()),
             aspect: Cell::new (AspectRatio::default ()),
             vbox:   Cell::new (None)
         }
     }
 
-    fn get_default_size () -> &'static str {
+    fn get_default_size (dir: LengthDir) -> RsvgLength {
         // per the spec
-        "3"
+        RsvgLength::parse ("3", dir).unwrap()
     }
 
     fn render (&self,
@@ -194,20 +194,20 @@ impl NodeTrait for NodeMarker {
     fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) -> NodeResult {
         self.units.set (property_bag::parse_or_default (pbag, "markerUnits", ())?);
 
-        self.ref_x.set (property_bag::length_or_default (pbag, "refX", LengthDir::Horizontal)?);
-        self.ref_y.set (property_bag::length_or_default (pbag, "refY", LengthDir::Vertical)?);
+        self.ref_x.set (property_bag::parse_or_default (pbag, "refX", LengthDir::Horizontal)?);
+        self.ref_y.set (property_bag::parse_or_default (pbag, "refY", LengthDir::Vertical)?);
 
-        self.width.set (property_bag::length_or_value (pbag, "markerWidth",
-                                                       LengthDir::Horizontal,
-                                                       NodeMarker::get_default_size ())?
+        self.width.set (property_bag::parse_or_value (pbag, "markerWidth",
+                                                      LengthDir::Horizontal,
+                                                      NodeMarker::get_default_size (LengthDir::Horizontal))?
                         .check_nonnegative ()
                         .map_err (|e| NodeError::attribute_error ("markerWidth", e))?);
 
-        self.height.set (property_bag::length_or_value (pbag, "markerHeight",
-                                                        LengthDir::Vertical,
-                                                        NodeMarker::get_default_size ())?
-                        .check_nonnegative ()
-                        .map_err (|e| NodeError::attribute_error ("markerHeight", e))?);
+        self.height.set (property_bag::parse_or_value (pbag, "markerHeight",
+                                                       LengthDir::Vertical,
+                                                       NodeMarker::get_default_size (LengthDir::Vertical))?
+                         .check_nonnegative ()
+                         .map_err (|e| NodeError::attribute_error ("markerHeight", e))?);
 
         self.orient.set (property_bag::parse_or_default (pbag, "orient", ())?);
         self.aspect.set (property_bag::parse_or_default (pbag, "preserveAspectRatio", ())?);
diff --git a/rust/src/pattern.rs b/rust/src/pattern.rs
index 67a0eb6..fdf7dce 100644
--- a/rust/src/pattern.rs
+++ b/rust/src/pattern.rs
@@ -209,10 +209,10 @@ impl NodeTrait for NodePattern {
 
         p.fallback = property_bag::lookup (pbag, "xlink:href");
 
-        p.x      = property_bag::length_or_none (pbag, "x", LengthDir::Horizontal)?;
-        p.y      = property_bag::length_or_none (pbag, "y", LengthDir::Vertical)?;
-        p.width  = property_bag::length_or_none (pbag, "width", LengthDir::Horizontal)?;
-        p.height = property_bag::length_or_none (pbag, "height", LengthDir::Vertical)?;
+        p.x      = property_bag::parse_or_none (pbag, "x", LengthDir::Horizontal)?;
+        p.y      = property_bag::parse_or_none (pbag, "y", LengthDir::Vertical)?;
+        p.width  = property_bag::parse_or_none (pbag, "width", LengthDir::Horizontal)?;
+        p.height = property_bag::parse_or_none (pbag, "height", LengthDir::Vertical)?;
 
         Ok (())
     }
diff --git a/rust/src/property_bag.rs b/rust/src/property_bag.rs
index 88bf484..72b50e9 100644
--- a/rust/src/property_bag.rs
+++ b/rust/src/property_bag.rs
@@ -34,37 +34,6 @@ pub fn free (pbag: *mut RsvgPropertyBag) {
     }
 }
 
-pub fn length_or_none (pbag: *const RsvgPropertyBag, key: &'static str, length_dir: LengthDir) -> Result 
<Option<RsvgLength>, NodeError> {
-    let value = lookup (pbag, key);
-
-    if let Some (v) = value {
-        RsvgLength::parse (&v, length_dir).map (|l| Some (l))
-            .map_err (|e| NodeError::attribute_error (key, e))
-    } else {
-        Ok (None)
-    }
-}
-
-pub fn length_or_default (pbag: *const RsvgPropertyBag, key: &'static str, length_dir: LengthDir) -> Result 
<RsvgLength, NodeError> {
-    let r = length_or_none (pbag, key, length_dir);
-
-    match r {
-        Ok (Some (v)) => Ok (v),
-        Ok (None)     => Ok (RsvgLength::default ()),
-        Err (e)       => Err (e)
-    }
-}
-
-pub fn length_or_value (pbag: *const RsvgPropertyBag, key: &'static str, length_dir: LengthDir, length_str: 
&str) -> Result <RsvgLength, NodeError> {
-    let r = length_or_none (pbag, key, length_dir);
-
-    match r {
-        Ok (Some (v)) => Ok (v),
-        Ok (None)     => Ok (RsvgLength::parse (length_str, length_dir).unwrap ()),
-        Err (e)       => Err (e)
-    }
-}
-
 pub fn parse_or_none<T> (pbag: *const RsvgPropertyBag, key: &'static str, data: <T as Parse>::Data) -> 
Result <Option<T>, NodeError>
     where T: Parse<Err = AttributeError>
 {
diff --git a/rust/src/shapes.rs b/rust/src/shapes.rs
index 97a773b..360e886 100644
--- a/rust/src/shapes.rs
+++ b/rust/src/shapes.rs
@@ -201,10 +201,10 @@ impl NodeLine {
 
 impl NodeTrait for NodeLine {
     fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) -> NodeResult {
-        self.x1.set (property_bag::length_or_default (pbag, "x1", LengthDir::Horizontal)?);
-        self.y1.set (property_bag::length_or_default (pbag, "y1", LengthDir::Vertical)?);
-        self.x2.set (property_bag::length_or_default (pbag, "x2", LengthDir::Horizontal)?);
-        self.y2.set (property_bag::length_or_default (pbag, "y2", LengthDir::Vertical)?);
+        self.x1.set (property_bag::parse_or_default (pbag, "x1", LengthDir::Horizontal)?);
+        self.y1.set (property_bag::parse_or_default (pbag, "y1", LengthDir::Vertical)?);
+        self.x2.set (property_bag::parse_or_default (pbag, "x2", LengthDir::Horizontal)?);
+        self.y2.set (property_bag::parse_or_default (pbag, "y2", LengthDir::Vertical)?);
 
         Ok (())
     }
@@ -258,13 +258,13 @@ impl NodeRect {
 
 impl NodeTrait for NodeRect {
     fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) -> NodeResult {
-        self.x.set (property_bag::length_or_default (pbag, "x", LengthDir::Horizontal)?);
-        self.y.set (property_bag::length_or_default (pbag, "y", LengthDir::Vertical)?);
-        self.w.set (property_bag::length_or_default (pbag, "width", LengthDir::Horizontal)?);
-        self.h.set (property_bag::length_or_default (pbag, "height", LengthDir::Vertical)?);
+        self.x.set (property_bag::parse_or_default (pbag, "x", LengthDir::Horizontal)?);
+        self.y.set (property_bag::parse_or_default (pbag, "y", LengthDir::Vertical)?);
+        self.w.set (property_bag::parse_or_default (pbag, "width", LengthDir::Horizontal)?);
+        self.h.set (property_bag::parse_or_default (pbag, "height", LengthDir::Vertical)?);
 
-        self.rx.set (property_bag::length_or_none (pbag, "rx", LengthDir::Horizontal)?);
-        self.ry.set (property_bag::length_or_none (pbag, "ry", LengthDir::Vertical)?);
+        self.rx.set (property_bag::parse_or_none (pbag, "rx", LengthDir::Horizontal)?);
+        self.ry.set (property_bag::parse_or_none (pbag, "ry", LengthDir::Vertical)?);
 
         Ok (())
     }
@@ -433,11 +433,11 @@ impl NodeCircle {
 
 impl NodeTrait for NodeCircle {
     fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) -> NodeResult {
-        self.cx.set (property_bag::length_or_default (pbag, "cx", LengthDir::Horizontal)?);
-        self.cy.set (property_bag::length_or_default (pbag, "cy", LengthDir::Vertical)?);
+        self.cx.set (property_bag::parse_or_default (pbag, "cx", LengthDir::Horizontal)?);
+        self.cy.set (property_bag::parse_or_default (pbag, "cy", LengthDir::Vertical)?);
 
-        self.r.set  (property_bag::length_or_default (pbag, "r", LengthDir::Both)
-                     .and_then (|l| l.check_nonnegative ()
+        self.r.set  (property_bag::parse_or_default (pbag, "r", LengthDir::Both)
+                     .and_then (|l: RsvgLength| l.check_nonnegative ()
                                 .map_err (|e| NodeError::attribute_error ("r", e)))?);
 
         Ok (())
@@ -478,15 +478,15 @@ impl NodeEllipse {
 
 impl NodeTrait for NodeEllipse {
     fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) -> NodeResult {
-        self.cx.set (property_bag::length_or_default (pbag, "cx", LengthDir::Horizontal)?);
-        self.cy.set (property_bag::length_or_default (pbag, "cy", LengthDir::Vertical)?);
+        self.cx.set (property_bag::parse_or_default (pbag, "cx", LengthDir::Horizontal)?);
+        self.cy.set (property_bag::parse_or_default (pbag, "cy", LengthDir::Vertical)?);
 
-        self.rx.set (property_bag::length_or_default (pbag, "rx", LengthDir::Horizontal)
-                     .and_then (|l| l.check_nonnegative ()
+        self.rx.set (property_bag::parse_or_default (pbag, "rx", LengthDir::Horizontal)
+                     .and_then (|l: RsvgLength| l.check_nonnegative ()
                                 .map_err (|e| NodeError::attribute_error ("rx", e)))?);
 
-        self.ry.set (property_bag::length_or_default (pbag, "ry", LengthDir::Vertical)
-                     .and_then (|l| l.check_nonnegative ()
+        self.ry.set (property_bag::parse_or_default (pbag, "ry", LengthDir::Vertical)
+                     .and_then (|l: RsvgLength| l.check_nonnegative ()
                                 .map_err (|e| NodeError::attribute_error ("ry", e)))?);
 
         Ok (())
diff --git a/rust/src/stop.rs b/rust/src/stop.rs
index e05218f..780b8ea 100644
--- a/rust/src/stop.rs
+++ b/rust/src/stop.rs
@@ -40,7 +40,7 @@ impl NodeStop {
 
 impl NodeTrait for NodeStop {
     fn set_atts (&self, node: &RsvgNode, handle: *const RsvgHandle, pbag: *const RsvgPropertyBag) -> 
NodeResult {
-        let offset_length = property_bag::length_or_default (pbag, "offset", LengthDir::Both)?;
+        let offset_length: RsvgLength = property_bag::parse_or_default (pbag, "offset", LengthDir::Both)?;
         match offset_length.unit {
             LengthUnit::Default |
             LengthUnit::Percent => {
diff --git a/rust/src/structure.rs b/rust/src/structure.rs
index 19c5b19..fea1e41 100644
--- a/rust/src/structure.rs
+++ b/rust/src/structure.rs
@@ -141,15 +141,15 @@ impl NodeTrait for NodeSvg {
         // x & y attributes have no effect on outermost svg
         // http://www.w3.org/TR/SVG/struct.html#SVGElement
         if node.get_parent ().is_some () {
-            self.x.set (property_bag::length_or_default (pbag, "x", LengthDir::Horizontal)?);
-            self.y.set (property_bag::length_or_default (pbag, "y", LengthDir::Vertical)?);
+            self.x.set (property_bag::parse_or_default (pbag, "x", LengthDir::Horizontal)?);
+            self.y.set (property_bag::parse_or_default (pbag, "y", LengthDir::Vertical)?);
         }
 
-        self.w.set (property_bag::length_or_value (pbag, "width", LengthDir::Horizontal, "100%")
+        self.w.set (property_bag::parse_or_value (pbag, "width", LengthDir::Horizontal, RsvgLength::parse 
("100%", LengthDir::Horizontal).unwrap ())
                     .and_then (|l| l.check_nonnegative ()
                                .map_err (|e| NodeError::attribute_error ("width", e)))?);
 
-        self.h.set (property_bag::length_or_value (pbag, "height", LengthDir::Vertical, "100%")
+        self.h.set (property_bag::parse_or_value (pbag, "height", LengthDir::Vertical, RsvgLength::parse 
("100%", LengthDir::Vertical).unwrap ())
                     .and_then (|l| l.check_nonnegative ()
                                .map_err (|e| NodeError::attribute_error ("height", e)))?);
 
@@ -265,10 +265,10 @@ impl NodeTrait for NodeUse {
     fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) -> NodeResult {
         *self.link.borrow_mut () = property_bag::lookup (pbag, "xlink:href");
 
-        self.x.set (property_bag::length_or_default (pbag, "x", LengthDir::Horizontal)?);
-        self.y.set (property_bag::length_or_default (pbag, "y", LengthDir::Vertical)?);
+        self.x.set (property_bag::parse_or_default (pbag, "x", LengthDir::Horizontal)?);
+        self.y.set (property_bag::parse_or_default (pbag, "y", LengthDir::Vertical)?);
 
-        let opt_w = property_bag::length_or_none (pbag, "width", LengthDir::Horizontal)?;
+        let opt_w: Option<RsvgLength> = property_bag::parse_or_none (pbag, "width", LengthDir::Horizontal)?;
         self.w.set (match opt_w {
             Some (w) => {
                 Some (w.check_nonnegative ().map_err (|e| NodeError::attribute_error ("width", e))?)
@@ -279,7 +279,7 @@ impl NodeTrait for NodeUse {
             }
         });
 
-        let opt_h = property_bag::length_or_none (pbag, "height", LengthDir::Vertical)?;
+        let opt_h: Option<RsvgLength> = property_bag::parse_or_none (pbag, "height", LengthDir::Vertical)?;
         let h = match opt_h {
             Some (h) => {
                 Some (h.check_nonnegative ().map_err (|e| NodeError::attribute_error ("height", e))?)


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