rust-command
Recipe to add a Rust command to EasyTask (src-tauri) - the command and its registration, heavy work off the UI thread, path checks, a new crate with cargo deny / cargo audit, caches in the data folder
SKILL.md.claude/skills/rust-command/SKILL.md
GitHubAdd a Rust command (EasyTask)
Rust lives in src-tauri/src/, one module per area: audio.rs (metadata with lofty), files.rs (path checks, asset scope), paths.rs (data folder), transfer_zip.rs, db.rs, media_session.rs, settings.rs, window.rs. The database is NOT here: tables and queries are TypeScript (src/db/). Model a new command on audio::audio_metadata.
1. The command
#[tauri::command]
pub(crate) async fn audio_metadata(path: String) -> Result<AudioMetadata, String> {
if !is_audio_file(&path) {
return Err("audio file not found".to_string());
}
tauri::async_runtime::spawn_blocking(move || read_audio_metadata(Path::new(&path)))
.await
.map_err(|error| error.to_string())?
}
pub(crate) async fn, errors asResult<_, String>; the result type derivesserde::Serializewith#[serde(rename_all = "camelCase")]if the TS side expects camelCase (check the existing struct).- Check every path from the frontend before touching it:
files::is_audio_file/ the media check (allowed extension + regular file). Never read or write a path the user did not pick. - Heavy work (decoding, hashing, zip) goes in
spawn_blocking.Cargo.tomlhas nopanic = "abort"on purpose: a panic while parsing a user file must come back as aJoinError, not kill the app. - Long work that the UI can abandon (another file chosen): make it cancellable or make the frontend ignore stale answers (a sequence number, as
playSeqinaudio-context.tsx). - The data folder is
paths::DataDirInfo(managed state:info: tauri::State<'_, DataDirInfo>). Caches go in a subfolder of it, keyed by something that changes with the file (path + size + mtime), written atomically (temp file + rename) and safe to delete.
2. Registration and permissions
- Add it to
tauri::generate_handler![...]inlib.rs. - App commands need no capability entry; plugin commands and window APIs do (skill tauri-permission). If the frontend then loads the file with
convertFileSrc, the path must be in the asset scope (files::allow_audio_file).
3. A new crate
src-tauri/Cargo.toml[dependencies], with only the features needed (default-features = falsewhen the defaults pull a lot). KeepCargo.lockin the commit.- CI runs
cargo deny checkandcargo auditinsrc-tauri(job "audit").deny.tomlallows only permissive licenses (MIT, Apache-2.0, BSD, ISC, MPL-2.0, Zlib...): a crate outside the list fails CI and needs the user's OK, never a silentallow. Do not add anignoreto advisories without a written reason and a date, like the existing ones. - The release profile is
lto = true,opt-level = "s": check the effect on the size of the installer for big crates and say it in the PR. - The app ships on Windows and Linux (
[graph] targetsindeny.toml): no crate that needs a system library not installed by.github/actions/linux-tauri-deps.
4. Tests
#[cfg(test)] mod testsat the bottom of the module;crate::test_support::temp_dir(name)for a fresh folder, removed at the end.- Generate the files in the test instead of committing binaries:
audio.rshaswrite_test_wav(a WAV header + samples), extend it with a known signal (for example a 1 kHz sine at a known level) to assert values with a tolerance. - Cover: a valid file, a wrong extension, a missing file, a corrupt file (must be an
Err, not a panic). - Run in
src-tauri:cargo fmt --check,cargo clippy -- -D warnings,cargo test(skill pre-push). CI runs them on Windows and Ubuntu.
5. The TypeScript side
invoke<Type>("command_name", { path })from@tauri-apps/api/core, wrapped in a function of asrc/lib/orsrc/contexts/module (not called from components).- Model:
src/lib/audio-metadata.tswith its testsrc/lib/audio-metadata.test.ts(vitest mocksinvoke, error case included) andrightbar/DetailsPanel.audio.test.tsxfor the panel. - Show progress with
LoadingDotsand errors with the existing toasts; texts in en + it (skill i18n-keys).