[librsvg: 3/5] Make rsvg_state_parse_style_pair take attr as a string



commit b6d1504cb542ee323232a1ed1fbd6fa07a495688
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Sep 9 22:44:46 2018 +0200

    Make rsvg_state_parse_style_pair take attr as a string
    
    Parsing the attr name in rust allows to cleanup the code.

 librsvg/rsvg-attributes.h        |  4 ----
 librsvg/rsvg-styles.c            | 18 ++++++----------
 rsvg_internals/src/attributes.rs | 46 ----------------------------------------
 rsvg_internals/src/lib.rs        |  2 --
 rsvg_internals/src/state.rs      | 15 +++++++++----
 5 files changed, 18 insertions(+), 67 deletions(-)
---
diff --git a/librsvg/rsvg-attributes.h b/librsvg/rsvg-attributes.h
index 666ec58a..4ad31615 100644
--- a/librsvg/rsvg-attributes.h
+++ b/librsvg/rsvg-attributes.h
@@ -157,8 +157,4 @@ typedef enum {
     RSVG_ATTRIBUTE_Z,
 } RsvgAttribute;
 
-/* Implemented in rust/src/attributes.rs */
-G_GNUC_INTERNAL
-gboolean rsvg_attribute_from_name (const char *name, RsvgAttribute *out_attr);
-
 #endif /* RSVG_ATTRIBUTES_H */
diff --git a/librsvg/rsvg-styles.c b/librsvg/rsvg-styles.c
index da173e85..005780ff 100644
--- a/librsvg/rsvg-styles.c
+++ b/librsvg/rsvg-styles.c
@@ -37,7 +37,7 @@
 #include <libcroco/libcroco.h>
 
 /* Defined in rsvg_internals/src/state.rs */
-extern gboolean rsvg_state_parse_style_pair(RsvgState *state, RsvgAttribute attr, const char *value, 
gboolean important, gboolean accept_shorthands) G_GNUC_WARN_UNUSED_RESULT;
+extern gboolean rsvg_state_parse_style_pair(RsvgState *state, const char *attr, const char *value, gboolean 
important) G_GNUC_WARN_UNUSED_RESULT;
 
 typedef struct _StyleValueData {
     gchar *value;
@@ -282,16 +282,12 @@ static void
 apply_style (const gchar *key, StyleValueData *value, gpointer user_data)
 {
     RsvgState *state = user_data;
-    RsvgAttribute attr;
-
-    if (rsvg_attribute_from_name (key, &attr)) {
-        /* FIXME: this is ignoring errors */
-        gboolean success = rsvg_state_parse_style_pair (state,
-                                                        attr,
-                                                        value->value,
-                                                        value->important,
-                                                        TRUE);
-    }
+
+    /* FIXME: this is ignoring errors */
+    gboolean success = rsvg_state_parse_style_pair (state,
+                                                    key,
+                                                    value->value,
+                                                    value->important);
 }
 
 gboolean
diff --git a/rsvg_internals/src/attributes.rs b/rsvg_internals/src/attributes.rs
index 7a8fe53a..a16b5541 100644
--- a/rsvg_internals/src/attributes.rs
+++ b/rsvg_internals/src/attributes.rs
@@ -1,12 +1,7 @@
 extern crate phf;
 
-use glib::translate::*;
-use glib_sys;
-use libc;
 use std::str::FromStr;
 
-use util::utf8_cstr;
-
 include!(concat!(env!("OUT_DIR"), "/attributes-codegen.rs"));
 
 impl FromStr for Attribute {
@@ -33,29 +28,9 @@ impl Attribute {
     }
 }
 
-#[no_mangle]
-pub extern "C" fn rsvg_attribute_from_name(
-    raw_name: *const libc::c_char,
-    out_attr: *mut Attribute,
-) -> glib_sys::gboolean {
-    let name = unsafe { utf8_cstr(raw_name) };
-
-    match Attribute::from_str(name) {
-        Ok(a) => {
-            unsafe {
-                *out_attr = a;
-            }
-            true.to_glib()
-        }
-
-        Err(_) => false.to_glib(),
-    }
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;
-    use std::mem;
 
     #[test]
     fn parses_attributes() {
@@ -67,27 +42,6 @@ mod tests {
         assert_eq!(Attribute::from_str("foobar"), Err(()));
     }
 
-    #[test]
-    fn c_attribute_from_name() {
-        let mut a: Attribute = unsafe { mem::uninitialized() };
-        let res: bool = from_glib(rsvg_attribute_from_name(
-            "width".to_glib_none().0,
-            &mut a as *mut Attribute,
-        ));
-        assert!(res);
-        assert_eq!(a, Attribute::Width);
-    }
-
-    #[test]
-    fn invalid_c_attribute_from_name() {
-        let mut a: Attribute = unsafe { mem::uninitialized() };
-        let res: bool = from_glib(rsvg_attribute_from_name(
-            "foobar".to_glib_none().0,
-            &mut a as *mut Attribute,
-        ));
-        assert!(!res);
-    }
-
     #[test]
     fn converts_attributes_back_to_strings() {
         assert_eq!(Attribute::ClipPath.to_str(), "clip-path");
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 37522218..5716a93c 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -26,8 +26,6 @@ extern crate lazy_static;
 #[macro_use]
 extern crate downcast_rs;
 
-pub use attributes::rsvg_attribute_from_name;
-
 pub use color::{rsvg_css_parse_color, ColorKind, ColorSpec};
 
 pub use defs::{rsvg_defs_free, rsvg_defs_lookup, rsvg_defs_new};
diff --git a/rsvg_internals/src/state.rs b/rsvg_internals/src/state.rs
index dfd285e9..1593bb1c 100644
--- a/rsvg_internals/src/state.rs
+++ b/rsvg_internals/src/state.rs
@@ -1387,18 +1387,25 @@ make_property!(
 #[no_mangle]
 pub extern "C" fn rsvg_state_parse_style_pair(
     state: *mut RsvgState,
-    attr: Attribute,
+    name: *const libc::c_char,
     value: *const libc::c_char,
     important: glib_sys::gboolean,
 ) -> glib_sys::gboolean {
     assert!(!state.is_null());
     let state = unsafe { &mut *(state as *mut State) };
 
+    assert!(!name.is_null());
+    let name = unsafe { utf8_cstr(name) };
+
     assert!(!value.is_null());
     let value = unsafe { utf8_cstr(value) };
 
-    match state.parse_style_pair(attr, value, from_glib(important)) {
-        Ok(_) => true.to_glib(),
-        Err(_) => false.to_glib(),
+    if let Ok(attr) = Attribute::from_str(name) {
+        match state.parse_style_pair(attr, value, from_glib(important)) {
+            Ok(_) => true.to_glib(),
+            Err(_) => false.to_glib(),
+        }
+    } else {
+        false.to_glib()
     }
 }


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