mirror of
https://github.com/tyrchen/cursor-rust-rules.git
synced 2025-12-23 01:30:00 +00:00
feature: add cli rules
This commit is contained in:
1083
.cursor/rules/rust/features/cli.mdc
Normal file
1083
.cursor/rules/rust/features/cli.mdc
Normal file
File diff suppressed because it is too large
Load Diff
@@ -34,11 +34,13 @@ graph TD
|
||||
|
||||
Features --> Web{"Web Framework<br>needed?"}
|
||||
Features --> DB{"Database<br>access needed?"}
|
||||
Features --> CLI{"CLI interface<br>needed?"}
|
||||
Features --> Concurrent{"Heavy<br>concurrency?"}
|
||||
Features --> Config{"Complex config<br>or templating?"}
|
||||
|
||||
Web -->|Yes| WebRules["Load Axum Rules"]
|
||||
DB -->|Yes| DBRules["Load Database Rules"]
|
||||
CLI -->|Yes| CLIRules["Load CLI Rules"]
|
||||
Concurrent -->|Yes| ConcurrencyRules["Load Concurrency Rules"]
|
||||
Config -->|Yes| ToolsRules["Load Tools & Config Rules"]
|
||||
|
||||
@@ -87,11 +89,13 @@ graph TD
|
||||
|
||||
FeatureDetection --> WebFeature{"Web Framework?"}
|
||||
FeatureDetection --> DBFeature{"Database?"}
|
||||
FeatureDetection --> CLIFeature{"CLI Interface?"}
|
||||
FeatureDetection --> SerdeFeature{"Serialization?"}
|
||||
FeatureDetection --> BuilderFeature{"Complex Types?"}
|
||||
|
||||
WebFeature -->|Yes| AxumRules["Axum Framework Rules"]
|
||||
DBFeature -->|Yes| SQLxRules["SQLx Database Rules"]
|
||||
CLIFeature -->|Yes| CLIRules["CLI Application Rules"]
|
||||
SerdeFeature -->|Yes| SerdeRules["Serde Best Practices"]
|
||||
BuilderFeature -->|Yes| TypedBuilderRules["TypedBuilder Rules"]
|
||||
|
||||
@@ -194,6 +198,14 @@ sequenceDiagram
|
||||
- [ ] Custom error types
|
||||
- [ ] → Load Utilities rules if YES to any
|
||||
|
||||
### CLI Application Requirements
|
||||
- [ ] Command-line interface needed
|
||||
- [ ] Multiple subcommands required
|
||||
- [ ] Interactive prompts needed
|
||||
- [ ] Progress bars or status indicators
|
||||
- [ ] Configuration file support
|
||||
- [ ] → Load CLI rules if YES to any
|
||||
|
||||
### HTTP Client Requirements
|
||||
- [ ] External API integration
|
||||
- [ ] HTTP requests needed
|
||||
@@ -217,7 +229,7 @@ Based on project analysis, load specific rule sets:
|
||||
# Feature-specific loading examples:
|
||||
# Web: core + axum + serde + utilities (JWT)
|
||||
# Database: core + sqlx + utilities (error handling)
|
||||
# CLI: core + simple + utilities (clap + builders)
|
||||
# CLI: core + cli + utilities (enum_dispatch + error handling)
|
||||
# Auth: core + utilities (JWT + validation)
|
||||
```
|
||||
|
||||
@@ -230,6 +242,7 @@ Based on project analysis, load specific rule sets:
|
||||
| **Complex** | `complex/workspace.mdc` | Multi-crate workspace management |
|
||||
| **Web** | `features/axum.mdc` | Axum 0.8 patterns, OpenAPI with utoipa |
|
||||
| **Database** | `features/database.mdc` | SQLx patterns, repository design, testing |
|
||||
| **CLI** | `features/cli.mdc` | Clap 4.0+ patterns, subcommands, enum_dispatch |
|
||||
| **Concurrency** | `features/concurrency.mdc` | Tokio, DashMap, async patterns |
|
||||
| **Tools & Config** | `features/tools-and-config.mdc` | Tracing, YAML config, MiniJinja templates |
|
||||
| **Utilities** | `features/utilities.mdc` | JWT auth, CLI tools, builders, enhanced derives |
|
||||
@@ -240,10 +253,10 @@ Based on project analysis, load specific rule sets:
|
||||
## 🎯 PROJECT TYPE EXAMPLES
|
||||
|
||||
### Simple Project Examples
|
||||
- CLI utilities (grep clone, file converter)
|
||||
- Single-purpose libraries (parsing, algorithms)
|
||||
- Simple HTTP servers (< 10 endpoints)
|
||||
- Desktop applications (single-window apps)
|
||||
- **CLI utilities** (grep clone, file converter, system tools)
|
||||
- **Single-purpose libraries** (parsing, algorithms)
|
||||
- **Simple HTTP servers** (< 10 endpoints)
|
||||
- **Desktop applications** (single-window apps)
|
||||
|
||||
### Complex Project Examples
|
||||
- **Workflow engines** (multi-node processing systems)
|
||||
|
||||
49
README.md
49
README.md
@@ -45,6 +45,7 @@ rules:
|
||||
- "@rust/main" # Core Rust standards
|
||||
- "@rust/axum" # Web development with Axum
|
||||
- "@rust/database" # Database patterns with SQLx
|
||||
- "@rust/cli" # CLI applications with clap
|
||||
- "@rust/concurrency" # Async and concurrency patterns
|
||||
- "@rust/utilities" # Utility libraries and CLI tools
|
||||
- "@rust/workspace" # Multi-crate workspace organization
|
||||
@@ -71,6 +72,7 @@ cursor-rust-rules/
|
||||
│ ├── features/ # Feature-specific rules
|
||||
│ │ ├── axum.mdc # Web framework standards
|
||||
│ │ ├── database.mdc # Database patterns
|
||||
│ │ ├── cli.mdc # CLI application patterns
|
||||
│ │ ├── concurrency.mdc # Async/concurrency patterns
|
||||
│ │ ├── utilities.mdc # Utility libraries
|
||||
│ │ ├── http-client.mdc # HTTP client patterns
|
||||
@@ -90,6 +92,7 @@ cursor-rust-rules/
|
||||
|
||||
- **Web Framework** (`@rust/features/axum`) - Axum 0.8+, OpenAPI, middleware, authentication
|
||||
- **Database** (`@rust/features/database`) - SQLx patterns, migrations, connection pooling
|
||||
- **CLI Applications** (`@rust/features/cli`) - Clap 4.0+, subcommands, enum_dispatch, modern CLI patterns
|
||||
- **Concurrency** (`@rust/features/concurrency`) - Tokio, async patterns, concurrent data structures
|
||||
- **Utilities** (`@rust/features/utilities`) - JWT, CLI tools, builder patterns, validation
|
||||
- **HTTP Client** (`@rust/features/http-client`) - Reqwest patterns, retry logic, authentication
|
||||
@@ -169,6 +172,52 @@ pub async fn setup_database(config: &DatabaseConfig) -> Result<PgPool, DatabaseE
|
||||
}
|
||||
```
|
||||
|
||||
### CLI Application Development
|
||||
|
||||
For building powerful command-line tools:
|
||||
|
||||
```rust
|
||||
// Cargo.toml gets configured with these dependencies
|
||||
[dependencies]
|
||||
clap = { version = "4.0", features = ["derive", "env", "unicode"] }
|
||||
enum_dispatch = "0.3"
|
||||
tokio = { version = "1.45", features = ["macros", "rt-multi-thread"] }
|
||||
anyhow = "1.0"
|
||||
colored = "2.0"
|
||||
```
|
||||
|
||||
Your CLI follows the enum_dispatch pattern:
|
||||
|
||||
```rust
|
||||
/// Command execution trait with enum_dispatch
|
||||
#[async_trait]
|
||||
#[enum_dispatch(Commands)]
|
||||
pub trait CommandExecutor {
|
||||
async fn execute(&self, args: &Args, config: &Config) -> Result<()>;
|
||||
}
|
||||
|
||||
/// All available commands
|
||||
#[derive(Debug, Subcommand)]
|
||||
#[enum_dispatch(CommandExecutor)]
|
||||
pub enum Commands {
|
||||
/// Database management commands
|
||||
#[command(name = "db", alias = "database")]
|
||||
Database(DatabaseCommand),
|
||||
|
||||
/// Server management commands
|
||||
#[command(name = "server", alias = "srv")]
|
||||
Server(ServerCommand),
|
||||
}
|
||||
|
||||
// Each command implements CommandExecutor
|
||||
#[async_trait]
|
||||
impl CommandExecutor for DatabaseCommand {
|
||||
async fn execute(&self, args: &Args, config: &Config) -> Result<()> {
|
||||
// Command implementation with progress bars, colored output, etc.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Multi-Crate Workspace
|
||||
|
||||
For complex applications, organize as subsystems:
|
||||
|
||||
16
specs/instructions.md
Normal file
16
specs/instructions.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# Instructions
|
||||
|
||||
请构建 Rust CLI 项目的 rules:
|
||||
|
||||
1. 如果项目需要使用到 CLI,则引入 clap,使用 derive feature。
|
||||
2. 如果有多个 CLI,请使用 subcommand。
|
||||
3. 构建应用于 command 的 execute trait,每个 subcommand 实现该 trait,并使用 enum_dispatch 进行 dispatch
|
||||
|
||||
```rust
|
||||
#[enum_dispatch(...)]
|
||||
pub trait CommandExecutor {
|
||||
async fn execute(&self, args: &Args) -> Result<(), Error>;
|
||||
}
|
||||
```
|
||||
|
||||
4. 其他请遵循 clap 最佳实践
|
||||
Reference in New Issue
Block a user