Procedural Macros (sea-schema-derive)
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
sea-schema-derive is a dedicated procedural macro crate within the SeaSchema workspace designed to provide automated implementations of the Name trait. This trait is essential for the SeaSchema ecosystem as it facilitates the mapping between Rust identifiers (structs or enums representing database entities) and their corresponding SQL string names.
The crate serves as a utility for both the core sea-schema library and the sea-schema-sync variant, ensuring consistent naming conventions and reducing boilerplate code when defining database schemas, tables, and columns in Rust.
Sources: sea-schema-derive/Cargo.toml:8, README.md:15-20, src/lib.rs:18
Architecture and Integration
sea-schema-derive is configured as a procedural macro library (proc-macro = true). It operates as a member of the larger SeaSchema workspace and is integrated as a dependency in the primary packages to support schema definition and discovery workflows.
Workspace Relationship
The macro crate is positioned as a foundational tool that other modules rely on for type-safe schema management.
The diagram above shows the dependency relationship where the derive macro crate provides code generation capabilities to the core and sync modules of the project.
Sources: Cargo.toml:34, sea-schema-sync/Cargo.toml:23, README.md:20-25
Component Logic
The crate leverages standard Rust procedural macro libraries to parse Abstract Syntax Trees (AST) and generate code.
Core Dependencies
The functionality is built upon several industry-standard crates:
- syn: Used for parsing Rust source code into a format the macro can manipulate.
- quote: Used for turning Rust tokens back into source code.
- heck: Specifically included for string case conversion (e.g., converting PascalCase Rust structs to snake_case SQL table names).
- proc-macro2: Provides a wrapper around the compiler's proc-macro API for better flexibility.
Sources: sea-schema-derive/Cargo.toml:17-21
Processing Flow
The derive macro follows a standard transformation pipeline to implement the Name trait for a given item.
This flowchart represents the internal logic of the macro, focusing on the transition from a Rust identifier to a generated trait implementation.
Sources: sea-schema-derive/Cargo.toml:17-21, CHANGELOG.md:104
Configuration Summary
The following table outlines the technical specifications of the sea-schema-derive crate as defined in its configuration.
| Property | Value | Description |
|---|---|---|
| Crate Name | sea-schema-derive | The unique identifier for the procedural macro crate. |
| Version | 0.3.0 | Current stable version. |
| Edition | 2024 | Rust edition used for development. |
| Primary Trait | Name | The trait for which the macro generates implementations. |
| Rust Version | 1.85.0 | Minimum supported Rust compiler version. |
Sources: sea-schema-derive/Cargo.toml:2-15
Development Evolution
The crate has undergone significant updates to maintain compatibility with the Rust ecosystem and the SeaQuery backend. Notable changes include:
- Upgrade to Syn 2: The macro was updated from
synversion 1 to version 2 to support modern Rust parsing requirements. - Heck Upgrade: The string manipulation logic was updated to use
heck 0.4for improved case conversion accuracy. - Rust Edition: The project transitioned to the
2024edition to leverage the latest language features.
Sources: CHANGELOG.md:4, CHANGELOG.md:88, CHANGELOG.md:103
Conclusion
sea-schema-derive provides the necessary automation to link Rust types with database schema identifiers via the Name trait. By utilizing a robust stack of parsing and generation tools, it ensures that the SeaSchema ecosystem remains DRY (Don't Repeat Yourself) while providing the flexibility needed for multi-database (MySQL, Postgres, SQLite) support.