Skip to content

Commit 3365229

Browse files
committed
[.github/workflows/ci-cargo.yml] Add missing clippy ; [README.md] Rework mermaid ; add rust-analyzer links
1 parent 97340da commit 3365229

File tree

2 files changed

+70
-88
lines changed

2 files changed

+70
-88
lines changed

.github/workflows/ci-cargo.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
run: |
2424
rustup override set nightly
2525
rustup component add rustfmt
26+
rustup component add clippy
2627
2728
- name: Check Formatting
2829
run: cargo fmt -- --check

README.md

Lines changed: 69 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ cdd-rust: OpenAPI ↔ Rust
66

77
**Compiler Driven Development (CDD)** for Rust.
88

9-
**cdd-rust** bridges the gap between your **Database**, **Rust Code**, and **OpenAPI Specifications**. Unlike traditional generators that produce untouchable code in a "generated" folder, `cdd-rust` uses advanced AST parsing to surgically patch your *existing* source files, strictly Typed handlers, and integration tests.
9+
**cdd-rust** creates a symbiotic link between your **Database**, **Rust Code**, and **OpenAPI Specifications**. Unlike traditional generators that overwrite files or hide code in "generated" directories, `cdd-rust` uses advanced AST parsing (`ra_ap_syntax`) to surgically patch your *existing* source files, strictly typed handlers, and integration tests.
1010

11-
## ⚡️ The CDD Loop
11+
It supports two distinct workflows:
12+
1. **Scaffold | Patch (OpenAPI ➔ Rust):** Generate/update Actix handlers, routes, and Diesel models from OpenAPI.
13+
2. **Reflect & Sync (Rust ➔ OpenAPI):** Generate OpenAPI specifications from your actual source code.
1214

13-
Automate the repetitive parts of building robust web services with **Actix Web**, **Diesel**, and **Postgres**.
15+
## ⚡️ The CDD Loop
1416

1517
```mermaid
1618
%%{init: {
@@ -25,95 +27,83 @@ Automate the repetitive parts of building robust web services with **Actix Web**
2527
}}%%
2628
2729
graph TD
28-
%% Nodes
29-
OpenAPI([OpenAPI]):::yellow
30-
Handlers(Actix Handlers):::blue
31-
Models(Diesel Models):::green
32-
DB[(Database)]:::navy
30+
%% --- Section 1: Route Logic ---
3331
34-
%% Flow 0 & 1: Downward (Scaffold & Patch)
35-
OpenAPI -->|"0. Scaffold (New) |<br/> 1. Patch (Existing)"| Handlers
32+
%% Node: OAS Path
33+
OasPath("<strong>OpenAPI Path (YAML)</strong><br/>/users/{id}:<br/>&nbsp;&nbsp;get:<br/>&nbsp;&nbsp;&nbsp;&nbsp;parameters:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: id<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in: path<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;schema: {format: uuid}"):::yellow
3634
37-
%% Connection
38-
Handlers -- "Uses Strictly Typed Structs" --> Models
35+
%% Node: Actix Handler
36+
Actix("<strong>Actix Handler (Rust)</strong><br/>async fn get_user(<br/>&nbsp;&nbsp;id: web::Path&lt;Uuid&gt;<br/>) -> impl Responder {<br/>&nbsp;&nbsp;/* Logic */<br/>}"):::blue
3937
40-
%% Flow 2: Upward (Reflection)
41-
Models -.->|"2. Derives Schema (Reflection)"| OpenAPI
38+
%% Flow: Down (Scaffold) & Up (Reflect)
39+
OasPath ==>|"1. SCAFFOLD / PATCH<br/>(Injects Handler & Route Signature)"| Actix
40+
Actix -.->|"2. REFLECT / GENERATE<br/>(Extracts Paths via AST)"| OasPath
4241
43-
%% Database Sync
44-
DB == "Syncs SQL Columns" ==> Models
42+
%% --- Spacer to force vertical layout ---
43+
Actix ~~~ OasSchema
4544
46-
%% Styles
47-
classDef yellow fill:#f9ab00,stroke:#20344b,stroke-width:2px,color:#ffffff,font-family:'Google Sans Medium',font-size:16px;
48-
classDef blue fill:#4285f4,stroke:#20344b,stroke-width:2px,color:#ffffff,font-family:'Google Sans Medium',font-size:16px;
49-
classDef green fill:#34a853,stroke:#20344b,stroke-width:2px,color:#ffffff,font-family:'Google Sans Medium',font-size:16px;
50-
classDef navy fill:#20344b,stroke:#20344b,stroke-width:2px,color:#ffffff,font-family:'Google Sans Medium',font-size:16px;
45+
%% --- Section 2: Data Models ---
5146
52-
linkStyle default stroke:#20344b,stroke-width:2px;
53-
```
47+
%% Node: OAS Schema
48+
OasSchema("<strong>OpenAPI Schema (YAML)</strong><br/>components:<br/>&nbsp;&nbsp;schemas:<br/>&nbsp;&nbsp;&nbsp;&nbsp;User:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: object<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;properties:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email: {type: string}"):::yellow
5449
55-
---
50+
%% Node: Diesel Model
51+
Diesel("<strong>Diesel Model (Rust)</strong><br/>#[derive(ToSchema)]<br/>struct User {<br/>&nbsp;&nbsp;id: Uuid,<br/>&nbsp;&nbsp;email: String<br/>}"):::green
5652
57-
## 🚀 The Philosophy
53+
%% Flow: Down (Scaffold) & Up (Reflect)
54+
OasSchema ==>|"1. SCAFFOLD / PATCH<br/>(Injects Fields & Types)"| Diesel
55+
Diesel -.->|"2. REFLECT / GENERATE<br/>(Derives Attributes)"| OasSchema
5856
59-
**Compiler Driven Development** solves synchronization issues across language boundaries (Backend $\leftrightarrow$ Frontend $\leftrightarrow$ Docs).
57+
%% --- Styles ---
58+
classDef yellow fill:#f9ab00,stroke:#20344b,stroke-width:2px,color:#ffffff,font-family:'Consolas',font-size:14px,text-align:left;
59+
classDef blue fill:#4285f4,stroke:#20344b,stroke-width:2px,color:#ffffff,font-family:'Consolas',font-size:14px,text-align:left;
60+
classDef green fill:#34a853,stroke:#20344b,stroke-width:2px,color:#ffffff,font-family:'Consolas',font-size:14px,text-align:left;
6061
61-
* **Single Source of Truth:** Your Code + Attributes *are* the documentation. Your Database *is* the source of your models.
62-
* **No Magic Folders:** We don't hide code. We generate code you can read, debug, and commit.
63-
* **Lossless Patching:** Powered by `ra_ap_syntax` (Rust Analyzer), we edit your files without breaking your manual formatting or comments.
64-
* **Contract Safety:** Automated updating of contract tests ensures your implementation strictly adheres to the API definition.
62+
linkStyle default stroke:#20344b,stroke-width:2px;
63+
```
6564

6665
---
6766

68-
## 🛠 Features
69-
70-
### 1. Database & Model Sync (`sync`)
71-
Keeps your Rust structs in perfect harmony with your Postgres schema.
72-
* **Diesel Integration:** Wraps `dsync` to generate raw structs from `schema.rs`.
73-
* **Attribute Injection:** Automatically parses generated structs and injects `utoipa::ToSchema`, `serde::Serialize`, and `serde::Deserialize`.
74-
* **Result:** A fully documented, OpenAPI-ready Rust struct generated directly from your DB.
75-
76-
### 2. Contract Test Generation (`test-gen`)
77-
Treats your application as a black box to ensure spec compliance.
78-
* **Route Parsing:** Reads your `openapi.yaml`.
79-
* **Test Scaffolding:** Generates `tests/api_contracts.rs` containing `#[actix_web::test]` functions.
80-
* **Smart Mocking:** Automatically generates dummy values for `Uuid`, `Date`, and primitives to satisfy route parameters during testing.
81-
* **Schema Validation:** Includes logic to validate response bodies against the JSON Schema definitions.
82-
83-
### 3. AST-Based Patching (Core)
84-
The engine under the hood (`cdd-core`) provides intelligent code manipulation:
85-
* **Smart Injection:** Adds fields to structs and lines to functions respecting indentation and comma placement.
86-
* **Structural Diffing:** configuration-aware diffing between target specs and actual implementations.
87-
* **Route Registration:** Injects `cfg.service(...)` calls into your Actix config without disrupting existing logic.
67+
## 🚀 Features
68+
69+
### 1. Zero-Boilerplate Scaffolding (OpenAPI ➔ Rust)
70+
Stop manually writing repetitive handler signatures. `cdd-rust` reads your spec and generates strictly typed code.
71+
* **Handler Scaffolding:** Transforms OpenAPI paths into `async fn` signatures with correct extractors:
72+
* Path variables ➔ `web::Path<Uuid>`
73+
* Query strings ➔ `web::Query<Value>`
74+
* Request bodies ➔ `web::Json<T>`
75+
* **Route Registration:** Surgically injects `cfg.service(...)` calls into your `routes.rs` configuration using AST analysis, preserving existing logic.
76+
* **Non-Destructive Patching:** Uses [`ra_ap_syntax`](https://docs.rs/ra_ap_syntax/) (part of official [rust-lang/rust-analyzer](https://github.com/rust-lang/rust-analyzer)) to edit files safely. It respects your manual comments and formatting.
77+
78+
### 2. Source-of-Truth Reflection (Rust ➔ OpenAPI)
79+
Keep your documentation alive. Your Rust code *is* the spec.
80+
* **Database Sync:** Wraps `dsync` to generate Rust structs directly from your Postgres `schema.rs`.
81+
* **Attribute Injection:** Automatically parses structs and injects `#[derive(ToSchema)]` and `#[serde(...)]` attributes.
82+
* **Type Mapping:** Maps Rust types (`Uuid`, `chrono::NaiveDateTime`) back to OpenAPI formats automatically.
83+
84+
### 3. Contract Safety (`test-gen`)
85+
Ensure your implementation actually matches the spec.
86+
* **Test Generation:** Generates `tests/api_contracts.rs` based on your `openapi.yaml`.
87+
* **Smart Mocking:** Automatically fills request parameters with valid dummy data (e.g., UUIDs for ID fields, ISO strings for Dates).
88+
* **Validation:** Verifies that your API responses align with the JSON Schema defined in your spec.
8889

8990
---
9091

91-
## 📦 Usage
92-
93-
### Installation
94-
95-
Clone the repository and build the CLI tool:
96-
97-
```bash
98-
cargo build -p cdd-cli --release
99-
```
100-
101-
### Command Reference
92+
## 📦 Command Usage
10293

103-
#### 1. Sync Pipeline (DB ➔ Rust ➔ OpenAPI)
104-
Transforms raw Diesel schema files into rich, OpenAPI-compatible Rust structs.
94+
### 1. The Sync Pipeline
95+
**DB ➔ Rust Models ➔ OpenAPI Attributes**
96+
Synchronizes your database schema to your Rust structs and ensures they are ready for OpenAPI generation.
10597

10698
```bash
107-
# Workflow:
108-
# 1. Runs `dsync` to update models from schema.rs
109-
# 2. Patches models with #[derive(ToSchema)]
11099
cargo run -p cdd-cli -- sync \
111100
--schema-path web/src/schema.rs \
112101
--model-dir web/src/models
113102
```
114103

115-
#### 2. Test Configuration (OpenAPI ➔ Tests)
116-
Reads your API definition and writes a Rust test suite.
104+
### 2. The Test Pipeline
105+
**OpenAPI ➔ Integration Tests**
106+
Generates a test suite that treats your app as a black box.
117107

118108
```bash
119109
cargo run -p cdd-cli -- test-gen \
@@ -124,30 +114,21 @@ cargo run -p cdd-cli -- test-gen \
124114

125115
---
126116

127-
## 🏗 Project Structure
117+
## 🛠 Project Structure
128118

129-
* **`core/`**: The brain of the operation. Contains AST parsers (`ra_ap_syntax`), OpenAPI parsers, and code patching logic. It performs surgical updates on Rust source files.
130-
* **`cli/`**: The automation runner. Orchestrates `diesel`, `dsync`, and the core library to execute pipelines.
131-
* **`web/`**: The reference implementation. A standard Actix Web project that demonstrates how generated models and tests live alongside hand-written logic.
119+
* **`core/`**: The engine. Contains AST parsers, OpenAPI parsers, and the diff/patch logic.
120+
* `patcher.rs`: Surgical code editing.
121+
* `handler_generator.rs`: Scaffolds Actix handlers.
122+
* `oas.rs`: Parses OpenAPI YAML.
123+
* **`cli/`**: The workflow runner. Use this to run `sync` or `test-gen`.
124+
* **`web/`**: Reference implementation. An Actix Web + Diesel project demonstrating the generated code in action.
132125

133126
## 🎨 Design Principles
134127

135-
* **Non-Destructive:** We respect your existing code style, comments, and spacing.
136-
* **Type Safety:** `Uuid`, `jso::serde::Value`, and `chrono::DateTime` are first-class citizens.
137-
* **Dependencies:**
138-
* **Actix Web:** For the HTTP layer.
139-
* **Diesel:** For ORM/DB layer.
140-
* **Utoipa:** For OpenAPI attribute generation.
141-
* **Ra_ap_syntax:** For 100% accurate code manipulation.
142-
143-
## 🔮 Roadmap
144-
145-
- [x] Structural Diffing & AST Patching
146-
- [x] Database Model Synchronization
147-
- [x] Contract Test Generation
148-
- [x] Handler Scaffolding (Core Library Support)
149-
- [ ] Visual Schema Designer Integration
150-
- [ ] Expose Handler Scaffolding via CLI
128+
* **No Magic Folders:** We generate code you can read, debug, and commit.
129+
* **Lossless Patching:** We edit your source files without breaking your style.
130+
* **Type Safety:** `Uuid`, `chrono`, and `rust_decimal` are first-class citizens.
131+
* **100% Coverage:** The toolchain itself enforces strict documentation and test coverage.
151132

152133
---
153134

0 commit comments

Comments
 (0)