Commit b6d1504c authored by Paolo Borelli's avatar Paolo Borelli

Make rsvg_state_parse_style_pair take attr as a string

Parsing the attr name in rust allows to cleanup the code.
parent 80df69d6
......@@ -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 */
......@@ -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
......
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");
......
......@@ -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};
......
......@@ -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()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment