design-patterns

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.

By rtk-ai · 1,322 installs

npx skills add rtk-ai/rtk --skill design-patterns

Source repository · Upstream listing

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). Pattern 2: Builder (Complex Configuration) Use when: a struct has 4+ optional fields, many with defaults. 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. Pattern 4: Trait Object (Command Dispatch) Use when: different command families need the same interface. Avoids massive match arms. 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). Pattern 6: Strategy (Swappable Filter Logic) Use when: a command has multiple filtering modes (e.g., compact vs. verbose). 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). RTK Pattern Selection Guide Situation Pattern Avoid New cmd.rs filter module Standard module pattern (see CLAUDE.md) Over abstracting 4+ optional config fields Builder Struct literal Multi phase output parsing State Machine Nested if/else Type safe wrapper around string Newtype Raw String Adding methods to &str Extension Trait Free functions Resource with cleanup RAII / Drop Manual cleanup Dynamic filter registry Trait Object Match sprawl Anti Patterns in RTK Context