Rust code style

From JetBrains/intellij-community · View interactive version

Rust code style is an AI agent skill from the GitHub repository JetBrains/intellij-community. Write or review Rust in the build tools.

How to use this skill

Skill file

Rust Code Style

These rules apply to the Rust tool workspaces: the distribution tools, BT and the Air UI-lane tooling. The spec of a

workspace is CODE0. It names the files, the

Bazel targets and the checks that every workspace has. This skill holds the idioms.

First, find the audience

A tool has one of two audiences. The audience picks the error model and the command line grammar.

| | Action tool | Person- or agent-facing CLI |

|---|---|---|

| Writes the command line | Bazel, in an action | a person, an agent or a script |

| Examples | the packer, the collector, the composer, the descriptor writer | bt, vm |

| Error model | anyhow, ERROR: {e:#}, a nonzero exit | a typed Refusal with a stable code and exit |

| Command line | the cli crate, --key=value | clap derive |

| Crate closure | pinned in closure.txt | not pinned, except in the Air UI-lane tooling |

Bazel reads only "nonzero" from an action tool. A person or an agent branches on the exit code and on the refusal code.

1. Two error models

An action tool returns anyhow::Result from every function and prints the chain once. main.rs stays thin:

fn main() -> ExitCode {
    cli::main(run)
}

run takes the arguments and the error stream, as a test calls it, and returns the exit code: cli::exit_code turns a

result into 0, or into the code of the failure after cli::report prints ERROR: {error:#}. Each tool keeps its own

exit codes. bins/runtime-layout/src/main.rs of the distribution tools shows the shape.

An action tool that succeeds prints nothing. Bazel shows the output of a successful action as an INFO: From block.

A count or a report of a run goes into a tag of its span.

A person- or agent-facing CLI answers a Refusal { code, message, exit, details }. The code is the stable half that a

caller automates against. The shared crate community/tools/bt/crates/refusal holds the type, with the details as JSON

text. Its type parameter is the exit vocabulary, so each tool keeps its own exit codes: bt uses u8 with the codes 0

to 6 of community/tools/bt/crates/bt-core/src/exit.rs, and the Air UI-lane controller uses avl_base::Exit. A tool

that converts a refusal of another tool maps the exit by its meaning, never by its number.

2. Two command line grammars

Bazel writes the argv of an action tool, so the tool reads it with the cli crate. The crate depends only on anyhow,

and it refuses every other form with one text. community/build/dist-tools/crates/cli/API.md lists the refusals.

let mut options = cli::parse(args)?;
let output = options.require("--output")?;
let parts = options.take_all("--part")?;
options.finish()?; // refuses an option that the tool did not take

A person- or agent-facing CLI uses clap derive: help, environment variables, suggestions and -- passthrough.

community/tools/bt/bins/bt/src/options.rs is the example. Use no third grammar and no hand-written argument loop.

3. Format

4. Lints

without it.

the build, so a stale exception shows.

needs a banned call states why:

#[expect(
    clippy::disallowed_methods,
    reason = "the parent resolves the expected directory with the same call, so both sides agree on /private on macOS"
)]

5. Errors in libraries

error.downcast_ref::<io::Error>().

6. Types

distpath validates such a path once, at the input boundary.

7. Crates

goldens. Code that one binary uses lives in that binary.

community/build/dist-tools/bins/content-module-packer/closure.txt. A change to any crate in the closure reruns

every action of the tool, so <bin>_closure_test makes each new crate a visible choice.

lines replace it. State the reason in one sentence of the doc comment:

/// It is written by hand, because a crate dependency in the packer re-keys every packing action when the crate changes.
pub(crate) fn is_versioned_module_info(name: &str) -> bool {

error that names the input. The API.md of the crate lists each refusal.

8. Tests

lib.rs or main.rs live in src/tests.rs. There is no mod.rs: mod_module_files is on.

Under Bazel the path is relative, so make it absolute before the test changes the directory.

own library lives there: a unit test compiles a second copy of the library, so it cannot share types with the

testkit.

DT_TESTDATA_DIR, and the run-time CARGO_MANIFEST_DIR under cargo. The rules_rust process wrapper refuses

env!("CARGO_MANIFEST_DIR"). A file that include_str! reads is compile_data of the crate.

9. Docs

comment.

10. Versions

Refresh a crate hub

community/MODULE.bazel generates @dt and @bt from the workspace Cargo.toml and Cargo.lock. After a change of

Cargo.lock, update the Bazel lockfiles of both roots. For the distribution tools:

cd community && ./bazel.cmd build --nobuild --lockfile_mode=update //build/dist-tools/...
cd .. && ./bazel.cmd build --nobuild --lockfile_mode=update @community//build/dist-tools/... //build/dist-tools/...

For BT, use //tools/bt/... and @community//tools/bt/.... The extension watches only the workspace Cargo.toml and

Cargo.lock. A dependency that moves between [dev-dependencies] and [dependencies] of a member leaves the lock

unchanged. Then touch the workspace Cargo.toml, for example with a comment line, and run the commands again.

The gates

Run them before a commit, in addition to the gates of the workspace README:

cargo fmt --check && cargo clippy --all-targets      # in the workspace directory
bun community/build/rust-tools/sync.mjs --check
cd community && ./bazel.cmd test //build/dist-tools/... //tools/bt/... //build/rust-tools/...
RUSTC_BOOTSTRAP=1 cargo clippy -Zbuild-std=std,panic_abort --target x86_64-pc-windows-msvc --workspace --all-targets

From the ultimate root, also run ./bazel.cmd test @community//build/dist-tools/... //build/dist-tools/....

The clippy tests of a community crate run only from community/, because the clippy aspect skips a target of an

external repository.

Where things live

| What | Where |

|---|---|

| The spec of a workspace, the checks, how a workspace joins | CODE0 |

| The lint, format and clippy sources, and sync.mjs | community/build/rust-tools/ |

| The Bazel macro core: rust_tool_crate, optimized_binary, windows_clippy_tests | community/build/rust-tools/defs.bzl |

| The distribution tools: crate map, gates, agent rules | CODE0 and AGENTS.md |

| BT | CODE0 |

| The Air UI-lane tooling: crate map, gates, agent rules | CODE0 and AGENTS.md |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.