Skip to main content

ferro_cli/templates/
ai_boost.rs

1// ============================================================================
2// AI Development Boost Templates
3// ============================================================================
4
5/// Ferro framework guidelines for AI assistants
6pub fn ferro_guidelines_template() -> &'static str {
7    r#"# Ferro Framework Guidelines
8
9Ferro is a Rust web framework inspired by Laravel, providing a familiar developer experience with Rust's performance and safety.
10
11## Project Structure
12
13```
14app/
15├── src/
16│   ├── main.rs           # Application entry point
17│   ├── routes.rs         # Route definitions
18│   ├── bootstrap.rs      # Application bootstrap
19│   ├── controllers/      # Request handlers
20│   ├── middleware/       # HTTP middleware
21│   ├── models/           # Database models (SeaORM entities)
22│   │   └── entities/     # Auto-generated entities (do not edit)
23│   ├── actions/          # Business logic actions
24│   ├── events/           # Domain events
25│   ├── listeners/        # Event listeners
26│   ├── jobs/             # Background jobs
27│   ├── notifications/    # Multi-channel notifications
28│   ├── tasks/            # Scheduled tasks
29│   ├── config/           # Configuration modules
30│   └── migrations/       # Database migrations
31└── Cargo.toml
32frontend/
33├── src/
34│   ├── main.tsx          # Frontend entry
35│   └── pages/            # Inertia.js pages (React/TypeScript)
36└── package.json
37```
38
39## Key Conventions
40
41### Controllers
42- Use the `#[handler]` macro for route handlers
43- Return `Response` type using helper macros
44
45```rust
46use ferro::{handler, json_response, Request, Response};
47
48#[handler]
49pub async fn index(_req: Request) -> Response {
50    json_response!({ "message": "Hello" })
51}
52```
53
54### Middleware
55- Implement the `Middleware` trait
56- Use `#[async_trait]` for async methods
57
58```rust
59use ferro::{async_trait, Middleware, Next, Request, Response};
60
61pub struct MyMiddleware;
62
63#[async_trait]
64impl Middleware for MyMiddleware {
65    async fn handle(&self, request: Request, next: Next) -> Response {
66        // Before request
67        let response = next(request).await;
68        // After request
69        response
70    }
71}
72```
73
74### Models (SeaORM)
75- Models use SeaORM with an Eloquent-like API
76- Entity files in `models/entities/` are auto-generated
77- Custom logic goes in `models/{table_name}.rs`
78
79```rust
80// Query builder pattern
81let users = User::query()
82    .filter(Column::Active.eq(true))
83    .all()
84    .await?;
85
86// Fluent create
87let user = User::create()
88    .set_email("user@example.com")
89    .set_name("John")
90    .insert()
91    .await?;
92
93// Fluent update
94let updated = user
95    .set_name("Jane")
96    .update()
97    .await?;
98```
99
100### Inertia.js Integration
101- Backend sends data via `inertia_response!` macro
102- Frontend receives as props in React components
103- TypeScript types auto-generated from Rust structs
104
105```rust
106// Backend
107#[handler]
108pub async fn show(req: Request) -> Response {
109    inertia_response!("Users/Show", {
110        "user": user,
111        "posts": posts
112    })
113}
114```
115
116### Database Migrations
117- Create with `ferro make:migration <name>`
118- Run with `ferro migrate`
119- Sync models with `ferro db:sync`
120
121### Error Handling
122- Use `#[domain_error]` macro for custom errors
123- Errors automatically convert to appropriate HTTP responses
124
125```rust
126use ferro::domain_error;
127
128#[domain_error(status = 404, message = "User not found")]
129pub struct UserNotFound;
130```
131
132## CLI Commands
133
134- `ferro new <name>` - Create new project
135- `ferro serve` - Start dev servers
136- `ferro make:controller <name>` - Generate controller
137- `ferro make:middleware <name>` - Generate middleware
138- `ferro make:migration <name>` - Generate migration
139- `ferro make:event <name>` - Generate event
140- `ferro make:job <name>` - Generate background job
141- `ferro migrate` - Run migrations
142- `ferro db:sync` - Sync DB schema to entities
143- `ferro mcp` - Start MCP server for AI assistance
144
145## Best Practices
146
1471. **Use Actions for Business Logic**: Keep controllers thin, move logic to action classes
1482. **Leverage the Type System**: Use Rust's types for validation and safety
1493. **Auto-generate Types**: Run `ferro generate-types` to sync Rust structs to TypeScript
1504. **Database Sync**: Use `ferro db:sync` after migrations to update entity files
1515. **Middleware Order**: Register middleware in the correct order in routes.rs
152"#
153}
154
155/// Cursor-specific rules file
156pub fn cursor_rules_template() -> &'static str {
157    r#"# Ferro Framework - Cursor Rules
158
159You are working on a Ferro framework project. Ferro is a Rust web framework inspired by Laravel.
160
161## Framework Knowledge
162
163- Ferro uses Rust with async/await for the backend
164- Frontend uses React + TypeScript with Inertia.js
165- Database layer uses SeaORM with an Eloquent-like API
166- The project follows Laravel conventions adapted for Rust
167
168## Code Style
169
170- Use `#[handler]` macro for route handlers
171- Use `#[async_trait]` for middleware
172- Use `#[domain_error]` for custom errors
173- Follow Rust naming conventions (snake_case for functions, PascalCase for types)
174
175## When Generating Code
176
1771. Controllers go in `app/src/controllers/`
1782. Middleware goes in `app/src/middleware/`
1793. Models go in `app/src/models/`
1804. React pages go in `frontend/src/pages/`
181
182## Available MCP Tools
183
184Use the Ferro MCP tools for introspection:
185- `application_info` - Get app info, versions, crates
186- `list_routes` - See all defined routes
187- `db_schema` - Get database schema
188- `db_query` - Run read-only SQL queries
189- `list_migrations` - Check migration status
190- `list_middleware` - See registered middleware
191- `read_logs` - Read application logs
192- `last_error` - Get recent errors
193- `tinker` - Execute Rust code in app context
194- `browser_logs` - Read frontend error logs
195
196## Common Patterns
197
198### Adding a new page
1991. Create controller handler in `app/src/controllers/`
2002. Add route in `app/src/routes.rs`
2013. Create React page in `frontend/src/pages/`
2024. Run `ferro generate-types` to sync types
203
204### Adding a database table
2051. `ferro make:migration create_table_name`
2062. Edit migration file
2073. `ferro migrate`
2084. `ferro db:sync`
209"#
210}
211
212/// CLAUDE.md template for Claude Code
213pub fn claude_md_template() -> &'static str {
214    r#"# Project Instructions
215
216This is a Ferro framework project - a Rust web framework inspired by Laravel.
217
218## Quick Reference
219
220- **Backend**: Rust with async/await, SeaORM for database
221- **Frontend**: React + TypeScript with Inertia.js
222- **CLI**: Use `ferro` command for scaffolding
223
224## MCP Tools Available
225
226The Ferro MCP server provides these introspection tools:
227- `application_info`, `list_routes`, `db_schema`, `db_query`
228- `list_migrations`, `list_middleware`, `list_events`, `list_jobs`
229- `read_logs`, `last_error`, `browser_logs`, `tinker`
230
231## Development Workflow
232
2331. Use `ferro serve` to start dev servers
2342. Use `ferro make:*` commands for scaffolding
2353. Use `ferro db:sync` after migrations to update models
2364. Use `ferro generate-types` to sync TypeScript types
237
238## Ferro Framework Guidelines
239
240See `.ai/guidelines/ferro.md` for detailed framework conventions.
241"#
242}
243
244/// Section to append to existing CLAUDE.md
245pub fn claude_md_ferro_section() -> &'static str {
246    r#"
247---
248
249# Ferro Framework
250
251This is a Ferro framework project - a Rust web framework inspired by Laravel.
252
253## MCP Tools Available
254
255The Ferro MCP server provides introspection tools:
256- `application_info`, `list_routes`, `db_schema`, `db_query`
257- `list_migrations`, `list_middleware`, `list_events`, `list_jobs`
258- `read_logs`, `last_error`, `browser_logs`, `tinker`
259
260## Framework Conventions
261
262See `.ai/guidelines/ferro.md` for detailed framework conventions.
263"#
264}
265
266/// GitHub Copilot instructions
267pub fn copilot_instructions_template() -> &'static str {
268    r#"# GitHub Copilot Instructions
269
270## Project Type
271This is a Ferro framework project (Rust web framework inspired by Laravel).
272
273## Key Files
274- `app/src/routes.rs` - Route definitions
275- `app/src/controllers/` - Request handlers
276- `app/src/models/` - Database models (SeaORM)
277- `frontend/src/pages/` - React/TypeScript pages
278
279## Code Patterns
280
281### Controller Handler
282```rust
283use ferro::{handler, json_response, Request, Response};
284
285#[handler]
286pub async fn index(_req: Request) -> Response {
287    json_response!({ "data": value })
288}
289```
290
291### SeaORM Query
292```rust
293let items = Model::query()
294    .filter(Column::Field.eq(value))
295    .all()
296    .await?;
297```
298
299### Inertia Response
300```rust
301inertia_response!("PageName", { "prop": value })
302```
303
304## Conventions
305- Controllers are async handlers with `#[handler]` macro
306- Models use SeaORM with Eloquent-like query builder
307- Frontend pages receive data as Inertia props
308- TypeScript types are auto-generated from Rust structs
309"#
310}