Skip to content

Commit

Permalink
extract_cfg_attrs: also handle cfg_attr (but it's hacky with attr.val…
Browse files Browse the repository at this point in the history
…ue.to_token_stream().to_string().contains("cfg("))
  • Loading branch information
0x53A committed Jan 21, 2025
1 parent b4c181b commit 9a4c702
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions godot-macros/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,16 @@ pub(crate) fn extract_cfg_attrs(
attrs: &[venial::Attribute],
) -> impl IntoIterator<Item = &venial::Attribute> {
attrs.iter().filter(|attr| {
attr.get_single_path_segment()
.is_some_and(|name| name == "cfg")
let Some(attr_name) = attr.get_single_path_segment() else {
return false;
};
if attr_name == "cfg" {
return true;
}
if attr_name == "cfg_attr" && attr.value.to_token_stream().to_string().contains("cfg(") {
return true;
}
return false;
})
}

Expand Down Expand Up @@ -310,25 +318,6 @@ pub fn venial_parse_meta(

// util functions for handling #[func]s and #[var(get=f, set=f)]

pub fn get_cfg_attributes(attributes: &[Attribute]) -> Vec<&Attribute> {
attributes
.iter()
.filter(|attr| {
if attr.path.len() > 1 {
return false;
}
let name = attr.path[0].to_string();
if name == "cfg" {
return true;
}
if name == "cfg_attr" && attr.value.to_token_stream().to_string().contains("cfg(") {
return true;
}
false // skip this attribute
})
.collect::<Vec<_>>()
}

pub fn make_function_registered_name_constants(
funcs: &[FuncDefinition],
class_name: &Ident,
Expand All @@ -337,7 +326,9 @@ pub fn make_function_registered_name_constants(
.iter()
.map(|func| {
// the constant needs the same #[cfg] attribute(s) as the function, so that it is only active if the function is also active.
let cfg_attributes = get_cfg_attributes(&func.external_attributes);
let cfg_attributes = extract_cfg_attrs(&func.external_attributes)
.into_iter()
.collect();
make_function_registered_name_constant(
class_name,
&func.signature_info.method_name,
Expand Down

0 comments on commit 9a4c702

Please sign in to comment.