turtle-lib-macros
Summary of Changes
Preserve Parameter Pattern in Macro Expansion:
In
turtle-lib-macros/src/lib.rs
, updated the helper function generation when has_turtle_param is true:
rust
let param = &input_fn.sig.inputs[0];
quote! {
}
This retains the exact parameter pattern and identifier (e.g. t: &mut
TurtlePlan, mut t: &mut TurtlePlan, etc.) rather than replacing it with
turtle: &mut turtle_lib::TurtlePlan.
Preserves user function visibility (#fn_vis) for named non-main helper
functions.
Validation of Parameters:
In validate_input, explicitly reject self receivers (FnArg::Receiver)
with an informative error message.
Reject unsupported patterns (e.g., tuple destructuring or struct
patterns) during validation, only accepting identifier patterns
(syn::Pat::Ident) and wildcards (syn::Pat::Wild).
Macro Expansion Testing:
Factored macro expansion logic into turtle_main_impl(&args, input) ->
Result<proc_macro2::TokenStream, syn::Error> so macro expansions can be
parsed into syn::File and verified directly in unit tests.
Added unit tests verifying:
Expansion with custom parameter names like t: &mut TurtlePlan preserves
t
Expansion with mutable parameters like mut t: &mut TurtlePlan
Expansion when the function name is main renames to __turtle_main_draw
while preserving parameter t
Expansion for zero-argument functions generates parameter turtle
Rejection of &mut self
Rejection of unsupported destructuring patterns like (a, b)
Verification
cargo test --package turtle-lib-macros: All 13 tests passed.
cargo clippy --package turtle-lib-macros -- -Wclippy::pedantic: Passed
with 0 warnings.
cargo test --all-targets --all-features: All unit and doc tests across
the workspace passed.
cargo check --package turtle-lib --examples: All 30 examples compiled
cleanly.
turtle-lib-macros
Procedural macros for turtle-lib.
turtle_main Macro
The turtle_main macro simplifies creating turtle graphics programs by automatically setting up:
- The Macroquad window
- Turtle initialization
- The main rendering loop
- Quit handling (ESC or Q keys)
Usage
With a function parameter:
use macroquad::prelude::*;
use turtle_lib::*;
#[turtle_main("My Drawing")]
fn my_drawing(turtle: &mut TurtlePlan) {
turtle.set_pen_color(RED);
turtle.forward(100.0);
turtle.right(90.0);
turtle.forward(100.0);
}
With inline code:
use macroquad::prelude::*;
use turtle_lib::*;
#[turtle_main("My Drawing")]
fn my_drawing() {
turtle.set_pen_color(RED);
turtle.forward(100.0);
turtle.right(90.0);
turtle.forward(100.0);
}
What it does
The macro expands your code into a full Macroquad application with:
#[macroquad::main]attribute for window creation- Turtle instance creation
- TurtleApp initialization with your commands
- A main loop that:
- Clears the background to WHITE
- Updates the turtle app
- Renders the drawing
- Shows "Press ESC or Q to quit" message
- Handles quit keys
Benefits
- Less boilerplate: No need to write the same loop structure in every example
- Consistent UI: All examples have the same quit behavior
- Beginner-friendly: Makes turtle graphics examples more approachable
- Focus on drawing: Your code focuses on the turtle commands, not the framework
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.