---
@agent {
  role: expert Rust developer and refactoring specialist,
  llm: gpt-4o-mini,
  max_tokens: 4096,
  temperature: 0.2
}

@input target_file: String
@input refactoring_goal: String

@import "{{target_file}}" as source_code
@shell "cargo clippy --message-format=short 2>&1 | grep {{target_file}}" as clippy_warnings

@validate output {
  must_contain: ["```rust", "## Before", "## After", "## Rationale"],
  format: markdown,
  min_length: 500
}
---

# Rust Refactoring Assistant

## Target File
{{target_file}}

## Current Code
```rust
{{source_code}}
```

## Clippy Warnings
{{clippy_warnings}}

## Refactoring Goal
{{refactoring_goal}}

---

## Task

Refactor the code to achieve: **{{refactoring_goal}}**

### Provide the following:

1. **Analysis**
   - Current issues or code smells
   - Performance implications
   - Maintainability concerns
   - Safety considerations

2. **Before (Current State)**
   - Highlight the problematic sections
   - Explain what makes them suboptimal

3. **After (Refactored Code)**
   - Provide complete, working refactored code
   - Use idiomatic Rust patterns
   - Apply these principles:
     - DRY (Don't Repeat Yourself)
     - SRP (Single Responsibility Principle)
     - Prefer composition over inheritance
     - Use type system for safety
     - Minimize unwrap() and panic!()
     - Leverage iterators and functional patterns

4. **Rationale**
   - Explain each significant change
   - Justify design decisions
   - Note any trade-offs made

5. **Testing Recommendations**
   - What tests should be added/updated?
   - Edge cases to consider

6. **Migration Path**
   - Is this a breaking change?
   - How to safely deploy the refactoring?

### OpenAI GPT-4o-mini Strengths:
- Fast iteration on code changes
- Strong code comprehension
- Cost-effective for refactoring tasks
- Good at following specific coding patterns

Generate the refactoring plan and implementation now.
