Skip to content

Commit

Permalink
Merge pull request gtk-rs#319 from EPashkin/remove_unneeded_glib_ffi_…
Browse files Browse the repository at this point in the history
…reference_in_enum

Remove unused uses in generated enums.rs
  • Loading branch information
EPashkin authored Feb 26, 2017
2 parents aacfc92 + a4e8ecb commit 18e6807
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 33 deletions.
24 changes: 16 additions & 8 deletions src/analysis/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,39 @@ pub fn analyze(env: &Env, props: &[library::Property], type_tid: library::TypeId

let (getter, setter) = analyze_property(env, prop, type_tid, &configured_properties,
signatures);
if getter.is_none() && setter.is_none() {
continue;
}

let type_string = rust_type(env, prop.typ);
let used_type_string = used_rust_type(env, prop.typ);
if let Some(prop) = getter {
if let Ok(s) = used_rust_type(env, prop.typ) {
if let Ok(ref s) = used_type_string {
imports.add_used_type(&s, prop.version);
}
if prop.conversion != PropertyConversion::Direct {
imports.add("std::mem::transmute", prop.version);
}
if type_string.is_ok() && prop.default_value.is_some() {
imports.add("glib::Value", prop.version);
imports.add("gobject_ffi", prop.version);
}

properties.push(prop);
}
if let Some(prop) = setter {
if let Ok(s) = used_rust_type(env, prop.typ) {
if let Ok(ref s) = used_type_string {
imports.add_used_type(&s, prop.version);
}
if type_string.is_ok() {
imports.add("glib::Value", prop.version);
imports.add("gobject_ffi", prop.version);
}

properties.push(prop);
}
}

if !properties.is_empty() {
imports.add("glib::object::IsA", None);
imports.add("glib::Value", None);
imports.add("gobject_ffi", None);
}

properties
}

Expand Down
66 changes: 41 additions & 25 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,49 @@ use traits::*;
use version::Version;

pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
let configs: Vec<&GObject> = env.config.objects.values()
.filter(|c| {
c.status.need_generate() &&
c.type_id.map_or(false, |tid| tid.ns_id == namespaces::MAIN)
})
.collect();
let mut has_get_quark = false;
let mut has_any = false;
for config in &configs {
if let Type::Enumeration(ref enum_) = *env.library.type_(config.type_id.unwrap()) {
has_any = true;
if get_error_quark_name(enum_).is_some(){
has_get_quark = true;
break;
}
}
}

let path = root_path.join("enums.rs");
file_saver::save_to_file(path, env.config.make_backup, |w| {
try!(general::start_comments(w, &env.config));
try!(writeln!(w, ""));
try!(writeln!(w, "use ffi;"));
if env.namespaces.glib_ns_id == namespaces::MAIN {
try!(writeln!(w, "use ffi as glib_ffi;
use error::ErrorDomain;
use translate::*;"));
if has_get_quark {
try!(writeln!(w, "use ffi as glib_ffi;"));
try!(writeln!(w, "use error::ErrorDomain;"));
}
try!(writeln!(w, "use translate::*;"));
} else {
try!(writeln!(w, "use glib_ffi;
use glib::error::ErrorDomain;
use glib::translate::*;"));
if has_get_quark {
try!(writeln!(w, "use glib_ffi;"));
try!(writeln!(w, "use glib::error::ErrorDomain;"));
}
try!(writeln!(w, "use glib::translate::*;"));
}
try!(writeln!(w, ""));


let configs = env.config.objects.values()
.filter(|c| {
c.status.need_generate() &&
c.type_id.map_or(false, |tid| tid.ns_id == namespaces::MAIN)
});
let mut first = true;
for config in configs {
if has_any {
mod_rs.push("\nmod enums;".into());
}
for config in &configs {
if let Type::Enumeration(ref enum_) = *env.library.type_(config.type_id.unwrap()) {
if first {
mod_rs.push("\nmod enums;".into());
first = false;
}
if let Some (cfg) = version_condition_string(env, enum_.version, false, 0) {
mod_rs.push(cfg);
}
Expand Down Expand Up @@ -131,13 +145,7 @@ impl FromGlib<ffi::{ffi_name}> for {name} {{
}
}
"));
let error_domain: Option<String> = enum_.functions.iter()
.filter(|f| f.name == "quark")
.next()
.and_then(|f| f.c_identifier.clone())
.or_else(|| enum_.error_domain.clone());

if let Some(ref get_quark) = error_domain {
if let Some(ref get_quark) = get_error_quark_name(enum_) {
let get_quark = get_quark.replace("-", "_");
let has_failed_member = members.iter().any(|m| m.name == "Failed");

Expand Down Expand Up @@ -174,3 +182,11 @@ impl FromGlib<ffi::{ffi_name}> for {name} {{

Ok(())
}

fn get_error_quark_name(enum_: &Enumeration) -> Option<String> {
enum_.functions.iter()
.filter(|f| f.name == "quark")
.next()
.and_then(|f| f.c_identifier.clone())
.or_else(|| enum_.error_domain.clone())
}

0 comments on commit 18e6807

Please sign in to comment.