Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Rust wrappers for class structs too #487

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/analysis/general.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use config::gobjects::*;
use library::*;

#[derive(Debug, Clone)]
pub struct StatusedTypeId {
pub type_id: TypeId,
pub name: String,
Expand Down
6 changes: 6 additions & 0 deletions src/analysis/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use traits::*;
#[derive(Default)]
pub struct Info {
pub base: InfoBase,
pub is_interface: bool,
pub c_type: String,
pub class_type: Option<String>,
pub c_class_type: Option<String>,
pub get_type: String,
pub supertypes: Vec<general::StatusedTypeId>,
Expand Down Expand Up @@ -217,7 +219,9 @@ pub fn class(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<Info>

let info = Info {
base: base,
is_interface: false,
c_type: klass.c_type.clone(),
class_type: klass.type_struct.clone(),
c_class_type: klass.c_class_type.clone(),
get_type: klass.glib_get_type.clone(),
supertypes: supertypes,
Expand Down Expand Up @@ -340,7 +344,9 @@ pub fn interface(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<I

let info = Info {
base: base,
is_interface: true,
c_type: iface.c_type.clone(),
class_type: iface.type_struct.clone(),
c_class_type: iface.c_class_type.clone(),
get_type: iface.glib_get_type.clone(),
supertypes: supertypes,
Expand Down
72 changes: 62 additions & 10 deletions src/codegen/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ pub fn uses(w: &mut Write, env: &Env, imports: &Imports) -> Result<()> {
pub fn define_object_type(
w: &mut Write,
env: &Env,
is_interface: bool,
type_name: &str,
glib_name: &str,
class_name: &Option<&str>,
glib_class_name: &Option<&str>,
glib_func_name: &str,
parents: &[StatusedTypeId],
ifaces: &[StatusedTypeId],
) -> Result<()> {
let mut external_parents = false;
let parents: Vec<String> = parents
Expand All @@ -62,49 +65,98 @@ pub fn define_object_type(
)
})
.collect();
let ifaces: Vec<String> = ifaces
.iter()
.filter(|p| !p.status.ignored())
.map(|p| if p.type_id.ns_id == namespaces::MAIN {
p.name.clone()
} else {
external_parents = true;
format!(
"{krate}::{name} => {krate}_ffi::{ffi_name}",
krate = env.namespaces[p.type_id.ns_id].crate_name,
name = p.name,
ffi_name = env.library.type_(p.type_id).get_glib_name().unwrap()
)
})
.collect();

let (separator, class_name) = {
if let &Some(s) = glib_class_name {
(", ".to_string(), format!("ffi::{}", s))
if let (&Some(name), &Some(ffi_name)) = (class_name, glib_class_name) {
(", ".to_string(), format!("{}, ffi::{}", name, ffi_name))
} else {
("".to_string(), "".to_string())
}
};

let kind_name = if is_interface { "Interface" } else { "Object" };

try!(writeln!(w, ""));
try!(writeln!(w, "glib_wrapper! {{"));
if parents.is_empty() {
if parents.is_empty() && ifaces.is_empty() {
try!(writeln!(
w,
"\tpub struct {}(Object<ffi::{}{}{}>);",
"\tpub struct {}({}<ffi::{}{}{}>);",
type_name,
kind_name,
glib_name,
separator,
class_name
));
} else if external_parents {
try!(writeln!(
w,
"\tpub struct {}(Object<ffi::{}{}{}>): [",
"\tpub struct {}({}<ffi::{}{}{}>):",
type_name,
kind_name,
glib_name,
separator,
class_name
));
for parent in parents {
try!(writeln!(w, "\t\t{},", parent));

if !parents.is_empty() {
try!(writeln!(w, "\t\tparents=["));
try!(writeln!(w, "{}", parents.iter().map(|s| format!("\t\t\t{}", s)).collect::<Vec<_>>().join(",\n")));

if !ifaces.is_empty() {
try!(writeln!(w, "\t\t],"));
} else {
try!(writeln!(w, "\t\t];"));
}
}

if !ifaces.is_empty() {
try!(writeln!(w, "\t\tifaces=["));
try!(writeln!(w, "{}", ifaces.iter().map(|s| format!("\t\t\t{}", s)).collect::<Vec<_>>().join(",\n")));
try!(writeln!(w, "\t\t];"));
}
try!(writeln!(w, "\t];"));
} else {
try!(writeln!(
w,
"\tpub struct {}(Object<ffi::{}{}{}>): {};",
"\tpub struct {}({}<ffi::{}{}{}>):",
type_name,
kind_name,
glib_name,
separator,
class_name,
parents.join(", ")
));

if !parents.is_empty() {
try!(writeln!(w, "\t\tparents=["));
try!(writeln!(w, "{}", parents.iter().map(|s| format!("\t\t\t{}", s)).collect::<Vec<_>>().join(",\n")));

if !ifaces.is_empty() {
try!(writeln!(w, "\t\t],"));
} else {
try!(writeln!(w, "\t\t];"));
}
}

if !ifaces.is_empty() {
try!(writeln!(w, "\t\tifaces=["));
try!(writeln!(w, "{}", ifaces.iter().map(|s| format!("\t\t\t{}", s)).collect::<Vec<_>>().join(",\n")));
try!(writeln!(w, "\t\t];"));
}
}
try!(writeln!(w, ""));
try!(writeln!(w, "\tmatch fn {{"));
Expand Down
19 changes: 18 additions & 1 deletion src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,31 @@ pub fn generate(w: &mut Write, env: &Env, analysis: &analysis::object::Info) ->
try!(general::start_comments(w, &env.config));
try!(general::uses(w, env, &analysis.imports));

let parents = analysis.supertypes.iter().filter(|t| {
match *env.library.type_(t.type_id) {
library::Type::Class(..) => true,
_ => false,
}
}).cloned().collect::<Vec<_>>();

let interfaces = analysis.supertypes.iter().filter(|t| {
match *env.library.type_(t.type_id) {
library::Type::Interface(..) => true,
_ => false,
}
}).cloned().collect::<Vec<_>>();

try!(general::define_object_type(
w,
env,
analysis.is_interface,
&analysis.name,
&analysis.c_type,
&analysis.class_type.as_ref().map(|s| &s[..]),
&analysis.c_class_type.as_ref().map(|s| &s[..]),
&analysis.get_type,
&analysis.supertypes,
parents.as_ref(),
interfaces.as_ref(),
));

if need_generate_inherent(analysis) {
Expand Down