[librsvg] property_bag.rs: New functions lookup_and_parse(), lookup_length()
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] property_bag.rs: New functions lookup_and_parse(), lookup_length()
- Date: Tue, 28 Feb 2017 02:27:08 +0000 (UTC)
commit acbd87449144473ceddce8c419616e61cf25063d
Author: Federico Mena Quintero <federico gnome org>
Date: Mon Feb 27 20:16:01 2017 -0600
property_bag.rs: New functions lookup_and_parse(), lookup_length()
The former does T::from_str() and uses T::default() for the parsing and
default values.
The latter parses an RsvgLength, and lets the caller specify a
LengthDir.
This makes the set_atts() functions a bit less verbose.
rust/src/marker.rs | 20 +++++-----------
rust/src/property_bag.rs | 31 +++++++++++++++++++++++++
rust/src/shapes.rs | 56 ++++++++++++---------------------------------
3 files changed, 52 insertions(+), 55 deletions(-)
---
diff --git a/rust/src/marker.rs b/rust/src/marker.rs
index b12a211..4f01b67 100644
--- a/rust/src/marker.rs
+++ b/rust/src/marker.rs
@@ -214,27 +214,19 @@ impl NodeMarker {
impl NodeTrait for NodeMarker {
fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
- self.units.set (property_bag::lookup (pbag, "markerUnits").map_or (MarkerUnits::default (),
- |v| MarkerUnits::from_str
(&v).unwrap_or (MarkerUnits::default ())));
+ self.units.set (property_bag::lookup_and_parse (pbag, "markerUnits"));
- self.ref_x.set (property_bag::lookup (pbag, "refX").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
- self.ref_y.set (property_bag::lookup (pbag, "refY").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Vertical)));
+ self.ref_x.set (property_bag::lookup_length (pbag, "refX", LengthDir::Horizontal));
+ self.ref_y.set (property_bag::lookup_length (pbag, "refY", LengthDir::Vertical));
self.width.set (property_bag::lookup (pbag, "markerWidth").map_or (NodeMarker::get_default_size (),
|v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
self.height.set (property_bag::lookup (pbag, "markerHeight").map_or (NodeMarker::get_default_size (),
|v| RsvgLength::parse (&v,
LengthDir::Vertical)));
- self.orient.set (property_bag::lookup (pbag, "orient").map_or (MarkerOrient::default (),
- |v| MarkerOrient::from_str
(&v).unwrap_or (MarkerOrient::default ())));
-
- self.aspect.set (property_bag::lookup (pbag, "preserveAspectRatio").map_or (AspectRatio::default (),
- |v|
AspectRatio::from_str (&v).unwrap_or (AspectRatio::default ())));
-
- self.vbox.set (property_bag::lookup (pbag, "viewBox").map_or (RsvgViewBox::default (),
- |v| RsvgViewBox::from_str
(&v).unwrap_or (RsvgViewBox::default ())));
+ self.orient.set (property_bag::lookup_and_parse (pbag, "orient"));
+ self.aspect.set (property_bag::lookup_and_parse (pbag, "preserveAspectRatio"));
+ self.vbox.set (property_bag::lookup_and_parse (pbag, "viewBox"));
}
fn draw (&self, _: &RsvgNode, _: *const RsvgDrawingCtx, _: i32) {
diff --git a/rust/src/property_bag.rs b/rust/src/property_bag.rs
index 7a27383..1e296fd 100644
--- a/rust/src/property_bag.rs
+++ b/rust/src/property_bag.rs
@@ -1,8 +1,12 @@
extern crate libc;
extern crate glib;
+use std::str::FromStr;
+
use self::glib::translate::*;
+use length::*;
+
pub enum RsvgPropertyBag {}
extern "C" {
@@ -20,3 +24,30 @@ pub fn lookup (pbag: *const RsvgPropertyBag, key: &str) -> Option<String> {
from_glib_none (c_value)
}
}
+
+pub fn lookup_and_parse<T: Default + FromStr> (pbag: *const RsvgPropertyBag, key: &str) -> T {
+ let value = lookup (pbag, key);
+
+ if let Some (v) = value {
+ let result = T::from_str (&v);
+
+ if let Ok (r) = result {
+ r
+ } else {
+ // FIXME: Error is discarded here. Figure out a way to propagate it upstream.
+ T::default ()
+ }
+ } else {
+ T::default ()
+ }
+}
+
+pub fn lookup_length (pbag: *const RsvgPropertyBag, key: &str, length_dir: LengthDir) -> RsvgLength {
+ let value = lookup (pbag, key);
+
+ if let Some (v) = value {
+ RsvgLength::parse (&v, length_dir)
+ } else {
+ RsvgLength::default ()
+ }
+}
diff --git a/rust/src/shapes.rs b/rust/src/shapes.rs
index 1176a48..14a4c07 100644
--- a/rust/src/shapes.rs
+++ b/rust/src/shapes.rs
@@ -195,17 +195,10 @@ impl NodeLine {
impl NodeTrait for NodeLine {
fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
- self.x1.set (property_bag::lookup (pbag, "x1").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
-
- self.y1.set (property_bag::lookup (pbag, "y1").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Vertical)));
-
- self.x2.set (property_bag::lookup (pbag, "x2").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
-
- self.y2.set (property_bag::lookup (pbag, "y2").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Vertical)));
+ self.x1.set (property_bag::lookup_length (pbag, "x1", LengthDir::Horizontal));
+ self.y1.set (property_bag::lookup_length (pbag, "y1", LengthDir::Vertical));
+ self.x2.set (property_bag::lookup_length (pbag, "x2", LengthDir::Horizontal));
+ self.y2.set (property_bag::lookup_length (pbag, "y2", LengthDir::Vertical));
}
fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
@@ -257,17 +250,10 @@ impl NodeRect {
impl NodeTrait for NodeRect {
fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
- self.x.set (property_bag::lookup (pbag, "x").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
-
- self.y.set (property_bag::lookup (pbag, "y").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Vertical)));
-
- self.w.set (property_bag::lookup (pbag, "width").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
-
- self.h.set (property_bag::lookup (pbag, "height").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Vertical)));
+ self.x.set (property_bag::lookup_length (pbag, "x", LengthDir::Horizontal));
+ self.y.set (property_bag::lookup_length (pbag, "y", LengthDir::Vertical));
+ self.w.set (property_bag::lookup_length (pbag, "width", LengthDir::Horizontal));
+ self.h.set (property_bag::lookup_length (pbag, "height", LengthDir::Vertical));
self.rx.set (property_bag::lookup (pbag, "rx").map_or (None,
|v| Some (RsvgLength::parse (&v,
LengthDir::Horizontal))));
@@ -440,14 +426,9 @@ impl NodeCircle {
impl NodeTrait for NodeCircle {
fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
- self.cx.set (property_bag::lookup (pbag, "cx").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
-
- self.cy.set (property_bag::lookup (pbag, "cy").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Vertical)));
-
- self.r.set (property_bag::lookup (pbag, "r").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v, LengthDir::Both)));
+ self.cx.set (property_bag::lookup_length (pbag, "cx", LengthDir::Horizontal));
+ self.cy.set (property_bag::lookup_length (pbag, "cy", LengthDir::Vertical));
+ self.r.set (property_bag::lookup_length (pbag, "r", LengthDir::Both));
}
fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
@@ -485,17 +466,10 @@ impl NodeEllipse {
impl NodeTrait for NodeEllipse {
fn set_atts (&self, _: &RsvgNode, _: *const RsvgHandle, pbag: *const RsvgPropertyBag) {
- self.cx.set (property_bag::lookup (pbag, "cx").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
-
- self.cy.set (property_bag::lookup (pbag, "cy").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Vertical)));
-
- self.rx.set (property_bag::lookup (pbag, "rx").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Horizontal)));
-
- self.ry.set (property_bag::lookup (pbag, "ry").map_or (RsvgLength::default (),
- |v| RsvgLength::parse (&v,
LengthDir::Vertical)));
+ self.cx.set (property_bag::lookup_length (pbag, "cx", LengthDir::Horizontal));
+ self.cy.set (property_bag::lookup_length (pbag, "cy", LengthDir::Vertical));
+ self.rx.set (property_bag::lookup_length (pbag, "rx", LengthDir::Horizontal));
+ self.ry.set (property_bag::lookup_length (pbag, "ry", LengthDir::Vertical));
}
fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]