Skip to content

Commit

Permalink
codgen: Allow renaming objects/enums/flags
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed Aug 11, 2022
1 parent d3c2779 commit e37fe47
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 57 deletions.
2 changes: 2 additions & 0 deletions book/src/config_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Sometimes Gir understands the object definition incorrectly or the `.gir` file c
[[object]]
# object's fullname
name = "Gtk.SomeClass"
# An optional different name of the object
rename = "Gtk.SomeNewName"
# can be also "manual" and "ignore" but it's simpler to just put the object in the same array
status = "generate"
# replace the parameter name for the child in child properties (instead "child")
Expand Down
26 changes: 15 additions & 11 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
mod_rs.push(format!(
"{} use self::enums::{};",
enum_analysis.visibility.export_visibility(),
enum_.name
config.rename.as_ref().unwrap_or(&enum_.name)
));

generate_enum(env, w, enum_, config, enum_analysis)?;
Expand Down Expand Up @@ -132,8 +132,12 @@ fn generate_enum(
writeln!(w, "#[derive(Clone, Copy)]")?;
writeln!(w, "#[non_exhaustive]")?;
doc_alias(w, &enum_.c_type, "", 0)?;
if config.rename.is_some() {
doc_alias(w, &enum_.name, "", 0)?;
}

writeln!(w, "{} enum {} {{", analysis.visibility, enum_.name)?;
let enum_name = config.rename.as_ref().unwrap_or(&enum_.name);
writeln!(w, "{} enum {} {{", analysis.visibility, enum_name)?;
for member in &members {
cfg_deprecated(
w,
Expand Down Expand Up @@ -208,7 +212,7 @@ fn generate_enum(
"impl fmt::Display for {0} {{\n\
\tfn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{\n\
\t\twrite!(f, \"{0}::{{}}\", match *self {{",
enum_.name
enum_name
)?;
for member in &members {
version_condition_no_doc(w, env, None, member.version, false, 3)?;
Expand Down Expand Up @@ -236,7 +240,7 @@ impl IntoGlib for {name} {{
fn into_glib(self) -> {sys_crate_name}::{ffi_name} {{
match self {{",
sys_crate_name = sys_crate_name,
name = enum_.name,
name = enum_name,
ffi_name = enum_.c_type
)?;
for member in &members {
Expand Down Expand Up @@ -274,7 +278,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
unsafe fn from_glib(value: {sys_crate_name}::{ffi_name}) -> Self {{
{assert}match value {{",
sys_crate_name = sys_crate_name,
name = enum_.name,
name = enum_name,
ffi_name = enum_.c_type,
assert = assert
)?;
Expand Down Expand Up @@ -308,7 +312,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
"impl ErrorDomain for {name} {{
fn domain() -> Quark {{
{assert}",
name = enum_.name,
name = enum_name,
assert = assert
)?;

Expand Down Expand Up @@ -390,7 +394,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
}}
}}",
sys_crate_name = sys_crate_name,
name = enum_.name,
name = enum_name,
get_type = get_type
)?;
writeln!(w)?;
Expand All @@ -402,7 +406,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
"impl {valuetype} for {name} {{
type Type = Self;
}}",
name = enum_.name,
name = enum_name,
valuetype = use_glib_type(env, "value::ValueType"),
)?;
writeln!(w)?;
Expand All @@ -418,7 +422,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
{assert}from_glib({glib}(value.to_glib_none().0))
}}
}}",
name = enum_.name,
name = enum_name,
glib = use_glib_type(env, "gobject_ffi::g_value_get_enum"),
gvalue = use_glib_type(env, "Value"),
genericwrongvaluetypechecker = use_glib_type(env, "value::GenericValueTypeChecker"),
Expand All @@ -443,7 +447,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
Self::static_type()
}}
}}",
name = enum_.name,
name = enum_name,
glib = use_glib_type(env, "gobject_ffi::g_value_set_enum"),
gvalue = use_glib_type(env, "Value"),
gtype = use_glib_type(env, "Type"),
Expand All @@ -455,7 +459,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
w,
env,
config,
&enum_.name,
enum_name,
enum_.version,
enum_.members.iter(),
|member| {
Expand Down
22 changes: 12 additions & 10 deletions src/codegen/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
mod_rs.push(format!(
"{} use self::flags::{};",
flags_analysis.visibility.export_visibility(),
flags.name
config.rename.as_ref().unwrap_or(&flags.name),
));
generate_flags(env, w, flags, config, flags_analysis)?;
}
Expand Down Expand Up @@ -92,11 +92,13 @@ fn generate_flags(
derives(w, d, 1)?;
}

let flags_name = config.rename.as_ref().unwrap_or(&flags.name);

doc_alias(w, &flags.c_type, "", 1)?;
writeln!(
w,
" {} struct {}: u32 {{",
analysis.visibility, flags.name
analysis.visibility, flags_name
)?;
for member in &flags.members {
let member_config = config.members.matched(&member.name);
Expand Down Expand Up @@ -184,14 +186,14 @@ fn generate_flags(
\t\t<Self as fmt::Debug>::fmt(self, f)\n\
\t}}\n\
}}\n",
flags.name
flags_name
)?;
}
generate_default_impl(
w,
env,
config,
&flags.name,
flags_name,
flags.version,
flags.members.iter(),
|member| {
Expand Down Expand Up @@ -222,7 +224,7 @@ impl IntoGlib for {name} {{
}}
",
sys_crate_name = sys_crate_name,
name = flags.name,
name = flags_name,
ffi_name = flags.c_type
)?;

Expand All @@ -244,7 +246,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
}}
",
sys_crate_name = sys_crate_name,
name = flags.name,
name = flags_name,
ffi_name = flags.c_type,
assert = assert
)?;
Expand All @@ -266,7 +268,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
}}
}}",
sys_crate_name = sys_crate_name,
name = flags.name,
name = flags_name,
get_type = get_type
)?;
writeln!(w)?;
Expand All @@ -278,7 +280,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
"impl {valuetype} for {name} {{
type Type = Self;
}}",
name = flags.name,
name = flags_name,
valuetype = use_glib_type(env, "value::ValueType"),
)?;
writeln!(w)?;
Expand All @@ -294,7 +296,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
{assert}from_glib({glib}(value.to_glib_none().0))
}}
}}",
name = flags.name,
name = flags_name,
glib = use_glib_type(env, "gobject_ffi::g_value_get_flags"),
gvalue = use_glib_type(env, "Value"),
genericwrongvaluetypechecker = use_glib_type(env, "value::GenericValueTypeChecker"),
Expand All @@ -319,7 +321,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
Self::static_type()
}}
}}",
name = flags.name,
name = flags_name,
glib = use_glib_type(env, "gobject_ffi::g_value_set_flags"),
gvalue = use_glib_type(env, "Value"),
gtype = use_glib_type(env, "Type"),
Expand Down
Loading

0 comments on commit e37fe47

Please sign in to comment.