Cline rules
.clinerules/backend-rules.mdUse when: writing or modifying Rust code in src-tauri/. Covers Tauri command structure, state management, external process handling, logging, and compile-time config.
Cline rules
Quality
81/100
Scores the file, not the repository.Length
535 words
9 headings · 7 code blocksRepository
1
— · pushed 1 days agoLast changed
yesterday
First indexed yesterday.1234567# Backend Rules89<!-- Tauri v2 / Rust 后端规范,操作 src-tauri/ 目录时自动加载 -->1011## Tauri Commands1213Always declare Tauri commands in `src-tauri/src/lib.rs` and register them in `run()` via `.invoke_handler(tauri::generate_handler![...])`.1415Always use `#[tauri::command]` on async functions. NEVER use sync commands for I/O-bound work.1617```rust18#[tauri::command]19async fn my_command(app: tauri::AppHandle, ...) -> Result<T, String> { ... }20```2122Always receive `app: tauri::AppHandle` as the first parameter. Use `app.state::<MyState>()` to access managed state.2324Prefer `Result<T, String>` as the return type for all commands. Use the `?` operator with `.map_err(|e| e.to_string())` or `.map_err(|e| format!("context: {e}"))` for error propagation.2526Use `tauri::async_runtime::spawn()` for long-running background tasks spawned from commands (e.g., polling loops, sidecar process monitors). NEVER use `std::thread::spawn` or `tokio::spawn` directly.2728## State Management2930Always use Tauri managed state (`app.manage(...)` in `run()`) for shared data. Declare state structs with thread-safe wrappers:3132```rust33/// task_id → aria2 gid34struct GidMap(Mutex<HashMap<String, String>>);3536/// task_id → oneshot sender to stop the polling loop37struct PollStops(Mutex<HashMap<String, oneshot::Sender<()>>>);3839/// Flag to break re-entry loops40struct Closing(AtomicBool);41```4243Never use global `static mut` or `lazy_static!` for mutable state — always go through `app.state::<T>()`.4445## Frontend Communication4647Always emit events to the frontend via `app.emit("event-name", payload)`. Define payload types with `#[derive(serde::Serialize, Clone)]`.4849```rust50#[derive(serde::Serialize, Clone)]51struct MyEventPayload {52 id: String,53 value: u8,54}5556let _ = app.emit("my-event", MyEventPayload { id, value });57```5859Never call `app.emit()` from a synchronous context that holds a lock — always clone the `AppHandle` and emit from an async context or after releasing the lock.6061## Logging6263Always use the `log` crate macros: `debug!`, `info!`, `warn!`, `error!`.6465Prefix log messages with a tag for filtering:6667```rust68info!("[add_magnet] task={task_id}");69debug!("[{task_id}] gid handoff: {current_gid} → {new_gid}");70error!("[merge][{}] 合并失败: {}", job.id, msg.trim());71```7273Use `debug!` for detailed diagnostics, `info!` for lifecycle events, `warn!` for recoverable issues, `error!` for failures.7475Register `env_logger` in `run()` before the Tauri builder. Do NOT add additional logging frameworks.7677## External Processes7879Always use `tauri-plugin-shell` for spawning external binaries (aria2c, mkvmerge, etc.). NEVER use `std::process::Command` directly.8081```rust82use tauri_plugin_shell::{process::CommandEvent, ShellExt};8384let sidecar_cmd = app.shell().sidecar("mkvmerge").map_err(|e| format!("sidecar error: {e}"))?;85let (mut rx, _child) = sidecar_cmd.args(&args).spawn().map_err(|e| format!("spawn error: {e}"))?;86```8788External binaries (`aria2c`, `mkvmerge`) are bundled via `externalBin` in `tauri.conf.json`. Reference them by name via `app.shell().sidecar()`.8990Always handle `CommandEvent::Stdout`, `Stderr`, `Terminated`, and the `None` case in the event loop.9192## Compile-time Config9394Compile-time configuration lives in `src-tauri/app-config.json`. Embed it at compile time with `include_str!`:9596```rust97fn load_app_config() -> Result<AppConfig, String> {98 const RAW: &str = include_str!("../app-config.json");99 serde_json::from_str(RAW).map_err(|e| format!("app-config.json 格式错误: {e}"))100}101```102103Define a `#[derive(serde::Deserialize)]` struct matching the JSON shape. Changes to `app-config.json` take effect on the next `cargo build`.104105## HTTP Client106107Always use `reqwest` for outbound HTTP. Include `use reqwest::Client;` and build clients with `reqwest::Client::builder()`.108109Include reasonable `User-Agent` headers for external scraping. Use `.timeout(Duration::from_secs(N))` on requests to aria2c and external services.110111```rust112let client = reqwest::Client::builder()113 .user_agent("Mozilla/5.0 ...")114 .build()115 .map_err(|e| e.to_string())?;116```117118## Clippy & Code Quality119120Always respect Clippy's `disallowed_macros` and `too-many-arguments` rules. If a lint is explicitly suppressed, add `#[allow(...)]` with a comment explaining why.121122Keep argument counts low. Use helper structs or `app.state()` to pull dependencies into the function body rather than adding more parameters.123124Avoid `unwrap()` on locks in async contexts — use `unwrap_or_else(|e| e.into_inner())` to handle poisoned mutexes gracefully.125
Also in VaillerTeeter/HoshimiNest
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| VaillerTeeter/HoshimiNest.clinerules/frontend-rules.md · 1 | Cline rules | styletypesdependenciesui+1 | 81/100 | yesterday | |
| VaillerTeeter/HoshimiNest.clinerules/git-workflow.md · 1 | Cline rules | gitsecuritydo-notagent-behaviour | 81/100 | yesterday | |
| VaillerTeeter/HoshimiNest.clinerules/project-identity.md · 1 | Cline rules | setuparchtypesdo-not | 93/100 | yesterday |
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| bashdeban/fastmind.clinerules/.project-consistency-keeper2.md · 5 | Cline rules | setupbuildtestlint-format+11 | 100/100 | 3 days ago | |
| JCodesMore/ai-website-cloner-template.clinerules · 31k | Cline rules | buildlint-formatstylearch+3 | 97/100 | 2 days ago | |
| BryaanF/LiantPortfolio.clinerules/project-guidelines.md · 0 | Cline rules | buildstylearchgit+2 | 96/100 | 3 days ago | |
| u9401066/zotero-keepervscode-extension/resources/repo-assets/pubmed-search-mcp/.clinerules/50-pubmed-project.md · 6 | Cline rules | testlint-formatstylearch+1 | 94/100 | 3 days ago | |
| u9401066/zotero-keeper.clinerules/50-pubmed-project.md · 6 | Cline rules | testlint-formatstylearch+1 | 94/100 | 3 days ago | |
| VaillerTeeter/HoshimiNest.clinerules/project-identity.md · 1 | Cline rules | setuparchtypesdo-not | 93/100 | yesterday | |
| blendsdk/codeops-mcp.clinerules/project.md · 0 | Cline rules | buildteststylearch+7 | 91/100 | 3 days ago | |
| u9401066/zotero-keeper.clinerules/60-pubmed-python.md · 6 | Cline rules | setuptestlint-formatstyle+2 | 86/100 | 3 days ago |
