Agent skill

RTK Rust Design Patterns

by rtk-ai in rtk-ai/rtk

Describes seven Rust design patterns for the RTK CLI filter modules, with when to use each, RTK examples, and notes on when a pattern is overkill.

Apache-2.0Auto-check passedDevelopment

Install RTK Rust Design Patterns

skills CLI
$ npx skills add rtk-ai/rtk --skill design-patterns -a claude-code

Project install by default; add -g for ~/.claude/skills/.

GitHub CLI
$ gh skill install rtk-ai/rtk design-patterns --agent claude-code

Project scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).

Manual copy
$ git clone --depth 1 https://github.com/rtk-ai/rtk.git skills-src && mkdir -p .claude/skills && cp -r skills-src/.claude/skills/design-patterns .claude/skills/design-patterns && rm -rf skills-src

Use ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.

Claude Code skills documentation · loads skills from .claude/skills/

Facts

Skill name
design-patterns
GitHub stars
83k
Token cost
~1.9k tokens
SKILL.md length
260 words
Files
1
Skills in repo
7
Repo updated
First seen
Licence
Apache-2.0

At a glance

Describes seven Rust design patterns for the RTK CLI filter modules, with when to use each, RTK examples, and notes on when a pattern is overkill.

  • Designing a new filter module in RTK
  • SKILL.md covers Pattern 1: Newtype (Type Safety), Pattern 2: Builder (Complex…, Pattern 3: State Machine… and Pattern 4: Trait Object…, plus 5 more sections
  • Instructions only: no scripts, shell commands, URLs or credentials in SKILL.md
  • Refactoring an existing module to a clearer pattern

What it does

RTK's filter module architecture is the subject of this pattern guide, which is aimed at CLI tool code rather than web or service code. Each pattern comes with a use-when rule and a Rust example from RTK: Newtype to stop primitives such as command names, paths and token counts being mixed up, Builder for structs with four or more optional fields, and State Machine for parsing multi-section output such as pytest results.

It also covers Trait Objects for command dispatch, RAII for temp files and SQLite connections, Strategy for swappable filter modes such as compact and summary, and Extension Traits for adding methods to types like str. It states limits as well: Builder is overkill for one to three obvious fields, and RTK's static match dispatch in main.rs is intentional until it passes about 20 commands. The skill only needs file reading and search tools.

When your agent uses it

  • Designing a new filter module in RTK
  • Refactoring an existing module to a clearer pattern
  • Deciding whether trait objects are warranted over the current match dispatch

Example prompts

  • “I am adding a filter for cargo test output, so which pattern fits its multiple sections?”
  • “Refactor FilterConfig to use a builder.”
  • “Should the temp file handling in tee.rs use RAII, and how would it look?”

Requirements

  • The RTK Rust codebase
  • Pre-approved tools (allowed-tools): Read, Grep, Glob

What it can do on your machine

Read from SKILL.md and the folder at commit e0b2e85. It shows what the files ask for, not the result of running them.

  • Tool permissions

    Pre-approves these tools, so the agent can use them without asking each time:

    • Read
    • Grep
    • Glob

    From allowed-tools in the SKILL.md frontmatter.

  • Runs code

    No scripts in the folder and no shell commands in SKILL.md (its code samples are rust).

    From the folder's file list and the shell code blocks in SKILL.md.

  • Network

    No URLs in SKILL.md.

    From URLs in SKILL.md, links to its own repository left out.

  • Credentials

    Names no API keys, tokens, secrets or passwords.

    From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.

Context cost

RTK Rust Design Patterns loads about 1.9k tokens when it runs. Until then it costs about 48 tokens; SKILL.md has 260 words of instructions outside code blocks.

Always · name and description, kept in context so the agent knows when to use it
~48
When it runs · the whole SKILL.md, loaded when a task matches
~1.9k

Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.

Safety

Auto-check passed

The automated check found no risky patterns in SKILL.md.

Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); files beside SKILL.md are not scanned.

SKILL.md

The full file from rtk-ai/rtk at commit e0b2e85, republished under its Apache-2.0 licence (© rtk-ai). 260 words, ~1,859 tokens.

Download SKILL.mdSave it as .claude/skills/design-patterns/SKILL.md (or your agent's skills folder).
name
design-patterns
description
Rust design patterns for RTK. Newtype, Builder, RAII, Trait Objects, State Machine. Applied to CLI filter modules. Use when designing new modules or refactoring existing ones.
allowed-tools
Read, Grep, Glob
triggers
design pattern, how to structure, best pattern for, refactor to pattern
effort
medium
tags
rust, design-patterns, architecture, newtype, builder, rtk

RTK Rust Design Patterns

Patterns that apply to RTK's filter module architecture. Focused on CLI tool patterns, not web/service patterns.

Pattern 1: Newtype (Type Safety)

Use when: wrapping primitive types to prevent misuse (command names, paths, token counts).

rust
// Without Newtype — easy to mix up
fn track(input_tokens: usize, output_tokens: usize) { ... }
track(output_tokens, input_tokens);  // Silent bug!

// With Newtype — compile error on swap
pub struct InputTokens(pub usize);
pub struct OutputTokens(pub usize);
fn track(input: InputTokens, output: OutputTokens) { ... }
track(OutputTokens(100), InputTokens(400));  // Compile error ✅
rust
// Practical RTK example: command name validation
pub struct CommandName(String);
impl CommandName {
    pub fn new(s: &str) -> Result<Self> {
        if s.contains(';') || s.contains('|') || s.contains('`') {
            anyhow::bail!("Invalid command name: shell metacharacters");
        }
        Ok(Self(s.to_string()))
    }
    pub fn as_str(&self) -> &str { &self.0 }
}

Pattern 2: Builder (Complex Configuration)

Use when: a struct has 4+ optional fields, many with defaults.

rust
#[derive(Default)]
pub struct FilterConfig {
    max_lines: Option<usize>,
    strip_ansi: bool,
    show_warnings: bool,
    truncate_at: Option<usize>,
}

impl FilterConfig {
    pub fn new() -> Self { Self::default() }
    pub fn max_lines(mut self, n: usize) -> Self { self.max_lines = Some(n); self }
    pub fn strip_ansi(mut self, v: bool) -> Self { self.strip_ansi = v; self }
    pub fn show_warnings(mut self, v: bool) -> Self { self.show_warnings = v; self }
}

// Usage — readable, no positional arg confusion
let config = FilterConfig::new()
    .max_lines(50)
    .strip_ansi(true)
    .show_warnings(false);

When NOT to use Builder: if the struct has 1-3 fields with obvious meaning. Over-engineering for simple cases.

Pattern 3: State Machine (Parser/Filter Flows)

Use when: parsing multi-section output (test results, build output) where context changes behavior.

rust
// RTK example: pytest output parsing
#[derive(Debug, PartialEq)]
enum ParseState {
    LookingForTests,
    InTestOutput,
    InFailureSummary,
    Done,
}

fn parse_pytest(input: &str) -> String {
    let mut state = ParseState::LookingForTests;
    let mut failures = Vec::new();

    for line in input.lines() {
        match state {
            ParseState::LookingForTests => {
                if line.contains("FAILED") || line.contains("ERROR") {
                    state = ParseState::InFailureSummary;
                    failures.push(line);
                }
            }
            ParseState::InFailureSummary => {
                if line.starts_with("=====") { state = ParseState::Done; }
                else { failures.push(line); }
            }
            ParseState::Done => break,
            _ => {}
        }
    }
    failures.join("\n")
}

Pattern 4: Trait Object (Command Dispatch)

Use when: different command families need the same interface. Avoids massive match arms.

rust
// Define a common interface for filters
pub trait OutputFilter {
    fn filter(&self, input: &str) -> Result<String>;
    fn command_name(&self) -> &str;
}

pub struct GitFilter;
pub struct CargoFilter;

impl OutputFilter for GitFilter {
    fn filter(&self, input: &str) -> Result<String> { filter_git(input) }
    fn command_name(&self) -> &str { "git" }
}

// RTK currently uses match-based dispatch in main.rs (simpler, no dynamic dispatch overhead)
// Trait objects are useful if filter registry becomes dynamic (e.g., TOML-loaded plugins)

Note: RTK's current match dispatch in main.rs is intentional — static dispatch, zero overhead. Only move to trait objects if the match arm count exceeds ~20 commands.

Pattern 5: RAII (Resource Management)

Use when: managing resources that need cleanup (temp files, SQLite connections).

rust
// RTK tee.rs — RAII for temp output files
pub struct TeeFile {
    path: PathBuf,
}

impl TeeFile {
    pub fn create(content: &str) -> Result<Self> {
        let path = tee_path()?;
        fs::write(&path, content)
            .with_context(|| format!("Failed to write tee file: {}", path.display()))?;
        Ok(Self { path })
    }

    pub fn path(&self) -> &Path { &self.path }
}

// No explicit cleanup needed — file persists intentionally (rotation handled separately)
// If cleanup were needed: impl Drop { fn drop(&mut self) { let _ = fs::remove_file(&self.path); } }

Pattern 6: Strategy (Swappable Filter Logic)

Use when: a command has multiple filtering modes (e.g., compact vs. verbose).

rust
pub enum FilterMode {
    Compact,    // Show only failures/errors
    Summary,    // Show counts + top errors
    Full,       // Pass through unchanged
}

pub fn apply_filter(input: &str, mode: FilterMode) -> String {
    match mode {
        FilterMode::Compact => filter_compact(input),
        FilterMode::Summary => filter_summary(input),
        FilterMode::Full => input.to_string(),
    }
}

Pattern 7: Extension Trait (Add Methods to External Types)

Use when: you need to add methods to types you don't own (like &str for RTK-specific parsing).

rust
pub trait RtkStrExt {
    fn is_error_line(&self) -> bool;
    fn is_warning_line(&self) -> bool;
    fn token_count(&self) -> usize;
}

impl RtkStrExt for str {
    fn is_error_line(&self) -> bool {
        self.starts_with("error") || self.contains("[E")
    }
    fn is_warning_line(&self) -> bool {
        self.starts_with("warning")
    }
    fn token_count(&self) -> usize {
        self.split_whitespace().count()
    }
}

// Usage
if line.is_error_line() { ... }
let tokens = output.token_count();

RTK Pattern Selection Guide

SituationPatternAvoid
New *_cmd.rs filter moduleStandard module pattern (see CLAUDE.md)Over-abstracting
4+ optional config fieldsBuilderStruct literal
Multi-phase output parsingState MachineNested if/else
Type-safe wrapper around stringNewtypeRaw String
Adding methods to &strExtension TraitFree functions
Resource with cleanupRAII / DropManual cleanup
Dynamic filter registryTrait ObjectMatch sprawl

Anti-Patterns in RTK Context

rust
// ❌ Generic over-engineering for one command
pub trait Filterable<T: CommandArgs + Send + Sync + 'static> { ... }

// ✅ Just write the function
pub fn filter_git_log(input: &str) -> Result<String> { ... }

// ❌ Singleton registry with global state
static FILTER_REGISTRY: Mutex<HashMap<String, Box<dyn Filter>>> = ...;

// ✅ Match in main.rs — simple, zero overhead, easy to trace

// ❌ Async traits for "future-proofing"
#[async_trait]
pub trait Filter { async fn apply(&self, input: &str) -> Result<String>; }

// ✅ Synchronous — RTK is single-threaded by design
pub trait Filter { fn apply(&self, input: &str) -> Result<String>; }

© rtk-ai, Apache-2.0. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file

Files

Just SKILL.md in .claude/skills/design-patterns of rtk-ai/rtk.

Open the folder on GitHubat commit e0b2e85

Compare with similar skills

RTK Rust Design Patterns next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.

RTK Rust Design Patterns compared with similar skills
SkillStarsUsed inTokensAuto-checkLicenceRepo updated
RTK Rust Design Patterns this skillrtk-ai/rtk83k—~1.9kAutomated safety check: PassApache-2.0
AST Visitor Pattern for Unionsprisma/orm48k—~830Automated safety check: PassApache-2.0
Typed Dependencies with fp-go EffectIBM/fp-go2k—~4.3kAutomated safety check: PassApache-2.0
Rust Architectsergigp/yarrtube127—~6.1kAutomated safety check: PassMIT
Migrate Core Code to Submodulestinyhumansai/openhuman41k—~2.6kAutomated safety check: PassGPL-3.0
Swiftui View RefactorDimillian/Skills4k5 repos~2kAutomated safety check: PassMIT

Similar skills

  • Official

    Replaces a plain TypeScript union plus switch statements with frozen subclasses and a visitor interface when several places dispatch on the same variants.

    48k GitHub stars~830 tokensUpdated today
    DevelopmentAuto-check passed
  • Teaches an agent to write fp-go v2 services with the Effect type, carrying dependencies in its type parameter instead of in context.Context or parameters.

    2k GitHub stars~4.3k tokensUpdated today
    DevelopmentAuto-check passed
  • Rust Architect

    sergigp/yarrtube

    Architecture, naming, and testing conventions guidelines for architecting Rust codebases.

    127 GitHub stars~6.1k tokensUpdated 2 days ago
    DevelopmentAuto-check passed
  • Migrate Core Code to Submodules

    tinyhumansai/openhuman

    Plans and carries out moving non-host-specific code and its tests from the OpenHuman core into vendored tiny submodule libraries, then releases the submodule and re-pins the host.

    41k GitHub stars~2.6k tokensUpdated today
    DevelopmentAuto-check passed
  • Swiftui View Refactor

    Dimillian/Skills

    Refactor and review SwiftUI view files with strong defaults for small dedicated subviews, MV-over-MVVM data flow, stable view trees, explicit dependency injection, and correct Observation usage.

    4k GitHub starsUsed in 5 repos~2k tokens
    DevelopmentAuto-check passed
  • Guide for writing idiomatic Rust code based on Apollo GraphQL's best practices handbook.

    5.6k GitHub starsUsed in 3 repos~1.1k tokens
    DevelopmentAuto-check passed

More from rtk-ai/rtk

  • Enforces red-green-refactor for Rust work, with idiomatic test patterns, a naming convention and a pre-commit gate of cargo fmt, clippy and test.

    83k GitHub stars~753 tokensUpdated today
    Auto-check: notes
  • Enforces red-green-refactor for new RTK output filters in Rust, using real captured fixtures, snapshot tests with insta and token-savings assertions.

    83k GitHub stars~1.9k tokensUpdated today
    Auto-check: notes
  • Reviews RTK's Rust code for over-engineering and verbose patterns, applying idioms like iterator chains and early returns while protecting a specific list of constraints from being simplified away.

    83k GitHub stars~1.1k tokensUpdated today
    Auto-check passed
  • Audits open GitHub issues, categorizes them, flags duplicates and linked PRs in three phases, with optional deep analysis and comments posted only after validation.

    83k GitHub stars~3k tokensUpdated today
    Auto-check: notes
  • PR Triage

    rtk-ai/rtk

    Audits a repository's open pull requests, deep-reviews chosen ones and drafts review comments that are only posted after you approve them.

    83k GitHub stars~2.5k tokensUpdated today
    Auto-check: notes
  • Runs issue triage and PR triage in parallel, then cross-analyzes the results to flag duplicate coverage, security gaps, P0 issues with no PR, and PR conflicts.

    83k GitHub stars~1.6k tokensUpdated today
    Auto-check: notes

Works with

Categories

Questions about RTK Rust Design Patterns

What does RTK Rust Design Patterns do?

Describes seven Rust design patterns for the RTK CLI filter modules, with when to use each, RTK examples, and notes on when a pattern is overkill. RTK's filter module architecture is the subject of this pattern guide, which is aimed at CLI tool code rather than web or service code. Each pattern comes with a use-when rule and a Rust example from RTK: Newtype to stop primitives such as command names, paths and token counts being mixed up, Builder for structs with four or more optional fields, and State Machine for parsing multi-section output such as pytest results.

When should I use RTK Rust Design Patterns?

RTK Rust Design Patterns fits situations like: designing a new filter module in RTK; refactoring an existing module to a clearer pattern; deciding whether trait objects are warranted over the current match dispatch.

How do I install RTK Rust Design Patterns in Claude Code?

Run `npx skills add rtk-ai/rtk --skill design-patterns -a claude-code`. Or copy the skill folder (.claude/skills/design-patterns in rtk-ai/rtk) into .claude/skills/design-patterns in your project. Claude Code loads it when a task matches its description.

How do I install RTK Rust Design Patterns in Codex?

Run `npx skills add rtk-ai/rtk --skill design-patterns -a codex`. Or copy the skill folder (.claude/skills/design-patterns in rtk-ai/rtk) into .agents/skills/design-patterns in your project. Codex loads it when a task matches its description.

Can I use RTK Rust Design Patterns in Cursor, Gemini CLI or GitHub Copilot?

Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add rtk-ai/rtk --skill design-patterns -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/design-patterns, .gemini/skills/design-patterns, .github/skills/design-patterns and .opencode/skills/design-patterns in your project.

What does RTK Rust Design Patterns need to run?

SKILL.md names no scripts, command-line tools or credentials: RTK Rust Design Patterns is instructions for the agent only. Our summary lists: The RTK Rust codebase. Its frontmatter pre-approves these tools: Read, Grep, Glob.

Does RTK Rust Design Patterns access the network?

SKILL.md contains no URLs. Any network use would come from the scripts or tools the agent runs. This is read from the text; nothing was executed.

Is RTK Rust Design Patterns safe to install?

Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. Review the folder before installing.

What licence does RTK Rust Design Patterns use?

RTK Rust Design Patterns is published under the Apache-2.0 licence (the repository's licence). It allows redistribution, so the full SKILL.md is shown on this page.

How many tokens does RTK Rust Design Patterns use?

About 1.9k tokens (SKILL.md is roughly 7.4k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full.

What are the alternatives to RTK Rust Design Patterns?

Skills that share tags, products or a category with RTK Rust Design Patterns: AST Visitor Pattern for Unions (prisma/orm, 48k stars), Typed Dependencies with fp-go Effect (IBM/fp-go, 2k stars), Rust Architect (sergigp/yarrtube, 127 stars) and Migrate Core Code to Submodules (tinyhumansai/openhuman, 41k stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.

Who maintains RTK Rust Design Patterns?

rtk-ai (a GitHub organization) maintains it in rtk-ai/rtk, which has 82,568 GitHub stars. The repository holds 7 skills in this directory. The repository was last updated on October 7, 2026.

Source: rtk-ai/rtk on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.