---
name: rust-module-builder
description: "A complete, idiomatic Rust source file built to an exact list of structs, enums, and function signatures, warning-free under clippy and documented, with unit tests when you ask for them. Use when a Rust module must be implemented from an exact list of structs, enums, and function signatures as a ready-to-compile file. Do not use for deciding the module's design, reviewing existing Rust code, or optimising it. Related topics: coding, rust."
---

# Rust Module Builder

A complete, idiomatic Rust source file built to an exact list of structs, enums, and function signatures, warning-free under clippy and documented, with unit tests when you ask for them.

## When to use

Use this skill when a Rust module must be implemented from an exact list of structs, enums, and function signatures as a ready-to-compile file. Do not use it for deciding the module's design, reviewing existing Rust code, or optimising it.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Implement each listed struct, enum, and function with the chosen visibility and no extra items.
3. Derive Debug on every type and Clone, PartialEq, and Default where the fields allow, with #[non_exhaustive] on enums expected to grow.
4. Route failures through a module-level error enum returned as Result rather than panicking.
5. Document every public item, and keep parameters borrowed, loops as iterators, and the file clippy-clean at the default level.
6. When tests are requested, add a #[cfg(test)] module with a main-path and an error-case test per function.

## Inputs

- **Module name** (free text, e.g. "my_module")
- **Purpose** (longer free text)
- **Public structs** (longer free text)
- **Public enums** (longer free text)
- **Functions** (longer free text)
- **Include unit tests** (choose one: Yes, No)
- **Visibility** (choose one: pub, pub(crate), private)
- **Input 8** (choose one: non_exhaustive)
- **Input 9** (choose one: cfg(test))

## Output

One code block containing the full module source, starting with a comment giving the file path, and no text outside the block.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Rust Module from a Spec

Write a complete Rust module file.

- Module name: {{Module name}}
- Purpose: {{Purpose}}
- Public structs: {{Public structs}}
- Public enums: {{Public enums}}
- Functions: {{Functions}}
- Include unit tests: {{Include unit tests}}
- Visibility: {{Visibility}}

Implement exactly the items listed, with the chosen visibility, and nothing speculative. Derive Debug on every type, plus Clone, PartialEq, and Default where the fields allow it, and use #{{Input 8}} on enums that will grow. Return Result with a module-level error enum for any function that can fail, instead of panicking. Write a doc comment on each public item that says what it does and, for functions, what errors it returns. Prefer borrowing over owned parameters, iterators over index loops, and match over chains of if let. Keep the code free of clippy warnings at the default level. When tests are requested, add a #{{Input 9}} module with one test per function covering the main path and one error case.

Return a single code block with the complete source. Make the first line a comment with the file path (for example // src/my_module.rs). Include no text outside the code block.

## Pitfalls

- Deriving Default on a struct with a field that has no Default impl, which fails to compile.
- Using unwrap on parse or I/O results inside library code, which turns recoverable errors into panics for every caller.
- Taking String parameters where &str would do, which forces every caller to allocate.
