[librsvg/attribute-parsers-737: 1/5] set_attribute(): New helper function




commit 42332abc8ec3775b2fa44e5da6b65fd333dbd7c5
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Oct 19 19:17:36 2022 -0500

    set_attribute(): New helper function

 src/element.rs | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
---
diff --git a/src/element.rs b/src/element.rs
index 3d8fb60ba..06b5480a4 100644
--- a/src/element.rs
+++ b/src/element.rs
@@ -76,6 +76,26 @@ pub trait SetAttributes {
     }
 }
 
+/// Sets `dest` if `parse_result` is `Ok()`, otherwise just logs the error.
+///
+/// Implementations of the [`SetAttributes`] trait generally scan a list of attributes
+/// for the ones they can handle, and parse their string values.  Per the SVG spec, an attribute
+/// with an invalid value should be ignored, and it should fall back to the default value.
+///
+/// In librsvg, those default values are set in each element's implementation of the [`Default`] trait:
+/// at element creation time, each element gets initialized to its `Default`, and then each attribute
+/// gets parsed.  This function will set that attribute's value only if parsing was successful.
+pub fn set_attribute<T>(dest: &mut T, parse_result: Result<T, ElementError>, session: &Session) {
+    match parse_result {
+        Ok(v) => *dest = v,
+        Err(e) => {
+            // FIXME: this does not provide a clue of what was the problematic element.
+            // We need tracking of the current parsing position to do that.
+            rsvg_log!(session, "ignoring attribute with invalid value: {}", e);
+        }
+    }
+}
+
 pub trait Draw {
     /// Draw an element
     ///


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