|
| 1 | +# Simple Builder Proc-Macro PRD |
| 2 | + |
| 3 | +## 1. Objective |
| 4 | + |
| 5 | +To provide a simple and ergonomic solution for creating builder patterns in Rust. This proc-macro will automate the generation of a basic builder, reducing boilerplate code for simple use cases. |
| 6 | + |
| 7 | +## 2. Background |
| 8 | + |
| 9 | +The builder pattern is a useful way to construct complex objects. However, writing the builder struct and its methods can be tedious. This proc-macro automates the creation of a simple builder. |
| 10 | + |
| 11 | +## 3. User Persona |
| 12 | + |
| 13 | +This tool is for Rust developers who want a quick and easy way to create a builder for their structs without the complexity of a full type-state implementation. |
| 14 | + |
| 15 | +## 4. API Definition |
| 16 | + |
| 17 | +The public API will consist of a single derive macro: |
| 18 | + |
| 19 | +```rust |
| 20 | +use simple_builder::SimpleBuilder; |
| 21 | + |
| 22 | +#[derive(SimpleBuilder, Default)] |
| 23 | +#[builder_name = "MyCustomBuilderName"] // Optional: to rename the builder struct |
| 24 | +pub struct MyStruct { |
| 25 | + // ... fields |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +- `#[derive(SimpleBuilder)]`: The main entry point. It will trigger the builder generation. |
| 30 | +- `#[builder_name = "..."]` (optional attribute): Allows the user to specify a custom name for the generated builder struct. If not provided, it will default to `[StructName]Builder`. |
| 31 | + |
| 32 | +### Constraints |
| 33 | + |
| 34 | +- The target struct **must** implement the `Default` trait. |
| 35 | + |
| 36 | +## 5. Requirements & Features |
| 37 | + |
| 38 | +### 5.1. "With Defaults" Builder Logic |
| 39 | + |
| 40 | +The builder will follow a "with defaults" strategy. This means: |
| 41 | + |
| 42 | +- The `build()` method can be called at any time. |
| 43 | +- If a field has been set using its `with_...` method, the builder will use that value. |
| 44 | +- If a field has not been set, the builder will use the value from the struct's `Default` implementation. |
| 45 | + |
| 46 | +### 5.2. Generated Code |
| 47 | + |
| 48 | +The macro will generate: |
| 49 | + |
| 50 | +1. A public `...Builder` struct with `Option` fields. |
| 51 | +2. A `new()` method to create an empty builder. |
| 52 | +3. A `with_<field_name>()` method for each field in the target struct. |
| 53 | +4. A `build()` method that constructs the target struct, intelligently combining user-provided values with default values. |
| 54 | + |
| 55 | +## 6. Acceptance Criteria |
| 56 | + |
| 57 | +To consider this feature complete and correct, the following criteria must be met: |
| 58 | + |
| 59 | +1. **Compilation:** A struct decorated with `#[derive(SimpleBuilder, Default)]` must compile successfully. |
| 60 | +2. **Builder Generation:** The corresponding `...Builder` struct must be generated and be publicly accessible. |
| 61 | +3. **`new()` Method:** The `...Builder::new()` method must exist and return a new builder instance. |
| 62 | +4. **`with_...` Methods:** Each field in the target struct must have a corresponding `with_<field_name>()` method on the builder. |
| 63 | +5. **`build()` Method:** The `build()` method must be callable on the builder at any time. |
| 64 | +6. **Correctness (Set Fields):** When a field is set via its `with_...` method, the `build()` method must produce a struct with that exact value for that field. |
| 65 | +7. **Correctness (Unset Fields):** When a field is not set, the `build()` method must produce a struct where that field has the value from the `Default` implementation. |
| 66 | + |
| 67 | +## 7. Out of Scope (Future Work) |
| 68 | + |
| 69 | +- **Type-State Builder:** A more advanced builder that uses the type-state pattern to enforce compile-time correctness. |
| 70 | +- **Generic Structs:** Support for structs with generic parameters. |
| 71 | +- **Required Fields:** An attribute to mark certain fields as required. |
| 72 | +- **Validation:** Adding validation logic to the `build()` method. |
0 commit comments