Rust Lombok Macros
Treat
as a procedural-macro dependency and API generator, not as a complete Rust equivalent of Java Lombok. Confirm every generated method's signature, visibility, failure behavior, and compatibility boundary before removing boilerplate.
Scope
Use this skill for:
- DTOs, configuration snapshots, internal messages, test fixtures, and other data carriers where every field combination is valid;
- reducing mechanical methods with , , , , or ;
- excluding explicitly identified sensitive fields with ;
- reviewing existing attributes, upgrading the crate, or migrating handwritten methods.
Do not generate methods blindly for:
- domain entities, value objects, or security boundaries that require validation;
- methods with stable public contracts, custom errors, auditing, or side effects;
- or access paths that must preserve absence or error information;
- builders, default-value policies, serialization, comparisons, or hashing; version does not provide these capabilities.
Route dependency versions, features, and supply-chain policy to
; general procedural-macro implementation to
; API and security review to
; and broader test design to
.
Workflow
1. Establish the exact baseline
Inspect the project instead of assuming a version:
bash
rustc --version --verbose
cargo metadata --format-version 1
cargo tree -i lombok-macros -e features
Check
,
, the edition,
, and supported targets. Version
uses Edition 2024 and does not declare a Rust version, so compile it on the project's actual MSRV. Do not infer the selected version only from the current crates.io page or a GitHub release badge.
When adding the dependency, use
or an explicit reviewed version such as
cargo add lombok-macros@2.0.32
. Never copy
into
; Cargo dependencies use semantic version requirements, not a
keyword.
2. Triage examples by crate version
Do not assume that a blog post, generated answer, or older snippet matches the locked crate. In particular,
does not export a
derive. Rewrite examples such as
#[derive(Lombok, Debug, Clone)]
with the smallest current derives, for example
#[derive(Getter, Setter, Debug, Clone)]
, or use
only when mutable getters are also intended.
Keep capability ownership explicit:
- and are standard-library derives, not features generated by .
- is the crate-provided alternative when selected fields must be skipped.
- and implement from an existing representation; they do not make Debug output a stable presentation format.
- A procedural macro removes handwritten source but still adds compile-time work and generated behavior. Do not describe it as cost-free without qualification.
3. Inventory the pre-generation API
Record each field's existing method signature, visibility, ownership behavior, validation, side effects, errors, and callers. Replace a method only when the generated interface is equivalent or the contract change has been accepted.
Select the smallest derive:
| Requirement | Derive | Default risk |
|---|
| Read-only access | | Default and getters unwrap and return the inner value |
| Mutable borrowing | | Callers can bypass field invariants |
| Direct replacement | | Setters do not validate and add write access |
| All three accessor types | | The API surface is often wider than necessary |
| All-field construction | | The constructor is public by default; skipped fields use |
| Redacted debug output | | New sensitive fields still require explicit review |
| Debug reused as Display | | Structural output leaks easily and is not a stable user contract |
4. Make generated semantics explicit
Write attributes against the locked version's source and documentation. For
:
rust
use lombok_macros::{CustomDebug, Getter, New, Setter};
#[derive(Getter, Setter, New, CustomDebug)]
#[new(pub(crate))]
struct WorkerConfig {
#[get(pub)]
#[set(pub, type(Into<String>))]
name: String,
#[get(pub, type(copy))]
#[set(pub)]
workers: usize,
#[debug(skip)]
#[new(skip)]
token: String,
}
- Return for expensive values unless callers require ownership.
- Use only when cloning is part of the API contract.
- Use when value semantics are required for a field.
- Do not use the shorthand shown in part of the documentation; the parser requires .
- Use or for setter conversion, then compile the exact target type.
- Keep visibility minimal. Generated public methods become part of a library's semver surface.
Read Macro Reference for the complete derive and attribute matrix.
5. Preserve Rust invariants
- Keep handwritten or functions when construction validates state.
- Keep named methods when mutation requires validation, auditing, or coordinated field updates; do not generate or for those fields.
- For and , write explicit , , or container-returning methods. Reject default getters and when they introduce panic paths.
- Mark keys, tokens, passwords, and personal data with , then test formatted output. Review every new field later.
- Write manually for CLI, user-facing error, or protocol output. Restrict to internal diagnostics.
Read Adoption and Review when replacing existing methods or reviewing a pull request.
6. Test the expanded contract through callers
Add tests that call generated methods instead of only checking that the derive compiles:
bash
cargo fmt --all --check
cargo check --workspace --all-targets --all-features
cargo test --workspace --all-targets --all-features
cargo clippy --workspace --all-targets --all-features -- -D warnings
Cover method visibility, return types, setter chaining, constructor argument order,
defaults, Debug redaction, and the continued enforcement of handwritten invariants. Rerun these contract tests after dependency upgrades. Use
for manual inspection when useful, but do not make a nightly-only tool the sole quality gate.
Completion Criteria
- Confirm the dependency version, source, edition, and project MSRV with a real build.
- Use the smallest derive instead of defaulting to .
- Confirm every generated method's signature, visibility, ownership, and panic behavior.
- Keep validation, invariants, side effects, and error semantics in explicit Rust code.
- Prevent sensitive fields from reaching Debug or Display, and do not expose Debug as user output.
- Pass fmt, check, test, and Clippy with tests that call the generated API.
Resources
- Macro and Attribute Reference
- Adoption, Migration, and Review Checklist
- Execution Scenarios
- : a compilable contract example locked to .
Upstream Sources
- crates.io: confirm the published version, checksum, license, repository, features, and dependency metadata.
- docs.rs 2.0.32: inspect the public derive macros and version-specific rustdoc; avoid the moving URL during implementation.
- GitHub source: inspect implementation history, tags, issues, and unreleased changes. Compare the matching release tag, not only the default branch.
- Rust Reference: procedural macros
If prose, rustdoc examples, and behavior disagree, treat the source included in the selected crates.io package as authoritative for generated code, reproduce the behavior in a minimal compile test, and document the discrepancy. Never silently substitute GitHub
behavior for the version in
.
Data Privacy
This skill does not collect, store, or transmit user data. Dependency changes may access a registry. Confirm authorization before accessing a private registry, changing credentials, or publishing a crate.