Procedural Macros
Relevant source files
The following files were used as context for generating this wiki page:
Procedural macros in Graphite serve as essential compile-time metaprogramming tools that facilitate the project's complex architecture, particularly within the Graphene node engine. These macros allow the codebase to remain concise by automating the generation of repetitive code structures, such as those required for node registration and serialized discriminant handling.
The project utilizes several specialized procedural macro crates, each tailored to specific domains like the editor backend, the node graph, and the Rawkit library. These macros leverage the standard Rust ecosystem for metaprogramming, including syn, quote, and proc-macro2.
Sources: proc-macros/Cargo.toml, node-graph/node-macro/Cargo.toml, website/content/volunteer/guide/codebase-overview/_index.md:31-35
Macro Crate Architecture
The Graphite project divides its procedural macro logic into distinct crates to maintain a clear separation of concerns between different layers of the application.
| Crate Name | Purpose | Key Dependencies |
|---|---|---|
graphite-proc-macros | General editor macros, including discriminant serialization. | syn, quote, proc-macro2, serde |
node-macro | Logic for defining and registering nodes within the Graphene engine. | syn, quote, convert_case, strum |
rawkit-proc-macros | Procedural macros specifically for the Rawkit library. | syn, quote, toml |
Sources: proc-macros/Cargo.toml:1-25, node-graph/node-macro/Cargo.toml:1-25, libraries/rawkit/rawkit-proc-macros/Cargo.toml:1-20
Crate Dependency Flow
Procedural macro crates are consumed by their respective functional modules. For instance, the editor crate depends on graphite-proc-macros for compile-time logic generation.
The diagram shows how specialized macro crates provide compile-time services to the main application components.
Sources: proc-macros/Cargo.toml:16-17, node-graph/node-macro/Cargo.toml:1-10, libraries/rawkit/rawkit-proc-macros/Cargo.toml:1-10
Implementation Details
Node Engine Macros
The node-macro crate is critical for the Graphene engine. It uses strum and convert_case to handle the naming and iteration of node types, which is essential for the Graphene programming language and runtime environment. It also includes a disable-registration feature to toggle node registration during specific build configurations.
Sources: node-graph/node-macro/Cargo.toml:12-25, website/content/volunteer/guide/codebase-overview/_index.md:31-35
Editor and Serde Integration
The graphite-proc-macros crate includes a serde-discriminant feature, enabled by default. This suggests specialized handling for enum discriminants during serialization/deserialization, likely supporting the Message System which relies on mapped data structures for backend-to-frontend communication.
Sources: proc-macros/Cargo.toml:11-13, website/content/volunteer/guide/codebase-overview/_index.md:50-55
Contribution and Quality Standards
When working with or extending procedural macros, contributors must follow the project's code quality guidelines. Macros are subject to the same strict linting and formatting rules as standard code.
- Clippy: Must be clean of warnings.
- Naming: Full words are preferred (e.g.,
generate_document_formatovergen_doc_fmt). - Formatting: Standard
cargo fmtapplies, and macro-generated code should adhere to the project's import grouping rules (e.g., no::inside{}).
Sources: website/content/volunteer/guide/starting-a-task/code-quality-guidelines.md:7-12, 14-20, 50-68
Summary
Procedural macros in Graphite are a foundational element that enables the high-performance, node-based nature of the editor. By segregating macro logic into node-macro, graphite-proc-macros, and rawkit-proc-macros, the project maintains a modular architecture that supports the Graphene engine and the editor's Message System. These tools automate complex tasks like node registration and serialization, ensuring the codebase remains maintainable while providing powerful compile-time guarantees.