npx skills add ...
npx skills add pproenca/dot-skills --skill rust-implement
Write production-grade Rust code using a multi-pass approach. Design types first, then implement, then simplify, then verify with automated lint. Use this skill whenever writing new Rust functions, structs, modules, or features. Triggers on Rust implementation, new Rust code, Rust functions, Rust modules, error handling in Rust, async Rust, or type design in Rust.
npx skills add pproenca/dot-skills --skill rust-implement
Write code in passes. Experts don't produce perfect code in one shot -- they design, implement, review, and simplify. Follow this process for every module you write.
Before writing any implementation, write ONLY the type definitions and function signatures. No bodies. No logic.
Ask yourself these questions before moving on:
bool params with enums (Transformation 2).BTreeMap, not HashMap (Transformation 3)?Default (Transformation 6)?Option<T> fields where only one combination is valid at a time? Convert to an enum (Transformation 8).Do not proceed to Pass 2 until the types are right. Types are the architecture.
Fill in the function bodies. As you write each function, apply these rules:
? gets .context("failed to [verb] [noun]") -- no exceptions (Transformation 1).thiserror for error enums. For application code: use anyhow.Cow<str> when a function sometimes borrows and sometimes allocates (Transformation 7)._ => wildcards (Transformation 4).When adding a new field that needs to reach execution sites, trace the full path and explicitly name every intermediate layer before writing code. For each hop, state the module and struct/function that carries the value. If you skip a layer, the field silently disappears at runtime.
.await -- a held MutexGuard across an await blocks the executor.Drop impls. Brief wait, then fallback kill.kill_on_drop(true) for child processes that must not outlive their parent.BEGIN IMMEDIATE not BEGIN for SQLite write transactions (prevents SQLITE_BUSY_SNAPSHOT).ON CONFLICT DO NOTHING over read-then-write for idempotent inserts.When events arrive for entities that no longer exist (dead agents, closed threads), handle explicitly. Never silently drop -- log a warning and clean up stale state. Panicking on an orphan event is always wrong.
Walk this tree in order when you need shared access to data:
&T. Zero cost, zero complexity.Cow<'_, str>. Zero-cost when borrowed.Arc<T>. Prefer Arc::clone(&x) over x.clone().Arc<Mutex<T>> or Arc<RwLock<T>>. Choose RwLock when reads vastly outnumber writes.move closure with owned data.Every .clone() is a decision point. Ask: "Is this clone necessary, or can I restructure to borrow?"
| Context | Tool | Why |
|---|---|---|
| Application code (main, CLI, tests) | anyhow::Result + .context() | Rich error chains, no boilerplate |
| Library code (crates consumed by others) | thiserror enums | Typed, matchable, callers can branch on variants |
Error enum design: user-facing messages tell the user what to do, not what went wrong internally. Box<T> large payloads. Classify every variant explicitly in is_retryable() -- no wildcards.
When to Box::pin: When thin async wrappers inline large callee futures into their state machine, causing stack pressure. Wrap the inner call: Box::pin(self.inner_method(args)).await.
| Need | Channel | Why |
|---|---|---|
| One response back | oneshot | Exactly one value, then done |
| Stream of events | mpsc | Multiple producers, single consumer |
| Latest value only | watch | Receivers always see the most recent value |
| Broadcast to all | broadcast | Every receiver gets every message |
Shutdown: use CancellationToken for hierarchical shutdown. Never rely on Drop for ordered async cleanup. Never hold a MutexGuard across an .await point.
Review your code as if you're trying to REMOVE things, not add them.
Three diagnostic questions:
"Rewrite, don't rewire" principle: When the bug is in a function's internal logic, rewrite the body with explicit checks. Don't delegate to an existing API that happens to produce the correct result for now -- the explicit version is more auditable and survives upstream changes.
If you added more code than the task strictly requires, something is wrong. Cut it.
Run the bundled lint script on your code:
Fix every ERROR. Review every WARNING. Then do the manual checklist:
Fix every violation before presenting the code.
Each transformation shows the exact delta between first-draft and production-grade.
.context() on Every ? OperatorBEFORE:
AFTER:
Pattern: "failed to [verb] [noun]". The upstream error describes itself -- your job is to name the operation that broke.
BEFORE:
AFTER:
foo(true, false) is meaningless. Replace with enums so callsites read NetworkMode::Restricted, AccessLevel::ReadOnly. When you cannot change the API, add /*param_name*/ comments before opaque literals.
BEFORE: HashMap<String, Rule> -- AFTER: BTreeMap<String, Rule>
HashMap iteration order is random -- diffs become noisy, snapshot tests flake. Use BTreeMap whenever data is serialized, compared in tests, or shown to users.
BEFORE:
AFTER:
When a new variant is added, the compiler flags every match that needs updating. Wildcards hide this.
BEFORE: fn build_command() -> (Vec<String>, Vec<OwnedFd>) -- caller writes result.0
AFTER: fn build_command() -> CommandOutput -- caller writes output.args, output.preserved_fds
Named fields are self-documenting. Define a struct for any function returning 2+ values.
Default Derive on Config StructsDerive Default so callers override only what matters. For non-zero defaults, implement Default manually.
Cow<str> for Conditional OwnershipBEFORE:
AFTER:
Cow<str> avoids the unconditional allocation when only some paths need to allocate.
BEFORE:
AFTER:
When a struct has Option<T> fields valid only in certain combinations, it permits invalid states at the type level. Convert to an enum where each variant carries only its relevant data. Diagnostic: if you see 2+ Option<T> fields with is_some()/is_none() guards, it should be an enum.
BEFORE:
AFTER:
At 4+ parameters, callsites become hard to read and easy to misorder. An args struct names each field at the callsite and makes future parameter additions non-breaking.
let content = std::fs::read_to_string(path)?;
let config: Config = toml::from_str(&content)?;let content = std::fs::read_to_string(path)
.context("failed to read config file")?;
let config: Config = toml::from_str(&content)
.context("failed to parse TOML config")?;fn create_sandbox(network: bool, writable: bool) -> Sandbox {fn create_sandbox(network: NetworkMode, access: AccessLevel) -> Sandbox {match decision {
PolicyDecision::Allow => true,
_ => false,
}match decision {
PolicyDecision::Allow => true,
PolicyDecision::Block { .. } => false,
PolicyDecision::Rewrite { .. } => false,
PolicyDecision::Ask { .. } => false,
}#[derive(Default)]
pub struct ServerConfig {
pub timeout_secs: u64,
pub max_retries: u32,
pub bind_addr: String,
}
let config = ServerConfig { timeout_secs: 30, ..Default::default() };fn normalize_path(input: &str) -> String {
if needs_normalization(input) { input.replace('\\', "/") }
else { input.to_string() } // allocates even when unchanged
}fn normalize_path(input: &str) -> Cow<'_, str> {
if needs_normalization(input) { Cow::Owned(input.replace('\\', "/")) }
else { Cow::Borrowed(input) } // zero-cost when unchanged
}struct Permissions {
profile_name: Option<String>, // only for named profiles
sandbox_policy: Option<SandboxPolicy>, // only for legacy
file_paths: Option<Vec<PathBuf>>, // only when sandbox_policy is set
}enum Permissions {
Named { profile_name: String },
Legacy { sandbox_policy: SandboxPolicy, file_paths: Vec<PathBuf> },
}fn spawn_process(
cmd: &str,
args: &[String],
env: &BTreeMap<String, String>,
cwd: &Path,
stdin_policy: StdinPolicy,
sandbox: SandboxPolicy,
timeout: Duration,
) -> Result<Child> {struct SpawnArgs<'a> {
cmd: &'a str,
args: &'a [String],
env: &'a BTreeMap<String, String>,
cwd: &'a Path,
stdin_policy: StdinPolicy,
sandbox: SandboxPolicy,
timeout: Duration,
}
fn spawn_process(args: &SpawnArgs<'_>) -> Result<Child> {