Identify conversion nodes via node macro annotations (#5083)

Flag adapter nodes and register them by identifier
This commit is contained in:
Dennis Kobert authored and GitHub committed 2026-12-06 13:57:52 +00:00
1 parent c0778752ab
commit 1300b4f92a
6 files changed
+102 -35

No files matched your search

@@ -155,14 +155,24 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
)
};
}
// Keeps an adapter row under the identifier the preprocessor names it by and repeats it under the adapter node's own identifier,
// which the new compiler selects by signature
macro_rules! also_under_own_identifier {
($module:ident, $row:expr) => {{
let row: (ProtoNodeIdentifier, NodeConstructor, NodeIOTypes) = $row;
let own = (graphene_core::ops::$module::IDENTIFIER, row.1, row.2.clone());
[row, own]
}};
}
// A conversion row registered under the same `input_adapter<$element>` identifier so a convertible-but-not-identical
// ranked wire can feed an `Item<$element>` connector, converting each element via `Into`
macro_rules! input_adapter_row {
(from_element: $from:ty, element: $element:ty) => {{
let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![
input_adapter_row!(node: IntoItemNode, from: Item<$from>, to: Item<$element>, element: $element),
input_adapter_row!(node: IntoListNode, from: List<$from>, to: List<$element>, element: $element),
];
let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [
also_under_own_identifier!(into_item, input_adapter_row!(node: IntoItemNode, from: Item<$from>, to: Item<$element>, element: $element)),
also_under_own_identifier!(into_list, input_adapter_row!(node: IntoListNode, from: List<$from>, to: List<$element>, element: $element)),
]
.concat();
entries
}};
(node: $node:ident, from: $from:ty, to: $to:ty, element: $element:ty) => {
@@ -254,7 +264,7 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
let mut entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = Vec::new();
$(
entries.extend(input_adapter_node!(element: $element));
entries.push(item_to_list_node!(element: $element));
entries.extend(also_under_own_identifier!(item_to_list, item_to_list_node!(element: $element)));
)*
entries
}};
@@ -359,9 +369,10 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
// Type-erased rows for the `ListDyn` connectors (`Read Attribute`, `Count`): any `List` wire erases its element type
macro_rules! list_dyn_rows {
($($element:ty),* $(,)?) => {{
let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![
$(input_adapter_node!(name: "input_adapter", from: List<$element>, to: ListDyn, element: ListDyn),)*
];
let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [
$(also_under_own_identifier!(into, input_adapter_node!(name: "input_adapter", from: List<$element>, to: ListDyn, element: ListDyn)),)*
]
.concat();
entries
}};
}
@@ -394,8 +405,8 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
($($element:ty),* $(,)?) => {{
let mut entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = Vec::new();
$(
entries.push(bundle_node!(element: $element));
entries.push(unbundle_node!(element: $element));
entries.extend(also_under_own_identifier!(bundle, bundle_node!(element: $element)));
entries.extend(also_under_own_identifier!(unbundle, unbundle_node!(element: $element)));
)*
entries
}};
@@ -455,10 +466,11 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
// Each item moves inside its matching variant, attributes and all, rather than mapping only its element.
macro_rules! embed_adapter_row {
(from_element: $from:ty, element: $element:ty) => {{
let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![
input_adapter_row!(node: EmbedItemNode, from: Item<$from>, to: Item<$element>, element: $element),
input_adapter_row!(node: EmbedListNode, from: List<$from>, to: List<$element>, element: $element),
];
let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [
also_under_own_identifier!(embed_item, input_adapter_row!(node: EmbedItemNode, from: Item<$from>, to: Item<$element>, element: $element)),
also_under_own_identifier!(embed_list, input_adapter_row!(node: EmbedListNode, from: List<$from>, to: List<$element>, element: $element)),
]
.concat();
entries
}};
}
@@ -475,10 +487,11 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
// The `Convert`-based counterpart of `input_adapter_row!`, for casts the std `Into` trait cannot express
macro_rules! convert_adapter_node {
(from_element: $from:ty, element: $element:ty) => {{
let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![
input_adapter_row!(node: ConvertItemNode, from: Item<$from>, to: Item<$element>, element: $element),
input_adapter_row!(node: ConvertListNode, from: List<$from>, to: List<$element>, element: $element),
];
let entries: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [
also_under_own_identifier!(convert_item, input_adapter_row!(node: ConvertItemNode, from: Item<$from>, to: Item<$element>, element: $element)),
also_under_own_identifier!(convert_list, input_adapter_row!(node: ConvertListNode, from: List<$from>, to: List<$element>, element: $element)),
]
.concat();
entries
}};
}
@@ -500,13 +513,19 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
// The sanctioned attribute value conversions: an Item wire's elements box per cell, while a List wire boxes whole as one value
macro_rules! attribute_value_node {
(Item<$element:ty>) => {
input_adapter_row!(node: ItemToAttributeValueNode, from: Item<$element>, to: Item<AttributeValueDyn>, element: AttributeValueDyn)
also_under_own_identifier!(
item_to_attribute_value,
input_adapter_row!(node: ItemToAttributeValueNode, from: Item<$element>, to: Item<AttributeValueDyn>, element: AttributeValueDyn)
)
};
(List<$element:ty>) => {
input_adapter_row!(node: ListToAttributeValueNode, from: List<$element>, to: Item<AttributeValueDyn>, element: AttributeValueDyn)
also_under_own_identifier!(
list_to_attribute_value,
input_adapter_row!(node: ListToAttributeValueNode, from: List<$element>, to: Item<AttributeValueDyn>, element: AttributeValueDyn)
)
};
}
let attribute_value_rows: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = vec![
let attribute_value_rows: Vec<(ProtoNodeIdentifier, NodeConstructor, NodeIOTypes)> = [
attribute_value_node!(Item<f64>),
attribute_value_node!(Item<i64>),
attribute_value_node!(Item<bool>),
@@ -531,7 +550,8 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
attribute_value_node!(List<Raster<GPU>>),
attribute_value_node!(List<Graphic>),
attribute_value_node!(List<Artboard>),
];
]
.concat();
node_types.extend(attribute_value_rows);
// Whole-List Transform companion rows: a rank-1 content wire composes the matrix onto every item
macro_rules! transform_list_node {
@@ -29,6 +29,17 @@ pub struct NodeFlags {
pub forwards_input: bool,
pub inject_scope: bool,
pub asynchronous: bool,
/// Whether type resolution may insert this node on an edge, from `#[node_macro::node(adapter(...))]`.
pub adapter: Option<AdapterKind>,
}
/// How an adapter node changes the wire it is inserted on.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AdapterKind {
/// Converts the wire's element into another element type.
Conversion,
/// Changes the wire's rank, like raising an `Item` onto a `List`.
Rank,
}
// Translation struct between macro and definition
+7
View File
@@ -838,8 +838,14 @@ pub(crate) fn generate_node_code(crate_ident: &CrateIdent, parsed: &ParsedNodeFn
footprint_invariant,
forwards_input,
inject_scope,
adapter,
..
} = attributes.flags;
let adapter = match adapter {
Some(AdapterKind::Conversion) => quote!(Some(gcore::registry::AdapterKind::Conversion)),
Some(AdapterKind::Rank) => quote!(Some(gcore::registry::AdapterKind::Rank)),
None => quote!(None),
};
let asynchronous = parsed.is_async;
let version = attributes.version.unwrap_or(1);
// A node returning a `Destructure` struct records its fields as output connectors
@@ -1053,6 +1059,7 @@ pub(crate) fn generate_node_code(crate_ident: &CrateIdent, parsed: &ParsedNodeFn
forwards_input: #forwards_input,
inject_scope: #inject_scope,
asynchronous: #asynchronous,
adapter: #adapter,
},
version: #version,
output_fields: #output_fields,
+29 -1
View File
@@ -54,6 +54,15 @@ pub(crate) struct NodeFlags {
pub(crate) no_partial: bool,
/// Set on the `Destructure` derive's generated extractor nodes, which take the struct rather than an `Item` or `List`. Not parsed from source.
pub(crate) destructure_extractor: bool,
/// Whether type resolution may insert this node on an edge, and how it changes the wire.
pub(crate) adapter: Option<AdapterKind>,
}
/// Mirrors `core_types::registry::AdapterKind`, which this proc-macro crate cannot depend on.
#[derive(Debug, Clone, Copy)]
pub(crate) enum AdapterKind {
Conversion,
Rank,
}
#[derive(Debug, Default, Clone)]
@@ -557,6 +566,25 @@ impl Parse for NodeFnAttributes {
// Example usage:
// #[node_macro::node(..., forwards_input, ...)]
"forwards_input" => set_flag(&mut flags.forwards_input, meta, "forwards_input")?,
// Marks the node as an adapter that type resolution may insert on an edge, either converting the wire's element type or changing its rank.
//
// Example usage:
// #[node_macro::node(..., adapter(conversion), ...)]
// #[node_macro::node(..., adapter(rank), ...)]
"adapter" => {
let meta = meta.require_list()?;
if flags.adapter.is_some() {
return Err(Error::new_spanned(meta, "Multiple 'adapter' attributes are not allowed"));
}
let kind: Ident = meta
.parse_args()
.map_err(|_| Error::new_spanned(meta, "Expected 'conversion' or 'rank' for 'adapter', e.g., adapter(rank)"))?;
flags.adapter = Some(match kind.to_string().as_str() {
"conversion" => AdapterKind::Conversion,
"rank" => AdapterKind::Rank,
_ => return Err(Error::new_spanned(kind, "Expected 'conversion' or 'rank' for 'adapter', e.g., adapter(rank)")),
});
}
// Instructs the preprocessor to make this node available as a scope.
// Other nodes can then access it with `#[scope(node::IDENTIFIER)]`.
//
@@ -604,7 +632,7 @@ impl Parse for NodeFnAttributes {
indoc!(
r#"
Unsupported attribute in `node`.
Supported attributes are 'category', 'name', 'icon', 'path', 'skip_impl', 'inspector', 'cfg', 'memoize', 'footprint_invariant', 'forwards_input', 'inject_scope', 'placeholder', 'no_partial', and 'version'.
Supported attributes are 'category', 'name', 'icon', 'path', 'skip_impl', 'inspector', 'cfg', 'memoize', 'footprint_invariant', 'forwards_input', 'adapter', 'inject_scope', 'placeholder', 'no_partial', and 'version'.
Example usage:
#[node_macro::node(..., name("Test Node"), ...)]
"#
+12 -12
View File
@@ -13,19 +13,19 @@ fn passthrough<'i, T: 'i + Send>(_: impl Ctx, content: T) -> T {
}
/// Shifts a whole wire value onto a connector's type through the std `Into` trait, serving the whole-`List` erasure onto `ListDyn` under the input adapter identifier.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn into<'i, T: 'i + Send + Into<O>, O: 'i + Send>(_: impl Ctx, value: T, _out_ty: PhantomData<O>) -> O {
value.into()
}
/// Raises an `Item` wire onto a `List` connector as its one-element list.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(rank))]
fn item_to_list<'i, T: 'i + Send>(_: impl Ctx, value: Item<T>) -> List<T> {
value.into()
}
/// Boxes a ranked wire's element into a type-erased attribute value, carrying the cell's attributes through the wire.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn item_to_attribute_value<'i, T: 'i + Clone + Send + Sync + Default + std::fmt::Debug + PartialEq + CacheHash + 'static>(
_: impl Ctx,
value: Item<T>,
@@ -36,7 +36,7 @@ fn item_to_attribute_value<'i, T: 'i + Clone + Send + Sync + Default + std::fmt:
}
/// Boxes a whole `List` wire as one type-erased attribute value, for attributes whose per-item value is itself a collection.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn list_to_attribute_value<'i, T: 'i + Clone + Send + Sync + Default + std::fmt::Debug + PartialEq + CacheHash + 'static>(
_: impl Ctx,
value: T,
@@ -46,27 +46,27 @@ fn list_to_attribute_value<'i, T: 'i + Clone + Send + Sync + Default + std::fmt:
}
/// Wraps a whole `List` onto the wire as one rank-0 `Item<Bundle<T>>` so an entire collection can feed a connector that carries it as one opaque cell.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(rank))]
fn bundle<'i, T: 'i + Send>(_: impl Ctx, value: List<T>) -> Item<Bundle<T>> {
Item::new_from_element(Bundle(value))
}
/// Unwraps a `Bundle` wire back into the whole `List` it carries, restoring the collection after it passed through a connector as one opaque cell.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(rank))]
fn unbundle<'i, T: 'i + Send>(_: impl Ctx, value: Item<Bundle<T>>) -> List<T> {
value.into_element().0
}
/// Converts an `Item` wire's element to a different element type it can produce through the std `Into` trait,
/// letting a convertible wire feed an `Item` connector whose element type it does not match by identity.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn into_item<'i, T: 'i + Send + Into<E>, E: 'i + Send>(_: impl Ctx, value: Item<T>, _element_ty: PhantomData<E>) -> Item<E> {
let (value, attributes) = value.into_parts();
Item::from_parts(value.into(), attributes)
}
/// The `List` counterpart of `into_item`, converting every element to a different element type it can produce.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn into_list<'i, T: 'i + Send + Clone + Into<E>, E: 'i + Send>(_: impl Ctx, value: List<T>, _element_ty: PhantomData<E>) -> List<E> {
value
.into_iter()
@@ -79,19 +79,19 @@ fn into_list<'i, T: 'i + Send + Clone + Into<E>, E: 'i + Send>(_: impl Ctx, valu
/// Moves a whole `Item` wire inside an element type like `Graphic` that embeds ranked values as its variants.
/// The attributes describe the element they arrived with, so they travel inside it and the fresh envelope starts empty.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn embed_item<'i, T: 'i + Send, E: 'i + Send + From<Item<T>>>(_: impl Ctx, value: Item<T>, _element_ty: PhantomData<E>) -> Item<E> {
Item::new_from_element(value.into())
}
/// The `List` counterpart of `embed_item`, each item moving whole inside its own embedding element.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn embed_list<'i, T: 'i + Send + Clone, E: 'i + Send + From<Item<T>>>(_: impl Ctx, value: List<T>, _element_ty: PhantomData<E>) -> List<E> {
value.into_iter().map(|item| Item::new_from_element(E::from(item))).collect()
}
/// The [`Convert`]-based counterpart of `into_item`, casting an `Item` wire's element to a connector's numeric element type.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn convert_item<'i, T: 'i + Send + Convert<E, ()>, E: 'i + Send>(ctx: impl Ctx + ExtractFootprint, value: Item<T>, _element_ty: PhantomData<E>) -> Item<E> {
let footprint = *ctx.try_footprint().unwrap_or(&Footprint::DEFAULT);
let (value, attributes) = value.into_parts();
@@ -100,7 +100,7 @@ fn convert_item<'i, T: 'i + Send + Convert<E, ()>, E: 'i + Send>(ctx: impl Ctx +
}
/// The `List` counterpart of `convert_item`, casting every element to the connector's numeric element type.
#[node_macro::node(category("_Adapters"), skip_impl)]
#[node_macro::node(category("_Adapters"), skip_impl, adapter(conversion))]
fn convert_list<'i, T: 'i + Send + Clone + Convert<E, ()>, E: 'i + Send + Clone>(ctx: impl Ctx + ExtractFootprint, value: List<T>, _element_ty: PhantomData<E>) -> List<E> {
let footprint = *ctx.try_footprint().unwrap_or(&Footprint::DEFAULT);
+1
View File
@@ -206,6 +206,7 @@ impl Preprocessor {
forwards_input,
inject_scope,
asynchronous,
adapter: _,
},
output_fields,
context_features,