rs-hack
Stop using sed on Rust code π¦
AST-aware refactoring tool for AI agents or other automated pipelines.
Why?
String-based search and replace (sed, Python regex) is fragile for code:
- Breaks on formatting changes
- Can't distinguish between similar patterns
- No semantic understanding
- Risk of partial matches
This tool uses Rust's syn parser to make precise, AST-aware edits based on actual code structure.
Use Cases
Perfect for AI agents making systematic changes across codebases:
β
Migration tasks: "Add #[derive(Clone)] to all structs"
β
API updates: "Add new field with default to 50 struct definitions"
β
Enum expansion: "Add Unknown variant to all enums, update matches"
β
Code generation: "Add builder methods to all structs with >3 fields"
Documentation
Three levels of documentation for different needs:
π HUMAN.md - Quick command reference
- Fast syntax lookup for humans
- One-liner examples for every command
- Perfect when you know what you want, just need the flags
π€ templates/claude-skills/rs-hack.md - Claude Code skill
- Complete workflows with best practices
- Teaches Claude Code how to use rs-hack effectively
- Copy to your project's
.claude/skills/directory
π README.md - You are here
- Complete documentation with examples
- Architecture and design decisions
- Integration guides
Installation
This project is organized as a Cargo workspace with three crates:
- rs-hack - The main CLI tool for AST-aware refactoring
- rshack - Convenient alias (no hyphens!) - same tool, easier to type
- rs-hack-mcp - MCP server for AI agent integration (Claude Desktop, etc.)
CLI Tool
From crates.io (choose one):
Both install the same tool - rshack is just easier to type!
From source:
MCP Server (for AI agents)
From crates.io:
From source:
Binaries will be installed to ~/.cargo/bin/.
What's New in 0.5.1
π Major Enhancements:
-
Unified Field API - Explicit, self-documenting flags
--field-name+--field-typefor struct definitions--field-name+--field-valuefor struct literals--field-name+ both for adding to both- No more parsing
"name: Type"strings!
-
Enum Variant Struct Literal Support - Operations on
View::Grid { ... }style patterns--kind structnow includes enum variant struct literals- Automatic literal-only mode for
::targets - Example:
rs-hack add --name View::Grid --field-name layer --field-value None --kind struct --apply
-
Trait Method Support - Complete function coverage
--kind functionnow includes trait method definitions- Rename works on trait methods, impl methods, and standalone functions
- Example:
rs-hack rename --kind function --name immediate_mode --to impulse_mode --apply
-
100% Formatting Preservation - Surgical editing everywhere
- Struct literal add/remove uses surgical editing (no more prettyplease!)
- Revert now preserves exact original formatting
- Defaults to surgical mode for all rename operations
-
Full Revert Support - All operations are now revertible
- Struct-literal operations fully supported
rs-hack revert <run-id>works for all commands- Preserves formatting on revert
-
Better UX - Improved error messages and result limiting
--limit Nfor limiting find results (no need for| head -20)- Helpful error messages for common mistakes (e.g.,
--inβ--paths) - Find command supports
--field-namefor field discovery
-
Cleaner Interface - Removed all deprecated commands
- Only modern unified commands (
add,remove,update,rename) - Deprecated hyphenated commands removed from help and MCP
- Simplified command list for better discoverability
- Only modern unified commands (
Quick Start
# v0.5.1: Enhanced field operations + formatting preservation
# NEW: Unified field API with explicit flags
# Add field to enum variant struct literals
# Add variant to enum (auto-detects it's an enum)
# Rename enum variant across entire codebase (preserves formatting!)
# Discover what exists (new discovery workflow)
# Shows: struct definitions, struct literals, identifiers, etc. (grouped by type)
# NEW: Limit results (no need for | head)
# NEW: Find all uses of a field
# Then operate on what you found (using same --name syntax)
# Use --kind for semantic grouping (struct = struct + struct-literal)
# Use --node-type for granular control (only struct definitions)
# Transform: Generic find-and-modify for any AST node
v0.5.0 Highlights β NEW
Unified Commands - 20+ specialized commands β 5 intuitive verbs:
find- Discover what exists (with auto-grouped output)add- Add fields, variants, methods, derives (auto-detects target type)remove- Remove fields, variants, methods (auto-detects target type)update- Update fields, variants (auto-detects target type)rename- Rename functions, enum variants (AST-aware)
Semantic Grouping with --kind:
Granular Control with --node-type:
Discovery Workflow:
# 1. Find what exists (omit --node-type to search everything)
# 2. Operate on it (same --name syntax)
Benefits:
- β
Consistent
--namesyntax across all commands - β Auto-detection: less typing, fewer flags to remember
- β Helpful hints when targets aren't found
- β Same mental model as Unix tools (find β operate)
Supported Operations
Unified Commands (5) β v0.5.0
- β
find: Discover AST nodes with powerful search
- Omit
--node-typeto search ALL types (auto-grouped output) - Use
--kindfor semantic grouping (struct, function, enum, etc.) - Use
--node-typefor granular control - Helpful hints when searches fail
- Omit
- β
add: Add fields, variants, methods, derives, use statements
- Auto-detects target type from context
--fieldβ struct field,--variantβ enum variant, etc.
- β
remove: Remove fields, variants, methods, derives
- Auto-detects target type from context
- β
update: Update fields, variants, match arms
- Auto-detects target type from context
- β
rename: Rename functions and enum variants (AST-aware)
- Use
--kindor--node-typefor disambiguation
- Use
Legacy Commands (Deprecated, use unified commands above)
- β οΈ add-struct-field, update-struct-field, remove-struct-field
- β οΈ add-enum-variant, update-enum-variant, remove-enum-variant
- β οΈ add-match-arm, update-match-arm, remove-match-arm
- β οΈ add-derive, add-impl-method, add-use
- See MIGRATION_v0.5.0.md for migration guide
Generic Transform (1)
- β
transform: Find and modify any AST nodes (comment, remove, or replace)
- Works with all node types
- Content filtering for precise targeting
State & Utilities (5)
- β history: View past operations
- β revert: Undo specific changes
- β clean: Remove old state
- β batch: Run multiple operations from JSON/YAML
- β
--format diff: Generate git-compatible patches
Pattern-Based Filtering
- β
--where: Filter targets by traits or attributes- Example:
--where "derives_trait:Clone"or--where "derives_trait:Clone,Debug"
- Example:
Usage
Glob Pattern Support
All commands now support glob patterns for targeting multiple files:
# Add derives to all structs in src directory
# Add match arms across multiple handler files
# Common glob patterns:
# src/**/*.rs - All .rs files in src and subdirectories
# src/models/*.rs - All .rs files in src/models
# src/**/handler.rs - All handler.rs files anywhere under src
Benefits:
- Perform bulk operations across your codebase
- Target specific directories or file patterns
- Ideal for migrations and refactoring tasks
Pattern-Based Filtering with --where
Filter which structs/enums to modify based on their traits or attributes:
# Add field only to structs that derive Clone
# Add Serialize to all types that already derive Clone OR Debug
# Update field only in Debug-enabled structs
# Remove variant only from enums with Clone
Filter Syntax:
derives_trait:Clone- Matches if type derives Clonederives_trait:Clone,Debug- Matches if type derives Clone OR Debug (OR logic)
Supported Operations:
- All struct operations:
add-struct-field,update-struct-field,remove-struct-field - All enum operations:
add-enum-variant,update-enum-variant,remove-enum-variant - Derive operations:
add-derive
Benefits:
- Selective refactoring: Only modify types that meet specific criteria
- Safe migrations: Add fields only to serializable types, etc.
- Powerful combinations: Combine with glob patterns for precise bulk operations
Unified Commands Examples (v0.5.0+)
Add Operations
# Add field to struct (auto-detects it's a struct)
# Add field with position control
# Add field to BOTH definition AND all literals
# Add enum variant (auto-detects it's an enum)
# Add derive macro (auto-detects target type)
# Add method to impl block
# Add use statement
# Add documentation comment (requires --node-type or --kind)
Remove Operations
# Remove struct field (auto-detects it's a struct)
# Remove enum variant field (from variant definition AND all literals)
# Remove enum variant field (literals only)
# Remove enum variant
# Remove derive
Update Operations
# Update struct field visibility
# Update struct field type
# Update enum variant
# Update match arm
Rename Operations
# Rename enum variant across entire codebase
# Rename function across entire codebase
# Validate rename (check for remaining references)
# Use --kind for disambiguation
Find Operations
# Discover what exists (searches ALL types, auto-grouped)
# Use --kind for semantic grouping
# Use --node-type for granular control
# Find with variant filtering
# Find with content filtering
Legacy Commands (Deprecated)
The following commands still work but are deprecated in favor of unified commands:
Struct Operations (Legacy - use add/remove/update instead)
# DEPRECATED: Use rs-hack add --name User --field "email: String"
Note: The --literal-default flag is optional. When omitted, only the struct definition is updated (original behavior). When provided, it also updates all struct literals with the given default value.
Add Field to Struct Literal Expressions Only
# Common case: field already exists in definition, just add to all literals
# Simply omit the type (:Type) and provide --literal-default
# This modifies initialization expressions like:
# IRCtx { stack: vec![], current_function_frame: None, return_type: None }
#
# NOT the struct definition:
# pub struct IRCtx { /* no change */ }
# How it works:
# - No ':' in --field means "literals only" (definition is skipped)
# - With ':' in --field, it tries definition (idempotent) + literals
# OLD (deprecated): add-struct-literal-field command
Pattern Matching for --struct-name (v0.4.0+):
The --struct-name parameter supports pattern matching to distinguish between struct literals and enum variant constructors:
# Match ONLY pure struct literals (no :: prefix)
# Matches: Rectangle { ... }
# Ignores: View::Rectangle { ... }
# Ignores: ViewType::Rectangle { ... }
# Match ANY path ending with Rectangle (wildcard)
# Matches: View::Rectangle { ... }
# Matches: ViewType::Rectangle { ... }
# Matches: Rectangle { ... }
# Match EXACT path only
# Matches: View::Rectangle { ... }
# Ignores: ViewType::Rectangle { ... }
# Ignores: Rectangle { ... }
Why This Matters:
Without explicit patterns, View::Rectangle { ... } is an enum variant constructor, not a struct literal:
// Enum definition
// Struct definition
// Usage:
let view = Rectangle ; // β Enum variant constructor
let rect = Rectangle ; // β Struct literal
The pattern matching prevents accidental modification of enum variants when you meant to target struct literals.
Update Field
# Change field visibility
# Change field type
Remove Field
Enum Operations
Add Variant
# Add simple variant (idempotent)
# Add variant with data
Update Variant
Remove Variant
Rename Variant β NEW
Rename an enum variant across the entire codebase in a type-safe, AST-aware manner:
# Rename across all files in src directory
# Dry-run with diff preview
# Summary format (shows only changed lines) β NEW Sprint 2
# Validate mode: check for remaining references β NEW Sprint 2
# Batch rename multiple variants (using batch command)
What it renames:
- β
Enum variant definitions (
pub enum IRValue { HashMapV2(Frame) }) - β
Match arm patterns (
IRValue::HashMapV2(frame) => { ... }) - β
Constructor calls (
let val = IRValue::HashMapV2(data)) - β
Reference patterns (
&IRValue::HashMapV2(_) => { ... }) - β
Struct patterns (
Some(IRValue::HashMapV2(f)) => { ... })
Benefits over sed/awk:
- β Type-safe: Only renames actual enum variants, not strings/comments
- β Complete: Finds all usages across patterns, expressions, and types
- β Fast: Processes entire codebase in seconds
- β Safe: Dry-run mode with diff preview
- β Reversible: Tracked in history for easy revert
New in Sprint 2:
- β Validation mode (
--validate): Check for remaining references after a rename- Helps identify patterns that weren't caught (e.g., fully qualified paths)
- Provides suggestions for fixing missed references
- Perfect for verifying completeness of large refactors
- β Summary format (
--format summary): Show only changed lines- Cleaner output than full diffs for large files
- Focuses on what actually changed
- Easier to review and verify changes
Real-world example:
The original motivation for this command was renaming IRValue::HashMapV2 β IRValue::HashMap across 23 files in the noisetable/koda codebase. What would have been a 4-6 hour manual refactor became a 30-second operation.
Match Arm Operations
Add Match Arm
# Add match arm (idempotent)
# Auto-detect missing variants and add them all
Auto-Detect Feature: The --auto-detect flag analyzes your enum definition and match expressions to automatically add match arms for ALL missing variants. This is perfect for:
- Ensuring exhaustive match coverage after adding new enum variants
- Quickly scaffolding match expressions with placeholder implementations
- Maintaining consistency across multiple match sites
Update Match Arm
Remove Match Arm
Note: Match operations automatically format the modified function using prettyplease to ensure consistent, readable code.
Derive Macros
# Add derive macros (idempotent)
# Works with enums too
Impl Methods
# Add method to impl block
# With position control
Use Statements
# Add use statement (idempotent)
# Position control
Find AST Nodes
Locate definitions (structs, enums, functions) in a single file:
# Find struct definition location
# Find enum definition
# Output (JSON):
# [{
# "line": 10,
# "column": 0,
# "end_line": 15,
# "end_column": 1
# }]
Inspect AST Nodes
List and view AST nodes (struct literals, etc.) across multiple files with glob support:
# List all Shadow struct initializations
# Output:
# // tests/shadow_bold.rs:42:18 - Shadow
# Shadow { offset: Vec2::new(2.0, 2.0), blur: 4.0, color: Color32::BLACK, }
#
# // tests/shadow_test.rs:15:20 - Shadow
# Shadow { offset: Vec2::ZERO, blur: 0.0, color: Color32::WHITE, }
# Get locations only (like grep -n but AST-aware)
# Output:
# src/app.rs:25:18
# src/config.rs:45:12
# src/main.rs:10:21
# Get structured JSON output
# List ALL struct literals (no name filter)
# Find match arms for specific enum variant (better than grep!)
# Output:
# // src/format/print.rs:766:12 - Operator::AssertSome
# Operator::AssertSome => write!(f, "!_"),
#
# // src/eval.rs:45:12 - Operator::AssertSome
# Operator::AssertSome => self.unwrap_or_panic(value),
# Find ALL match arms in a file
# Find enum variant usages (better than grep!)
# Output:
# // src/format/print.rs:763:12 - Operator::PropagateError
# Operator::PropagateError
#
# // src/eval.rs:120:25 - Operator::PropagateError
# Operator::PropagateError
# Find ALL usages of any Operator variant
|
# Find all calls to a specific function
# Output:
# // src/error.rs:42:4 - handle_error
# handle_error()
#
# // src/parser.rs:156:8 - handle_error
# handle_error(err)
# Find all .unwrap() calls (great for auditing!)
# Find all references to a variable/identifier
# Find all usages of a type
# Output:
# // src/lib.rs:15:18 - Vec<String>
# Vec<String>
#
# // src/lib.rs:42:11 - Vec<i32>
# Vec<i32>
# Find all eprintln! macros (NEW!)
# Find eprintln! macros with specific content (NEW!)
NEW: Discovery Mode (v0.5.0+)
When exploring unfamiliar code, you often don't know what node-type something is. Simply omit --node-type to search ALL types with auto-grouped output:
# Find "Rectangle" anywhere - don't know if it's a struct, enum variant, or what?
# Output (auto-grouped by type):
# Found "Rectangle" in 3 contexts:
#
# struct:
# // src/shapes.rs:10:0 - Rectangle
# pub struct Rectangle { width: f32, height: f32 }
#
# struct-literal (2 matches):
# // src/main.rs:15:12 - Rectangle
# Rectangle { width: 10.0, height: 5.0 }
#
# // src/tests.rs:42:20 - Rectangle
# Rectangle { width: 0.0, height: 0.0 }
#
# identifier (5 matches):
# // src/shapes.rs:10:11 - Rectangle
# Rectangle
# ... (4 more)
Hints System: If a specific search finds nothing, but matches exist in other node types, you'll get helpful hints:
)
)
NEW: Enum Variant Filtering (v0.5.0+)
Four flexible ways to filter enum variants:
# 1. Find ANY enum with a Rectangle variant
# Output:
# // src/view.rs:3:0 - View
# pub enum View {
# Rectangle { color: String, ... },
# // ... 7 other variants
# }
#
# // src/shapes.rs:17:0 - Shape
# pub enum Shape {
# Rectangle { width: f32, height: f32 },
# // ... 2 other variants
# }
# 2. Find specific enum + variant
# 3. Same using :: syntax (more concise)
# 4. Wildcard: any enum with Rectangle variant (same as #1, different syntax)
Supported Node Types:
struct-literal- Struct initialization expressionsmatch-arm- Match expression armsenum-usage- Enum variant references/usages anywhere in codefunction-call- Function invocationsmethod-call- Method callsmacro-call- Macro invocations (e.g.,println!,eprintln!,todo!) β NEWidentifier- Any identifier referencetype-ref- Type usages
Output Formats:
snippets(default): Shows file location + formatted code on single linelocations: File:line:column format (great for piping to other tools)json: Structured data with full location info and code snippets
Use Cases:
- Better than grep: Find code without false positives from comments/strings
- Multi-file search: Use glob patterns to search across many files
- Extract code chunks: Get full struct literal/match arm/path content, not just the first line
- Prepare for refactoring: Inspect before bulk modifications
- Find enum usage: Locate all places where a specific enum variant is used (matches, returns, comparisons, etc.)
- Track variant usage: See everywhere
Status::ActiveorOperator::Errorappears in your codebase - Audit function calls: Find all calls to specific functions (e.g.,
handle_error,format_operator) - Audit method calls: Find all
.unwrap(),.clone(), or.to_string()calls - Track identifiers: Find all references to variables, constants, or parameters
- Type usage analysis: See where types like
Vec,Option, orResultare used
Transform - Generic Find and Modify β NEW
The transform command provides a generic way to find and modify any AST nodes. It combines the power of inspect with mutation capabilities, keeping the API surface small while offering maximum flexibility.
Perfect for AI agents: One command to learn instead of dozens of specialized operations.
Basic Usage
# Comment out all eprintln! macros containing "[SHADOW RENDER]"
# Remove all .unwrap() calls in renderer code
# Comment out all todo!() macros
# Replace specific function calls
Supported Actions
comment: Wraps matched nodes in// ...commentsremove: Deletes matched nodes entirelyreplace: Replaces with provided code (via--withflag)
Supported Node Types
Works with all node types from inspect:
macro-call- Macro invocations (e.g.,println!,eprintln!,todo!)method-call- Method calls (e.g.,.unwrap(),.clone())function-call- Function invocationsenum-usage- Enum variant referencesstruct-literal- Struct initialization expressionsmatch-arm- Match expression armsidentifier- Any identifier referencetype-ref- Type usages
Filtering Options
Name Filter (--name): Filter by the name of the node
# Only eprintln! macros, not println!
Content Filter (--content-filter): Filter by source code content
# Only macros containing specific text
Combined Filters: Use both for precise targeting
Real-World Examples
Clean up debug logs:
# Comment out all debug eprintln! statements
Remove dangerous unwrap calls:
# Remove all .unwrap() calls (review first!)
Migrate from old to new API:
# Replace old function calls
Workflow: Use inspect first to see what matches, then transform to modify:
# 1. See what will be affected
# 2. Apply transformation (dry-run first!)
# 3. Apply for real
Why Transform is Better than Specialized Commands:
- β Single command for AI agents to learn
- β Works with any AST node type
- β Content filtering for precise targeting
- β Composable with all inspect node types
- β Fewer commands = smaller API surface
Batch Operations
Create a JSON or YAML file with multiple operations β YAML support NEW in Sprint 3:
JSON format:
YAML format (easier for humans to write):
base_path: src/
operations:
- type: RenameEnumVariant
enum_name: Status
old_variant: DraftV2
new_variant: Draft
edit_mode: surgical
- type: RenameEnumVariant
enum_name: Status
old_variant: PublishedV2
new_variant: Published
edit_mode: surgical
- type: RenameFunction
old_name: process_event_v2
new_name: process_event
Run batch:
# Auto-detects format from file extension
# With exclude patterns β NEW in Sprint 3
Exclude Patterns β NEW in Sprint 3
Skip certain paths during operations using glob patterns:
# Exclude test fixtures and deprecated code
# Exclude multiple patterns (use --exclude multiple times)
# Works with any command that accepts --paths
Use cases:
- Skip test fixtures that should remain unchanged
- Exclude deprecated code that will be removed
- Avoid vendored/third-party code
- Selective refactoring of specific modules
Pattern matching:
- Glob patterns:
**/tests/**,src/deprecated/*.rs - Simple strings:
deprecated,test(matches anywhere in path) - Multiple patterns: Use
--excludemultiple times
Documentation Comment Operations
Add, update, or remove documentation comments systematically:
# Add documentation to items
# Update existing documentation
# Remove documentation
Supported targets: struct, enum, function
Comment styles: line (///) or block (/** */)
Diff Output
Generate git-compatible patches for review before applying:
# Generate diff for review
# Output:
# --- src/user.rs
# +++ src/user.rs
# @@ -1,5 +1,6 @@
# pub struct User {
# id: u64,
# + age: u32,
# name: String,
# }
# Save to patch file
# Apply with git
# Or apply AND show diff
Perfect for AI-generated changes that need human review!
State Storage and Revert System
rs-hack includes a powerful state tracking and revert system that allows you to safely experiment with changes and undo them if needed. This is especially useful for AI agents that want to try different approaches.
How It Works
Every time you run a command with --apply, rs-hack:
- Generates a unique run ID (7 characters, like git)
- Backs up only the AST nodes being modified (not entire files)
- Computes checksums for integrity verification
- Stores operation metadata for auditing
Commands
View History
# Show last 10 runs
# Show last 50 runs
# Example output:
# Recent runs (showing up to 10):
#
# a05a626 2025-11-01 18:45 AddStructField 1 file [can revert]
# def456a 2025-11-01 09:15 add-derive 1 file [can revert]
# ghi789b 2025-10-31 16:45 add-match-arm 2 files [reverted]
Revert Changes
# Revert a specific run
# Force revert even if files have changed since
Clean Old State
# Clean runs older than 30 days (default)
# Keep only last 7 days
State Directory
rs-hack stores state in different locations based on your needs:
Priority order:
- Custom directory (via
RS_HACK_STATE_DIRenvironment variable) - highest priority - Local state (via
--local-stateflag) - uses./.rs-hackin current directory - Global default - uses system data directory (
~/.rs-hackon Unix-like systems)
Using Environment Variable (Recommended for Testing)
# Set custom state directory
# View history from custom state
RS_HACK_STATE_DIR=/tmp/my-test-state
# Revert using custom state
RS_HACK_STATE_DIR=/tmp/my-test-state
# Perfect for CI/CD or isolated testing
RS_HACK_STATE_DIR=/path/to/ci/state
Note: The environment variable takes precedence over --local-state, allowing you to override state location for testing without changing commands.
Using Local State Flag
# Use ./.rs-hack directory for state storage
# View history from local state
# Revert using local state
Using Global State (Default)
# No flag needed - uses ~/.rs-hack by default
Safety Features
- Hash Verification: Ensures files haven't changed before reverting (unless
--force) - Atomic Operations: Uses temp files and atomic renames
- AST Node Backups: Stores only modified nodes (85-95% space savings)
- Auto-Cleanup: Removes old backups with
cleancommand - Idempotent: Safe to run operations multiple times
AI Agent Workflow Example
# AI tries adding a field
# Output: Run ID: a05a626
# AI runs tests - they fail!
# AI reverts the change
# Output: β Run a05a626 reverted successfully
# AI tries a different approach
# Output: Run ID: b12c789
# Tests pass!
Use Cases
- Experimentation: Try changes and easily revert if they don't work
- Multi-step Migrations: Revert to any checkpoint if something breaks
- Debugging: Understand what changed when tests start failing
- Safety Net: Confidence to let AI agents make changes automatically
Storage Format
~/.rs-hack/
runs.json # Index of all runs
a05a626.json # Metadata for run a05a626
a05a626/ # Backup directory
node_0.json # Modified struct (AST node only)
node_1.json # Modified enum (AST node only)
Note: Only modified AST nodes are backed up (not entire files), resulting in 85-95% space savings.
Claude Code Integration
rs-hack works seamlessly with Claude Code via the Bash toolβno additional setup required!
Quick Setup
-
Install rs-hack:
-
(Optional) Add skill for guided usage:
Create
.claude/skills/rs-hack.mdin your project:Or copy from this repo's
templates/claude-skills/rs-hack.md
How It Works
Claude can use rs-hack directly through bash commands:
User: "Rename the enum variant IRValue::HashMapV2 to HashMap across all files"
Claude: I'll use rs-hack to safely rename that enum variant.
*Runs: rs-hack find to find usages*
*Runs: rs-hack rename-enum-variant with --format diff to preview*
*Shows you the diff*
*Runs: rs-hack rename-enum-variant --apply*
*Verifies with: cargo check*
β Renamed HashMapV2 β HashMap in 23 files
Best Practices
The skill teaches Claude to:
- β Always inspect before transforming
- β
Preview changes with
--format diffbefore applying - β Use glob patterns for multi-file operations
- β
Verify changes with
cargo check - β Track history and revert if needed
Example Workflows
Workflow 1: Large-Scale Refactoring
User: "Add a return_type field to all IRCtx struct literals"
Claude:
1. Inspects struct literals: rs-hack find --node-type struct-literal --name IRCtx
2. Previews changes: rs-hack add-struct-field ... --format diff
3. Shows you the diff for approval
4. Applies: rs-hack add-struct-field ... --apply
5. Verifies: cargo check
6. Reports: β Added field to 15 struct literals across 8 files
Workflow 2: Clean Up Debug Code
User: "Comment out all eprintln! macros with [DEBUG] in them"
Claude:
1. Finds matches: rs-hack find --node-type macro-call --name eprintln --content-filter "[DEBUG]"
2. Previews: rs-hack transform ... --action comment --format diff
3. Applies: rs-hack transform ... --action comment --apply
4. Reports: β Commented out 42 debug statements
Workflow 3: Safe Experimentation
User: "Try adding Clone to all structs and see if tests pass"
Claude:
1. Applies: rs-hack add-derive ... --apply
(saves run ID: a05a626)
2. Tests: cargo test
(tests fail!)
3. Reverts: rs-hack revert a05a626
4. Reports: Reverted changes, tests were incompatible
Why It Works Well
- Type-safe: Claude won't corrupt code with sed/awk
- Reversible: Every change tracked for easy revert
- Guided: Skill file teaches best practices
- Fast: Bulk operations across entire codebase
- Verifiable: Dry-run mode prevents accidents
Without Skill File
Claude can still use rs-hack effectively by reading the --help output, but the skill provides:
- Pre-learned command patterns
- Workflow best practices
- Common use case examples
- Error recovery patterns
Architecture
Core Components
- Parser (
syncrate): Parses Rust β AST - Editor: Manipulates AST and tracks byte positions
- Operations: Type-safe operation definitions
- CLI: User-friendly interface
Key Design Decisions
- Preserves formatting: Uses
prettypleasefor clean output - Idempotent: Running twice doesn't duplicate changes
- Fail-fast: Returns errors clearly, doesn't corrupt code
- Dry-run default: Must explicitly
--applyto modify files
Real-World Example: rs-hack vs perl/sed
The Problem: Perl Commands Are Dangerously Ambiguous
Consider this perl command that was used to add a field to struct initialization:
This command is DANGEROUSLY AMBIGUOUS because it matches text patterns without understanding Rust syntax:
What It Could Match (All Have the Same Text Pattern!)
// β Struct DEFINITION - probably NOT what you want
// β
Struct LITERAL - what you actually want
let ctx = IRCtx ;
// β COMMENT - corrupts your code!
// Example: current_function_frame: None, // β Matches! Corrupts comment
// β STRING - corrupts your string literal!
let s = "current_function_frame: None,"; // β Matches! Corrupts string
The perl command can't distinguish between these! It will modify ALL of them, likely corrupting your code.
β The Explicit, Safe Way (rs-hack)
rs-hack provides separate, explicit operations for each use case. You can update both in one command or separately:
Option 1: Update BOTH Definition and Literals (One Command!)
# NEW: Do BOTH in one command with --literal-default
This modifies BOTH the struct definition AND all struct literals:
// β
Struct definition updated
// β
All struct literals updated
let ctx = IRCtx ;
Option 2: Separate Operations (When You Need More Control)
Step 1: Modify Struct Definitions Only
Step 2: Modify Struct Literal Expressions Only
Benefits of Explicit Operations:
- β Explicit Intent: Command name tells you exactly what will be modified
- β AST-Aware: Only modifies actual Rust syntax nodes
- β Never Corrupts: Won't touch comments, strings, or unrelated code
- β Idempotent: Safe to run multiple times
- β Position Control: Precise placement of new fields
- β Glob Patterns: No manual file listing
- β Format-Independent: Works regardless of whitespace/formatting
- β Dry-Run Default: Preview changes before applying
vs Perl/Sed Problems:
- β Ambiguous: Can't distinguish struct definitions from literals from comments
- β Text-Based: Breaks on formatting changes
- β Not Idempotent: Running twice duplicates fields
- β No Validation: Can corrupt code on partial matches
- β Manual Files: Need to list every file explicitly
- β No Preview: Modifies files immediately
What rs-hack Does Behind the Scenes
- Parse each file into an AST using
syn - Traverse the AST to find struct definitions OR struct literal expressions (depending on operation)
- Validate the target exists and check if the field already exists (idempotent)
- Modify the AST by inserting the new field in the correct position
- Format the result with
prettyplease - Write back atomically
Safe, semantic, and correct every time. π¦
Comparison with Alternatives
| Tool | AST-Aware | Rust-Specific | AI-Friendly | Batch Ops | Idempotent |
|---|---|---|---|---|---|
sed |
β | β | β οΈ | β | β |
rust-analyzer |
β | β | β | β | β οΈ |
syn + custom |
β | β | β οΈ | β οΈ | β οΈ |
| rs-hack | β | β | β | β | β |
Features by Version
v0.5.0 - Unified Commands & Semantic Grouping (Current) β
- Unified Commands: 20+ specialized commands β 5 intuitive verbs
find- Discover what exists (with auto-grouped output)add- Add fields, variants, methods, derives (auto-detects target type)remove- Remove fields, variants, methods (auto-detects target type)update- Update fields, variants (auto-detects target type)rename- Rename functions, enum variants (AST-aware)
--kindflag: Semantic grouping for related node typesstructβ struct definitions + struct literalsfunctionβ function definitions + function callsenumβ enum definitions + enum usages- Plus:
match,identifier,type,macro,const,trait,mod,use
--node-typeflag: Granular control for specific AST node types- Conflicts with
--kindfor explicit control - Works on
find,add,remove,update,renamecommands
- Conflicts with
- Auto-detection: Commands infer target type from context
--fieldβ struct operation,--variantβ enum operation- Less typing, fewer flags to remember
- Helpful hints: When searches fail, suggests alternatives
- Discovery workflow:
findβ discover, then operate with same--name - Legacy commands deprecated: Old commands still work with migration hints
- Consistent
--namesyntax: Same pattern across all operations
v0.4.2 - Enum Variant Renaming β
rename-enum-variantcommand: Type-safe enum variant renaming across entire codebase- Renames variant in enum definitions, match patterns, constructors, and all usages
- Handles fully qualified (
Enum::Variant) and imported (Variant) paths - AST-aware: won't rename strings, comments, or unrelated identifiers
- Real-world impact: 4-6 hour refactor β 30 seconds
- Supports glob patterns for multi-file operations
- Integrates with state/revert system
- Example:
IRValue::HashMapV2βIRValue::HashMapacross 23 files
v0.4.0 - Generic Transform & Macro Support β
transformcommand: Generic find-and-modify operation for ANY AST nodes- Single command replaces need for dozens of specialized operations
- Actions:
comment,remove,replace - Works with all node types from
inspect - Perfect for AI agents: one pattern to learn
- Content filtering for precise targeting
- Macro call support: Find and modify macro invocations
- New
macro-callnode type ininspectandtransform - Great for cleaning up debug logs:
eprintln!,println!,todo!,dbg! - Content filtering: target specific debug categories
- New
- Enhanced
inspect: Added--content-filterflag- Filter nodes by their source code content
- Combine name and content filters for surgical precision
- Pattern matching for struct literals: Distinguish between struct literals and enum variants
--struct-name "Rectangle"β only pure struct literalsRectangle { ... }--struct-name "*::Rectangle"β any enum variant ending withRectangle(e.g.,View::Rectangle { ... })--struct-name "View::Rectangle"β exact path match only- Prevents accidental modification of enum variant constructors
- Simplified struct field commands:
add-struct-fieldnow handles all cases intelligently- No
--literal-defaultβ definition only --literal-defaultwith type (field: Type) β tries definition (idempotent) + always adds to literals--literal-defaultwithout type (field) β literals only (skips definition)- Common case: field exists, just use
--field "name" --literal-default "value" - Eliminates confusing dual-command footgun, natural API
- No
- State migration: Automatic handling of incompatible state from previous versions
- Integration tests: 36 tests including transform, pattern matching, and literal-only mode
- Documentation: Comprehensive examples and workflow guides
v0.3.2 - Pattern-Based Filtering & Inspection
--wherefilter: Pattern-based filtering for selective refactoring--where "derives_trait:Clone"- Filter by derived traits- OR logic support:
--where "derives_trait:Clone,Debug" - Works on all struct/enum operations +
add-derive
inspectcommand: AST-aware search and inspection- List struct literals, match arms, and enum variant usages across files
- Find all match arms handling a specific enum variant
- Find all places where an enum variant is referenced (complete grep replacement!)
- Three output formats:
snippets,locations,json - Glob pattern support for multi-file inspection
- Better than grep: no false positives, extracts full code chunks
- Enhanced
find: Improved documentation for locating definitions
v0.3.0 - State Storage, Revert & Diff Output
- State tracking: Every operation recorded with unique run ID
- Revert system: Undo changes with
rs-hack revert <run-id> - Diff output: Generate git-compatible patches with
--format diff - AST node backups: Stores only modified nodes (85-95% space savings)
- Configurable state: Use
RS_HACK_STATE_DIRenvironment variable - Commands:
history,revert,clean
v0.2.0 - Glob Patterns & Auto-Detect
- Glob patterns: Target multiple files with
"src/**/*.rs" - Auto-detect match arms: Find and add all missing enum variants
- Literal-default: Update struct definitions AND literals together
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests on GitHub.
License
MIT OR Apache-2.0
Credits
Built for AI agents to stop using sed on Rust code. π¦
Created by Leif Shackelford (@1e1f)