Vibe Coding
AI Vibe Coding進階79% · 需輔助

Rust CLI Vibe Coding:Clap v4 與整合測試閉環

面向中級開發者的 Rust 命令列工具設定,涵蓋 Clap 子命令、借用檢查、Tokio IO 與 assert_cmd 測試,整體完成度 79%。

預計耗時: 90 min更新: 2026-11-27
Cursor IDERustClap v4Tokiocargo test
Rust CLI Vibe Coding:Clap v4 與整合測試閉環

量化可完成度

整體完成度 79%:AI 在 Clap 子命令骨架、說明文件與整合測試模板生成上表現突出,能快速產出可編譯程式碼。借用檢查與生命週期收斂仍需人工把關,非同步 IO 與錯誤邊界也需要驗證。

79%需輔助

四維達成明細

Clap v4 子命令與說明文件93%

AI 能穩定生成 derive 子命令與 about 文本,說明輸出準確度高。

借用檢查與生命週期收斂74%

AI 能給出所有權建議但常需人工修正生命週期標註與借用衝突。

Tokio 非同步檔案與網路 IO78%

AI 能生成 Tokio 骨架,但阻塞呼叫與錯誤傳播需人工核對。

assert_cmd CLI 整合測試82%

AI 能生成 assert_cmd 測試模板,但失敗路徑覆蓋與斷言嚴謹性需人工補強。

人工把關檢查點

合併前人工審查借用檢查與生命週期錯誤、anyhow 錯誤邊界是否清晰、整合測試是否覆蓋失敗路徑,並確認 --help 輸出與實際行為一致。

升級 / 降級路徑

預算充足時可升級到 Tokio 全特性 + tokio-test + 更嚴格的 clippy 設定;預算緊張時可降級為同步 IO + 基礎 assert_cmd 測試,保留 Clap 與 anyhow 結構。

適用場景與前提

目標使用者是熟悉 Rust 基礎、希望用 AI 輔助搭建 CLI 工具的中級開發者。環境需求:Cursor IDE、Rust 工具鏈(cargo)、Clap v4、Tokio、assert_cmd。典型場景是從零搭建帶子命令的 CLI,或為現有工具補齊整合測試與錯誤處理。

可直接複製的工程設定

.cursor/rules/rust-cli.mdcmarkdown

约束 Cursor 在 Rust CLI 开发中使用 Clap v4 子命令、anyhow 错误处理与 cargo test 集成测试的规则。

---
description: Rust CLI vibe coding rules for Clap v4, anyhow, Tokio, cargo test
globs: ["**/*.rs"]
alwaysApply: true
---

# Rust CLI Rules
- Use Clap v4 derive API for all CLI parsing; define subcommands explicitly (e.g., `Command::new("app").subcommand(...)`).
- Every subcommand must have `about`/`long_about` help text and `#[command(name = "...")]`.
- Prefer `anyhow::Result<T>` for fallible functions; avoid `unwrap()`/`expect()` in library code paths.
- Async entry points use `#[tokio::main]`; keep blocking IO out of async tasks.
- Integration tests live in `tests/` and use `assert_cmd` + `predicates`; do not rely on stdout snapshots without explicit assertions.
- Run `cargo fmt`, `cargo clippy -- -D warnings`, and `cargo test` before considering a change complete.
- Keep `Cargo.toml` dependencies minimal; pin major versions (clap = "4", tokio = { version = "1", features = ["full"] }).
AGENTS.mdmarkdown

为 AI 编码助手提供仓库级上下文、命令与验收标准。

# AGENTS.md

## Project
Rust CLI tool built with Clap v4, Tokio, anyhow, and assert_cmd integration tests.

## Commands
- Build: `cargo build`
- Test: `cargo test`
- Lint: `cargo clippy -- -D warnings`
- Format: `cargo fmt`

## Conventions
- CLI structure: Clap v4 derive, one subcommand per user action.
- Errors: anyhow in app code; map to user-friendly messages at the boundary.
- Async: Tokio runtime; avoid blocking calls inside async blocks.
- Tests: `tests/cli.rs` with assert_cmd; cover success, invalid args, and IO failure paths.

## Acceptance
- `cargo test` passes.
- `cargo clippy -- -D warnings` is clean.
- `--help` output is accurate and complete.
- No `unwrap()` in non-test library code.

AI Vibe Coding 四步實戰

完成進度: 0 / 4
  1. 1

    搭建 Clap v4 子命令骨架

    目標: 用 derive API 定義子命令與說明文件。

    Prompt
    請為 Rust CLI 生成 Clap v4 derive 結構:定義一個 `app` 命令與 `run`/`status` 兩個子命令,每個子命令帶 about 與 long_about,使用 anyhow::Result 返回。保持程式碼可編譯,不要使用 unwrap。

    驗收標準: 執行 `cargo run -- --help` 與 `cargo run -- run --help`,確認輸出包含子命令與說明文本。

  2. 2

    收斂借用檢查與錯誤邊界

    目標: 消除 unwrap 並讓 anyhow 錯誤在邊界轉換。

    Prompt
    請審查目前程式碼,將 library 路徑中的 unwrap/expect 替換為 anyhow::Result,在 main 邊界將錯誤轉換為使用者友善訊息,並修正借用檢查與生命週期錯誤。

    驗收標準: 執行 `cargo clippy -- -D warnings` 與 `cargo test`,確認無警告且測試通過。

  3. 3

    接入 Tokio 非同步 IO

    目標: 用 Tokio 處理檔案與網路 IO,避免阻塞。

    Prompt
    請將 CLI 的 IO 部分改為 Tokio 非同步實作:使用 #[tokio::main],用 tokio::fs 讀取檔案,用 reqwest 或 tokio::net 處理網路請求,確保錯誤透過 anyhow 傳播。

    驗收標準: 執行 `cargo test` 並確認非同步路徑有測試覆蓋,且 `cargo clippy` 無阻塞呼叫警告。

  4. 4

    補齊 assert_cmd 整合測試

    目標: 用整合測試覆蓋成功、無效參數與 IO 失敗路徑。

    Prompt
    請在 tests/cli.rs 中使用 assert_cmd 與 predicates 編寫整合測試:覆蓋成功路徑、無效參數、IO 失敗三類場景,斷言 stdout/stderr 與退出碼。

    驗收標準: 執行 `cargo test --test cli`,確認所有整合測試通過且失敗路徑有斷言。

常見踩坑與解法

  • Pitfall 1: 在 async 區塊中呼叫阻塞 IO,導致執行時卡頓。Fix: 使用 tokio::fs 或 spawn_blocking。
  • Pitfall 2: 在 library 程式碼中使用 unwrap,導致 panic。Fix: 改用 anyhow::Result 並在邊界轉換。
  • Pitfall 3: 整合測試只斷言 stdout,忽略退出碼與 stderr。Fix: 用 assert_cmd 斷言 exit code、stdout 與 stderr。

常見問題

Clap v4 與 v3 有什麼主要差異?

Clap v4 改進了 derive API 與子命令結構,說明文件生成更一致。遷移時需檢查 #[command] 與 #[arg] 屬性的變化。

anyhow 與 thiserror 如何選擇?

anyhow 適合應用層錯誤傳播,thiserror 適合函式庫層定義結構化錯誤。CLI 工具通常用 anyhow,函式庫程式碼用 thiserror。

assert_cmd 測試如何覆蓋失敗路徑?

用 assert_cmd 的 .failure() 與 .stderr() 斷言失敗場景,並檢查退出碼與錯誤訊息。

相關方案

Rust CLI Vibe Coding:Clap v4 與整合測試閉環 · CursorHome