Skip to content

Commit 39f8862

Browse files
committed
wip
1 parent 3136db7 commit 39f8862

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

module/core/former/spec.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,70 @@
1515
* **Storage:** An internal, temporary struct (`...FormerStorage`) that holds the intermediate state of the object being built.
1616
* **Definition:** A configuration struct (`...FormerDefinition`) that defines the types and `End` condition for a forming process.
1717
* **Subformer:** A `Former` instance used to build a part of a larger object.
18+
* **Target Type Categories:** The fundamental classification of Rust types the macro operates on (Structs vs Enums).
19+
* **Variant Structure Types:** The three categories of enum variant syntax (Unit, Tuple, Named) that determine parsing and generation rules.
20+
* **Behavioral Categories:** The five fundamental groupings that classify all possible Former macro usage patterns based on syntax structure and complexity. These categories drive macro implementation architecture, code generation strategies, and systematic validation (including test organization).
1821

1922
### 2. Core Behavioral Specification
2023

2124
This section defines the core user-facing contract of the `former` macro. The following logic tables and attribute definitions are the single source of truth for its behavior.
2225

23-
#### 2.1. Enum Variant Constructor Logic
26+
#### 2.1. Target Type Classification
2427

25-
The macro generates a static constructor method on the enum for each variant. The type of constructor is determined by the variant's structure and attributes according to the following rules:
28+
The `former` macro operates on two fundamental **Target Type Categories**, each with distinct behavioral rules and test coverage families:
29+
30+
##### 2.1.1. Structural Type Categories
31+
32+
* **Structs** - Regular Rust structs with named fields (`struct MyStruct { field: T }`)
33+
* **Enums** - Rust enums with variants, subdivided by **Variant Structure Types**:
34+
35+
##### 2.1.2. Enum Variant Structure Types
36+
37+
The macro classifies enum variants into three **Variant Structure Types** based on their field syntax:
38+
39+
* **Unit Variants** - No associated data (`Variant`)
40+
* **Tuple Variants** - Positional fields (`Variant(T1, T2)` or `Variant()`)
41+
* **Named Variants** - Named fields (`Variant { field: T }` or `Variant {}`)
42+
43+
Each Variant Structure Type has distinct parsing rules, generated code patterns, and behavioral specifications as defined in the rule tables below.
44+
45+
##### 2.1.3. Behavioral Categories
46+
47+
The macro architecture is organized around five fundamental **Behavioral Categories** that classify all Former usage patterns by syntax structure and complexity:
48+
49+
* **Struct Formers** - Regular Rust structs with named fields (foundational builder patterns)
50+
* **Unit Variant Formers** - Enum variants with no associated data (simple enum cases)
51+
* **Tuple Variant Formers** - Enum variants with positional fields (tuple-like syntax)
52+
* **Named Variant Formers** - Enum variants with named fields (struct-like syntax)
53+
* **Complex Scenario Formers** - Advanced combinations and cross-cutting patterns
54+
55+
Each Behavioral Category has distinct:
56+
- **Implementation patterns** (parsing logic, code generation strategies)
57+
- **API characteristics** (constructor types, setter methods, subformer behavior)
58+
- **Rule coverage** (applicable specification rules and attribute combinations)
59+
- **Validation approach** (systematic testing through corresponding test families)
60+
61+
##### 2.1.4. Implementation and Testing Organization
62+
63+
Each **Behavioral Category** corresponds to distinct implementation modules and systematic test validation:
64+
65+
| Behavioral Category | Target Type | Variant Structure Type | Rule Coverage | Implementation Focus | Test Family |
66+
| :--- | :--- | :--- | :--- | :--- | :--- |
67+
| Struct Formers | Structs | N/A | All struct rules | Core builder patterns | `struct_tests` |
68+
| Unit Variant Formers | Enums | Unit Variants | Rules 1a, 2a, 3a | Simple constructors | `enum_unit_tests` |
69+
| Tuple Variant Formers | Enums | Tuple Variants | Rules 1b, 1d, 1f, 2b, 2d, 2f, 3b, 3d, 3f | Positional setters | `enum_unnamed_tests` |
70+
| Named Variant Formers | Enums | Named Variants | Rules 1c, 1e, 1g, 2c, 2e, 2g, 3c, 3g | Named field setters | `enum_named_tests` |
71+
| Complex Scenario Formers | Enums | Mixed/Advanced | Cross-cutting rules | Edge case handling | `enum_complex_tests` |
72+
73+
This **Behavioral Category** system provides:
74+
- **Architectural guidance** for macro implementation organization
75+
- **API design consistency** across similar usage patterns
76+
- **Specification completeness** through systematic rule coverage
77+
- **Quality assurance** via comprehensive test validation families
78+
79+
#### 2.2. Enum Variant Constructor Logic
80+
81+
The macro generates a static constructor method on the enum for each variant. The type of constructor is determined by the variant's **Variant Structure Type** and attributes according to the following rules:
2682

2783
| Rule | Variant Structure | Attribute(s) | Generated Constructor Behavior |
2884
| :--- | :--- | :--- | :--- |
@@ -53,7 +109,7 @@ The macro generates a static constructor method on the enum for each variant. Th
53109
- Workaround: Use positional setters or mark variants as `#[scalar]` for direct construction
54110

55111

56-
#### 2.2. Standalone Constructor Behavior
112+
#### 2.3. Standalone Constructor Behavior
57113

58114
When the `#[standalone_constructors]` attribute is applied to an item, the return type of the generated top-level function(s) is determined by the usage of `#[former_ignore]` on its fields:
59115

@@ -62,11 +118,11 @@ When the `#[standalone_constructors]` attribute is applied to an item, the retur
62118

63119
**⚠️ Breaking Change Notice**: This specification represents the current behavior. Previous versions may have implemented different patterns where standalone constructors always returned `Former` instances. Manual implementations following the old pattern need to be updated to match the new specification for consistency.
64120

65-
#### 2.3. Attribute Reference
121+
#### 2.4. Attribute Reference
66122

67123
The following attributes control the behavior defined in the logic tables above.
68124

69-
##### 2.3.1. Item-Level Attributes
125+
##### 2.4.1. Item-Level Attributes
70126

71127
| Attribute | Purpose & Behavior |
72128
| :--- | :--- |
@@ -76,7 +132,7 @@ The following attributes control the behavior defined in the logic tables above.
76132
| `#[standalone_constructors]` | Generates top-level constructor functions. |
77133
| `#[debug]` | Prints the macro's generated code to the console at compile time. |
78134

79-
##### 2.3.2. Field-Level / Variant-Level Attributes
135+
##### 2.4.2. Field-Level / Variant-Level Attributes
80136

81137
| Attribute | Purpose & Behavior |
82138
| :--- | :--- |
@@ -87,7 +143,7 @@ The following attributes control the behavior defined in the logic tables above.
87143
| `#[subform_entry]` | Generates a method returning a subformer for a single entry of a collection. |
88144
| `#[former_ignore]` | Excludes a field from being a parameter in `#[standalone_constructors]` functions. The field will use its default value or remain unset. |
89145

90-
##### 2.3.3. Attribute Precedence and Interaction Rules
146+
##### 2.4.3. Attribute Precedence and Interaction Rules
91147

92148
1. **Subform vs. Scalar:** Subform attributes (`#[subform_scalar]`, `#[subform_collection]`, `#[subform_entry]`) take precedence over `#[scalar]`. If both are present, the subform behavior is implemented, and a scalar setter is **not** generated unless explicitly requested via `#[scalar(setter = true)]`.
93149
2. **Setter Naming:** If a `name` is provided (e.g., `#[scalar(name = new_name)]`), it overrides the default setter name derived from the field's identifier.

0 commit comments

Comments
 (0)