rust-lombok-macros
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Lombok Macros
Rust Lombok 宏
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.
lombok-macros将 视为 procedural macro 依赖和 API 生成器,而不是 Java Lombok 在 Rust 中的完整等价物。在移除样板代码之前,确认每个生成方法的签名、可见性、失败行为和兼容性边界。
lombok-macrosScope
适用范围
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 ,
Getter,GetterMut,Setter, orData;New - excluding explicitly identified sensitive fields with ;
CustomDebug - 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
Optionaccess paths that must preserve absence or error information;Result - builders, default-value policies, serialization, comparisons, or hashing; version does not provide these capabilities.
2.0.32
Route dependency versions, features, and supply-chain policy to ; general procedural-macro implementation to ; API and security review to ; and broader test design to .
rust-cargo-buildrust-macrosrust-code-reviewrust-testing在以下场景中使用此 skill:
- DTO、配置快照、内部消息、测试 fixture,以及其他所有字段组合均有效的数据载体;
- 使用 、
Getter、GetterMut、Setter或Data减少机械化方法;New - 使用 排除已明确识别的敏感字段;
CustomDebug - 审查现有属性、升级 crate,或迁移手写方法。
不要盲目为以下对象生成方法:
- 需要验证的领域实体、值对象或安全边界;
- 具有稳定公共契约、自定义错误、审计要求或副作用的方法;
- 必须保留缺失或错误信息的 或
Option访问路径;Result - builder、默认值策略、序列化、比较或哈希;版本 不提供这些能力。
2.0.32
将依赖版本、feature 和供应链策略交由 处理;将通用 procedural macro 实现交由 处理;将 API 和安全审查交由 处理;将更广泛的测试设计交由 处理。
rust-cargo-buildrust-macrosrust-code-reviewrust-testingWorkflow
工作流
1. Establish the exact baseline
1. 确定准确基线
Inspect the project instead of assuming a version:
bash
rustc --version --verbose
cargo metadata --format-version 1
cargo tree -i lombok-macros -e featuresCheck , , 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.
Cargo.tomlCargo.lockrust-version2.0.32When adding the dependency, use or an explicit reviewed version such as . Never copy into ; Cargo dependencies use semantic version requirements, not a keyword.
cargo add lombok-macroscargo add lombok-macros@2.0.32lombok-macros = "latest"Cargo.tomllatest检查项目,不要假定版本:
bash
rustc --version --verbose
cargo metadata --format-version 1
cargo tree -i lombok-macros -e features检查 、、edition、 和支持的 target。版本 使用 Edition 2024,且未声明 Rust 版本,因此应在项目实际使用的 MSRV 上编译。不要仅根据当前 crates.io 页面或 GitHub release badge 推断所选版本。
Cargo.tomlCargo.lockrust-version2.0.32添加依赖时,使用 ,或使用经过审查的明确版本,例如 。绝不要将 复制到 ;Cargo 依赖使用语义版本要求,而不是 关键字。
cargo add lombok-macroscargo add lombok-macros@2.0.32lombok-macros = "latest"Cargo.tomllatest2. Triage examples by crate version
2. 按 crate 版本分类示例
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 with the smallest current derives, for example , or use only when mutable getters are also intended.
2.0.32Lombok#[derive(Lombok, Debug, Clone)]#[derive(Getter, Setter, Debug, Clone)]DataKeep capability ownership explicit:
- and
Debugare standard-library derives, not features generated byClone.lombok-macros - is the crate-provided alternative when selected fields must be skipped.
CustomDebug - and
DisplayDebugimplementDisplayDebugFormatfrom an existingDisplayrepresentation; they do not make Debug output a stable presentation format.Debug - A procedural macro removes handwritten source but still adds compile-time work and generated behavior. Do not describe it as cost-free without qualification.
不要假定博客文章、生成的答案或旧代码片段与锁定版本的 crate 一致。尤其是, 不导出 derive。请使用当前最小的 derive 重写类似 的示例,例如 ;只有在还需要可变 getter 时才使用 。
2.0.32Lombok#[derive(Lombok, Debug, Clone)]#[derive(Getter, Setter, Debug, Clone)]Data明确各项能力的归属:
- 和
Debug是标准库 derive,并非Clone生成的 feature。lombok-macros - 是 crate 提供的替代方案,用于跳过指定字段。
CustomDebug - 和
DisplayDebug基于现有的DisplayDebugFormat表示实现Debug;它们不会让 Debug 输出成为稳定的展示格式。Display - procedural macro 虽然移除了手写源代码,但仍会增加编译期工作和生成行为。不要在没有限定条件的情况下称其完全没有成本。
3. Inventory the pre-generation API
3. 盘点生成前的 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 |
| 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 |
记录每个字段对应的现有方法签名、可见性、所有权行为、验证、副作用、错误和调用方。只有在生成的接口等价,或契约变更已获得接受时,才能替换原有方法。
选择最小的 derive:
| 需求 | Derive | 默认风险 |
|---|---|---|
| 只读访问 | | 默认的 |
| 可变借用 | | 调用方可以绕过字段不变量 |
| 直接替换 | | setter 不会验证数据,并增加写入访问能力 |
| 三种访问器全部需要 | | API 表面通常比实际需要更宽 |
| 使用所有字段构造 | | 构造函数默认公开;被跳过的字段使用 |
| 隐藏 Debug 输出中的内容 | | 新增敏感字段后仍需明确审查 |
| 将 Debug 复用为 Display | | 结构化输出容易泄露信息,且不是稳定的用户契约 |
4. Make generated semantics explicit
4. 明确生成语义
Write attributes against the locked version's source and documentation. For :
2.0.32rust
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.
&T - Use only when cloning is part of the API contract.
type(clone) - Use when value semantics are required for a
type(copy)field.Copy - Do not use the shorthand shown in part of the documentation; the
#[get(pub, clone)]parser requires2.0.32.type(clone) - Use or
type(Into<T>)for setter conversion, then compile the exact target type.type(AsRef<T>) - Keep visibility minimal. Generated public methods become part of a library's semver surface.
Read Macro Reference for the complete derive and attribute matrix.
根据锁定版本的源代码和文档编写属性。对于 :
2.0.32rust
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,
}- 对开销较大的值返回 ,除非调用方明确需要所有权。
&T - 仅当克隆属于 API 契约的一部分时,才使用 。
type(clone) - 当 字段需要值语义时,使用
Copy。type(copy) - 不要使用部分文档中展示的 简写;
#[get(pub, clone)]解析器要求使用2.0.32。type(clone) - 使用 或
type(Into<T>)进行 setter 转换,然后编译准确的目标类型。type(AsRef<T>) - 保持最小可见性。生成的 public 方法会成为 library 的 semver API 表面。
阅读 宏参考,了解完整的 derive 和属性矩阵。
5. Preserve Rust invariants
5. 保持 Rust 不变量
- Keep handwritten or
newfunctions when construction validates state.try_new - Keep named methods when mutation requires validation, auditing, or coordinated field updates; do not generate or
Setterfor those fields.GetterMut - For and
Option, write explicitResult,as_ref, or container-returning methods. Reject default getters andas_derefwhen they introduce panic paths.type(deref) - Mark keys, tokens, passwords, and personal data with , then test formatted output. Review every new field later.
#[debug(skip)] - Write manually for CLI, user-facing error, or protocol output. Restrict
Displayto internal diagnostics.DisplayDebug
Read Adoption and Review when replacing existing methods or reviewing a pull request.
- 当构造过程需要验证状态时,保留手写的 或
new函数。try_new - 当修改操作需要验证、审计或协调多个字段更新时,保留具名方法;不要为这些字段生成 或
Setter。GetterMut - 对于 和
Option,编写显式的Result、as_ref或返回容器的方法。当默认 getter 和as_deref会引入 panic 路径时,拒绝使用它们。type(deref) - 使用 标记 key、token、password 和个人数据,然后测试格式化输出。之后每新增一个字段,都应重新审查。
#[debug(skip)] - 对于 CLI、面向用户的错误或协议输出,手动编写 。仅将
Display限制在内部诊断场景中使用。DisplayDebug
替换现有方法或审查 pull request 时,请阅读采用与审查。
6. Test the expanded contract through callers
6. 通过调用方测试扩展后的契约
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 warningsCover 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.
new(skip)cargo expand添加调用生成方法的测试,而不只是检查 derive 能否编译:
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覆盖方法可见性、返回类型、setter 链式调用、构造函数参数顺序、 默认值、Debug 信息隐藏,以及手写不变量是否仍被持续执行。依赖升级后应重新运行这些契约测试。必要时可使用 进行手动检查,但不要将仅适用于 nightly 的工具作为唯一的质量门禁。
new(skip)cargo expandCompletion Criteria
完成标准
- Confirm the dependency version, source, edition, and project MSRV with a real build.
- Use the smallest derive instead of defaulting to .
Data - 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.
- 通过真实构建确认依赖版本、来源、edition 和项目 MSRV。
- 使用最小的 derive,而不是默认采用 。
Data - 确认每个生成方法的签名、可见性、所有权和 panic 行为。
- 在显式的 Rust 代码中保留验证、不变量、副作用和错误语义。
- 防止敏感字段进入 Debug 或 Display,并且不要将 Debug 作为用户输出公开。
- fmt、check、test 和 Clippy 全部通过,并使用调用生成 API 的测试进行验证。
Resources
资源
- Macro and Attribute Reference
- Adoption, Migration, and Review Checklist
- Execution Scenarios
- : a compilable contract example locked to
examples/golden-lombok/.2.0.32
- 宏与属性参考
- 采用、迁移与审查清单
- 执行场景
- :一个锁定到
examples/golden-lombok/的可编译契约示例。2.0.32
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.
latest - 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 .
masterCargo.lock- crates.io:确认已发布版本、校验和、license、repository、feature 和依赖元数据。
- docs.rs 2.0.32:检查公开的 derive macro 和特定版本的 rustdoc;实现期间避免使用会指向最新版本的 URL。
latest - GitHub source:检查实现历史、tag、issue 和尚未发布的变更。应比较匹配的 release tag,而不是只比较默认分支。
- Rust Reference: procedural macros
如果说明文字、rustdoc 示例和实际行为之间存在冲突,应以所选 crates.io package 中包含的源代码作为生成代码的权威依据,在最小编译测试中复现该行为,并记录差异。绝不能将 GitHub 分支的行为静默替换为 中版本的行为。
masterCargo.lockData 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.
此 skill 不会收集、存储或传输用户数据。依赖项变更可能会访问 registry。在访问私有 registry、修改凭据或发布 crate 之前,必须确认已获得授权。