[librsvg: 2/95] New function rsvg_attribute_from_name()



commit 0bcad36db47da2237c436571e3b62c67cdaeeab5
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Feb 7 13:34:19 2018 -0600

    New function rsvg_attribute_from_name()

 rust/src/attributes.rs | 40 ++++++++++++++++++++++++++++++++++++++++
 rust/src/lib.rs        |  4 ++++
 2 files changed, 44 insertions(+)
---
diff --git a/rust/src/attributes.rs b/rust/src/attributes.rs
index 5c7d4c28..a8bc060a 100644
--- a/rust/src/attributes.rs
+++ b/rust/src/attributes.rs
@@ -1,7 +1,13 @@
 extern crate phf;
 
+use glib_sys;
+use glib::translate::*;
+use libc;
+use std::mem;
 use std::str::FromStr;
 
+use util::utf8_cstr;
+
 include!(concat!(env!("OUT_DIR"), "/attributes-codegen.rs"));
 
 impl FromStr for Attribute {
@@ -12,6 +18,23 @@ impl FromStr for Attribute {
     }
 }
 
+#[no_mangle]
+pub extern 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::*;
@@ -25,4 +48,21 @@ mod tests {
     fn unknown_attribute_yields_error() {
         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".as_ptr() as *const libc::c_char,
+                                                           &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".as_ptr() as *const libc::c_char,
+                                                           &mut a as *mut Attribute));
+        assert!(!res);
+    }
 }
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 1fbf438d..d5b5bcd8 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -19,6 +19,10 @@ extern crate lazy_static;
 #[macro_use]
 extern crate downcast_rs;
 
+pub use attributes::{
+    rsvg_attribute_from_name,
+};
+
 pub use bbox::{
     RsvgBbox,
     rsvg_bbox_init,


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