SkilVaultSign inStart free

Catalogue / rust-cli-tool-engineering

rust-cli-tool-engineering

Apache-2.0SignedEngineering skill: Rust CLI tool — how to build and ship a library with clap, Rust, cargo test. Use when writing, reviewing, testing or releasing code in this stack, and when turning a Celestial design or experience skill into code.
---
name: rust-cli-tool-engineering
description: "Engineering skill: Rust CLI tool — how to build and ship a library with clap, Rust, cargo test. Use when writing, reviewing, testing or releasing code in this stack, and when turning a Celestial design or experience skill into code."
type: engineering
stack: library
core: "1.1"
checked: 2026-10-26
---

Rust CLI tool

A single-binary command-line tool in Rust with typed argument parsing, clear exit codes and tests that run the real binary.

For developer tools, file processors, migration helpers and local automation that should start instantly and ship as one static binary. Released as prebuilt binaries per platform from tagged commits, and optionally published to the crate registry.

Stack

  • Language: Rust 1.82+ (edition 2021) — Memory safety without a runtime, so the binary starts fast and has no install dependencies.
  • Framework: clap (derive API) ^4 — Arguments, subcommands and help text come from one typed struct.
  • State: Config struct loaded once from flags, env and an optional config file — One resolved value is passed down; nothing reads globals.
  • Data: serde + serde_json / toml ^1 — Typed parsing of config files and machine-readable --json output.
  • Build: Cargo built in — Builds, tests, lints and packages with one tool.
  • Hosting: Release assets per target triple — Users download one file for their platform; no toolchain needed.
  • Package manager: Cargo + Cargo.lock committed built in — Reproducible builds for a binary crate.
  • Testing: cargo test built in — Unit tests beside the code and integration tests in tests/.
  • Testing: assert_cmd + predicates ^2 — Runs the compiled binary and checks stdout, stderr and exit code.
  • Testing: insta ^1 — Snapshot tests for help text and formatted output, reviewed as diffs.

Commands

  • dev: cargo run -- --help
  • lint: cargo fmt --check && cargo clippy --all-targets --all-features -- -D warnings
  • test: cargo test --all-features
  • build: cargo build --release
  • deploy: git tag vX.Y.Z && git push --tags
  • install: cargo fetch

Structure

  • src/main.rs — Parses arguments, calls run(), maps errors to exit codes; nothing else
  • src/cli.rs — clap derive structs: the full command surface in one file
  • src/commands/ — One module per subcommand, each exposing run(args, ctx) -> Result
  • src/lib.rs — Core logic with no terminal I/O, so it can be unit-tested and reused
  • src/output.rs — Human and --json renderers; the only place that writes to stdout
  • tests/ — Integration tests that run the built binary against fixture files
  • tests/snapshots/ — Reviewed insta snapshots of help text and output

Naming

  • Files: snake_case.rs; one module per subcommand named after it
  • Tests: #[cfg(test)] mod tests beside the code; tests/<subcommand>.rs for binary tests
  • Branches: type/short-description, e.g. fix/glob-on-windows
  • Components: Types PascalCase, functions and modules snake_case, flags kebab-case (--dry-run)

From design and experience skills

This stack has no UI; design skills do not apply.

When a Celestial experience skill is in use, give each journey one end-to-end test (happy path plus its error and empty states) and name analytics events object_action from its measures.

Conventions

Code

  • Prefer small, pure functions and explicit types at module boundaries. _(Easier to test, review and change.)_
  • Data goes to stdout, diagnostics and progress go to stderr. _(Piping the output into another tool must not capture log lines.)_

Components

  • Components read design tokens; no raw hex values, pixel sizes or font names in component code. _(One token change restyles the product.)_

Data

  • Analytics events are object_action in snake_case and come from the experience skill’s measures. _(Names stay consistent across teams and tools.)_
  • Every command that prints results supports --json with a stable, versioned shape. _(Other programs can consume the tool without parsing prose.)_

Errors

  • Every async action has loading, empty, error and success states, matching the experience skill. _(Unhandled states are where users get stuck.)_
  • No unwrap() or expect() outside tests; return Result and add context at each layer. _(A panic prints a stack trace instead of a message the user can act on.)_
  • Exit 0 on success, 1 on runtime failure, 2 on bad usage; document any other codes. _(Scripts and CI branch on exit codes, not on text.)_

Security

  • Secrets never live in the repository; read them from the environment or a secret store. _(A leaked key outlives the commit that removed it.)_
  • Validate every input on the server, even when the client already did. _(Clients can be bypassed.)_
  • Web pages ship a Content-Security-Policy, HSTS, X-Frame-Options and nosniff. _(Cheap protection against the most common attacks.)_
  • Never pass user input through a shell; build std::process::Command with explicit args. _(Prevents command injection from file names and arguments.)_

Performance

  • Stream large inputs with buffered readers instead of reading whole files into memory. _(The tool keeps working on files larger than RAM.)_

Accessibility

  • Use native elements first; add ARIA only when no native element fits. _(Native controls bring keyboard and screen-reader support for free.)_
  • Disable colour and spinners when NO_COLOR is set or stdout is not a terminal. _(Logs, screen readers and CI output stay readable.)_

Do

  • Offer --dry-run on every command that writes, moves or deletes files
  • Snapshot-test --help so flag changes show up in review
  • Keep logic in lib.rs and treat main.rs as a thin adapter

Don't

  • Change the --json output shape without a major version bump
  • Add a dependency for something the standard library already does
  • Prompt for input when stdin is not a terminal

Quality gates

| Gate | Tool | Threshold | When | |---|---|---|---| | Lint and types | rustfmt + clippy with -D warnings | 0 warnings, formatting clean | Every pull request | | Unit and component tests | The stack’s test runner | All pass; every design-skill component state covered | Every pull request | | Journey tests | End-to-end runner | One test per experience-skill journey: happy path + error state | Every pull request | | Accessibility | Automated audit (axe on web, platform audit on mobile) | WCAG 2.2 AA · no new violations | Every pull request | | Performance budget | Lab measurement in CI | Web: LCP ≤ 2.5s · INP ≤ 200ms · CLS ≤ 0.1 · ≤ 170KB JS per route. Mobile: cold start ≤ 2s · 60fps scroll | Every pull request | | Dependency and secret scan | cargo audit + cargo deny + secret scanner | No unpatched advisories; only allowed licences; no secrets | Every pull request and nightly | | Preview deploy | Hosting preview environment | A working preview link on the pull request | Every pull request | | Agent behaviour (HAI) | Journey tests for every agent step + a golden dataset of 20–50 cases (HAI skill, measurement) | Each agent step's failure response and handover are tested; the AI says it is AI; nothing commits without the person; 100% of AI actions in the audit log; no regression on the golden dataset | Every pull request that changes an AI feature; skip with "Not applicable — no AI assistant or agent" | | Cross-platform build | CI matrix: Linux, macOS, Windows | Release build and full test suite pass on every target | Every pull request |

The Accessibility gate checks Celestial accessibility core v1.0 (WCAG 2.2 AA); it can change tool but is never skipped. Automated: A11Y-C1, A11Y-F1, A11Y-I1, A11Y-N1, A11Y-N2, A11Y-S1, A11Y-L2, A11Y-P1 (partly). Manual pass before release: A11Y-K1, A11Y-G1, A11Y-R1, A11Y-M1, A11Y-O1, A11Y-V1, A11Y-E1, A11Y-U1, A11Y-L1 — keyboard-only run, screen reader on each journey step, 200% zoom and 320px width, reduced motion, captions on any media. Requirement IDs come from the design and experience skills this code builds. This layer owns: A11Y-F1 A visible focus indicator on every interactive element, never hidden behind sticky bars (WCAG 2.4.7, 2.4.11); A11Y-R1 Text works at 200% zoom, at 320px width, with increased text spacing and in either orientation (WCAG 1.4.4, 1.4.10, 1.4.12, 1.3.4); A11Y-M1 Motion respects the reduced-motion setting; anything moving for over 5 seconds can be paused; nothing flashes more than 3 times a second (WCAG 2.2.2, 2.3.1, 2.3.3 (AAA, advised)); A11Y-O1 Tooltips and pop-overs shown on hover or focus can be dismissed, hovered and stay until dismissed (WCAG 1.4.13); A11Y-I1 Images, icons and charts have text alternatives (empty for decoration); text is never an image (WCAG 1.1.1, 1.4.5); A11Y-V1 Video and audio have captions, transcripts or audio description; nothing plays sound automatically for over 3 seconds (WCAG 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.4.2); A11Y-K1 Every action works by keyboard in a logical order, with no keyboard traps; single-key shortcuts can be turned off or remapped (WCAG 2.1.1, 2.1.2, 2.1.4, 2.4.3); A11Y-G1 Nothing is drag-, gesture- or motion-only: each has a single tap or click alternative, and actions fire on release so they can be cancelled (WCAG 2.5.1, 2.5.2, 2.5.4, 2.5.7); A11Y-N1 Every page has a clear title, headings, landmarks and a skip link, and can be found more than one way (WCAG 2.4.1, 2.4.2, 2.4.5, 2.4.6); A11Y-N2 Controls have visible labels and instructions; links say where they go; the accessible name contains the visible label (WCAG 2.4.4, 2.5.3, 3.3.2); A11Y-S1 Status, results and errors are announced to screen readers (WCAG 4.1.3); A11Y-E2 Never ask for the same information twice in one flow; personal fields declare their purpose so they can be autofilled (WCAG 3.3.7, 1.3.5); A11Y-U1 Focusing or changing a field never moves the person somewhere else or submits without warning (WCAG 3.2.1, 3.2.2); A11Y-W1 Time limits warn first and can be extended (WCAG 2.2.1); A11Y-L2 The page language, and any passage in another language, is declared in code (WCAG 3.1.1, 3.1.2); A11Y-P1 Structure, order, names, roles and states are in the code: native elements first, ARIA only following the WAI-ARIA patterns (WCAG 1.3.1, 1.3.2, 4.1.2).

Workflow

  • Branching: Trunk-based: short-lived branches off main, squash-merged.

Review checklist

  • Correctness: does it do what the issue asks, including edge cases?
  • Readability: could a new teammate change it safely?
  • Architecture: is it in the right place, with the right boundaries?
  • Security: inputs, secrets, permissions, dependencies
  • Performance: budgets, queries, bundle size
  • Tests fail when the code is broken (flip a condition and re-run)
  • Reviewer runs the changed command locally against a fixture, including a failure case
  • Any change to flags, exit codes or --json output is called out in the description

Definition of done

  • Acceptance criteria met and demoed on the preview link
  • Tests added for new behaviour and every state
  • Accessibility and performance gates green
  • Analytics events named from the experience skill
  • Docs or changelog updated when behaviour changes
  • Help text and README usage updated for new or changed flags
  • Integration test covers success, bad input and exit code

Release

  • Small pull requests (aim for under 400 changed lines)
  • CI green before review
  • Squash-merge to main; main is always deployable
  • Every release has a written rollback path
  • Tag vX.Y.Z on main; CI builds binaries per target, writes checksums and attaches them to the release
  • Version in Cargo.toml follows semantic versioning; the CLI surface is the public API

Incidents

  • Yank or mark the broken release and point the install docs at the previous tag
  • Add a regression test reproducing the report before shipping the fix

Overrides of the engineering core

Preview shortened. The full SKILL.md is in the pack.