Skip to content

Commit a1a0b76

Browse files
committed
node_macro: add cfg() attr to feature gate node impl
1 parent a63c8c5 commit a1a0b76

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

node-graph/node-macro/src/codegen.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,30 +345,35 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
345345

346346
let properties = &attributes.properties_string.as_ref().map(|value| quote!(Some(#value))).unwrap_or(quote!(None));
347347

348-
let node_input_accessor = generate_node_input_references(parsed, fn_generics, &field_idents, &graphene_core, &identifier);
348+
let cfg = if let Some(cfg) = &attributes.cfg { quote!(#[cfg(#cfg)]) } else { quote!() };
349+
let node_input_accessor = generate_node_input_references(parsed, fn_generics, &field_idents, &graphene_core, &identifier, &cfg);
349350
Ok(quote! {
350351
/// Underlying implementation for [#struct_name]
351352
#[inline]
352353
#[allow(clippy::too_many_arguments)]
353354
#vis #async_keyword fn #fn_name <'n, #(#fn_generics,)*> (#input_ident: #input_type #(, #field_idents: #field_types)*) -> #output_type #where_clause #body
354355

356+
#cfg
355357
#[automatically_derived]
356358
impl<'n, #(#fn_generics,)* #(#struct_generics,)* #(#future_idents,)*> #graphene_core::Node<'n, #input_type> for #mod_name::#struct_name<#(#struct_generics,)*>
357359
#struct_where_clause
358360
{
359361
#eval_impl
360362
}
361363

364+
#cfg
362365
const fn #identifier() -> #graphene_core::ProtoNodeIdentifier {
363366
#graphene_core::ProtoNodeIdentifier::new(std::concat!(#identifier_path, "::", std::stringify!(#struct_name)))
364367
}
365368

369+
#cfg
366370
#[doc(inline)]
367371
pub use #mod_name::#struct_name;
368372

369373
#[doc(hidden)]
370374
#node_input_accessor
371375

376+
#cfg
372377
#[doc(hidden)]
373378
mod #mod_name {
374379
use super::*;
@@ -434,7 +439,14 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
434439
}
435440

436441
/// Generates strongly typed utilites to access inputs
437-
fn generate_node_input_references(parsed: &ParsedNodeFn, fn_generics: &[crate::GenericParam], field_idents: &[&PatIdent], graphene_core: &TokenStream2, identifier: &Ident) -> TokenStream2 {
442+
fn generate_node_input_references(
443+
parsed: &ParsedNodeFn,
444+
fn_generics: &[crate::GenericParam],
445+
field_idents: &[&PatIdent],
446+
graphene_core: &TokenStream2,
447+
identifier: &Ident,
448+
cfg: &TokenStream2,
449+
) -> TokenStream2 {
438450
let inputs_module_name = format_ident!("{}", parsed.struct_name.to_string().to_case(Case::Snake));
439451

440452
let mut generated_input_accessor = Vec::new();
@@ -479,6 +491,7 @@ fn generate_node_input_references(parsed: &ParsedNodeFn, fn_generics: &[crate::G
479491
}
480492

481493
quote! {
494+
#cfg
482495
pub mod #inputs_module_name {
483496
use super::*;
484497

node-graph/node-macro/src/parsing.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub(crate) struct NodeFnAttributes {
4545
pub(crate) path: Option<Path>,
4646
pub(crate) skip_impl: bool,
4747
pub(crate) properties_string: Option<LitStr>,
48+
/// whether to `#[cfg]` gate the node implementation, defaults to None
49+
pub(crate) cfg: Option<TokenStream2>,
4850
// Add more attributes as needed
4951
}
5052

@@ -184,6 +186,7 @@ impl Parse for NodeFnAttributes {
184186
let mut path = None;
185187
let mut skip_impl = false;
186188
let mut properties_string = None;
189+
let mut cfg = None;
187190

188191
let content = input;
189192
// let content;
@@ -239,13 +242,20 @@ impl Parse for NodeFnAttributes {
239242

240243
properties_string = Some(parsed_properties_string);
241244
}
245+
"cfg" => {
246+
if cfg.is_some() {
247+
return Err(Error::new_spanned(path, "Multiple 'feature' attributes are not allowed"));
248+
}
249+
let meta = meta.require_list()?;
250+
cfg = Some(meta.tokens.clone());
251+
}
242252
_ => {
243253
return Err(Error::new_spanned(
244254
meta,
245255
indoc!(
246256
r#"
247257
Unsupported attribute in `node`.
248-
Supported attributes are 'category', 'path' 'name', 'skip_impl' and 'properties'.
258+
Supported attributes are 'category', 'path' 'name', 'skip_impl', 'cfg' and 'properties'.
249259
250260
Example usage:
251261
#[node_macro::node(category("Value"), name("Test Node"))]
@@ -262,6 +272,7 @@ impl Parse for NodeFnAttributes {
262272
path,
263273
skip_impl,
264274
properties_string,
275+
cfg,
265276
})
266277
}
267278
}

0 commit comments

Comments
 (0)