rs-hack 0.5.5

AST-aware Rust refactoring tool for AI agents - transform, rename, inspect & more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Data types for all refactoring operations: add, remove, rename,
//! update, transform, and batch. Defines EditMode, BackupNode,
//! and the operation result types.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Edit mode for operations - controls how changes are applied to source files
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EditMode {
    /// Surgical mode: preserve all formatting, only change specific locations
    /// This is the recommended default for minimal diffs
    Surgical,
    /// Reformat mode: use prettyplease to reformat the entire file
    /// Use this if you want consistent formatting across the file
    Reformat,
}

impl Default for EditMode {
    fn default() -> Self {
        EditMode::Surgical
    }
}

impl std::fmt::Display for EditMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            EditMode::Surgical => write!(f, "surgical"),
            EditMode::Reformat => write!(f, "reformat"),
        }
    }
}

impl std::str::FromStr for EditMode {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "surgical" => Ok(EditMode::Surgical),
            "reformat" => Ok(EditMode::Reformat),
            _ => Err(format!("Invalid edit mode: {}. Valid values are 'surgical' or 'reformat'", s)),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Operation {
    AddStructField(AddStructFieldOp),
    UpdateStructField(UpdateStructFieldOp),
    RemoveStructField(RemoveStructFieldOp),
    AddStructLiteralField(AddStructLiteralFieldOp),
    AddEnumVariant(AddEnumVariantOp),
    UpdateEnumVariant(UpdateEnumVariantOp),
    RemoveEnumVariant(RemoveEnumVariantOp),
    AddMatchArm(AddMatchArmOp),
    UpdateMatchArm(UpdateMatchArmOp),
    RemoveMatchArm(RemoveMatchArmOp),
    AddImplMethod(AddImplMethodOp),
    AddUseStatement(AddUseStatementOp),
    AddDerive(AddDeriveOp),
    Transform(TransformOp),
    RenameEnumVariant(RenameEnumVariantOp),
    RenameFunction(RenameFunctionOp),
    AddDocComment(AddDocCommentOp),
    UpdateDocComment(UpdateDocCommentOp),
    RemoveDocComment(RemoveDocCommentOp),
    SetStructLiteralBase(SetStructLiteralBaseOp),
    AddCallArg(AddCallArgOp),
    UpdateCallArg(UpdateCallArgOp),
    RemoveCallArg(RemoveCallArgOp),
}

impl Operation {
    /// Stable string identifier for this operation, used in run metadata
    /// and for telemetry. Matches the variant name.
    pub fn kind_name(&self) -> &'static str {
        match self {
            Operation::AddStructField(_) => "AddStructField",
            Operation::UpdateStructField(_) => "UpdateStructField",
            Operation::RemoveStructField(_) => "RemoveStructField",
            Operation::AddStructLiteralField(_) => "AddStructLiteralField",
            Operation::AddEnumVariant(_) => "AddEnumVariant",
            Operation::UpdateEnumVariant(_) => "UpdateEnumVariant",
            Operation::RemoveEnumVariant(_) => "RemoveEnumVariant",
            Operation::RenameEnumVariant(_) => "RenameEnumVariant",
            Operation::AddMatchArm(_) => "AddMatchArm",
            Operation::UpdateMatchArm(_) => "UpdateMatchArm",
            Operation::RemoveMatchArm(_) => "RemoveMatchArm",
            Operation::AddImplMethod(_) => "AddImplMethod",
            Operation::AddUseStatement(_) => "AddUseStatement",
            Operation::AddDerive(_) => "AddDerive",
            Operation::Transform(_) => "Transform",
            Operation::RenameFunction(_) => "RenameFunction",
            Operation::AddDocComment(_) => "AddDocComment",
            Operation::UpdateDocComment(_) => "UpdateDocComment",
            Operation::RemoveDocComment(_) => "RemoveDocComment",
            Operation::SetStructLiteralBase(_) => "SetStructLiteralBase",
            Operation::AddCallArg(_) => "AddCallArg",
            Operation::UpdateCallArg(_) => "UpdateCallArg",
            Operation::RemoveCallArg(_) => "RemoveCallArg",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddStructFieldOp {
    pub struct_name: String,
    pub field_def: String, // e.g., "new_field: Option<String>" or just "new_field" if literal_default is provided
    pub position: InsertPosition,
    #[serde(default)]
    pub literal_default: Option<String>, // If provided: tries to add to definition (idempotent), always updates literals
    #[serde(default)]
    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateStructFieldOp {
    pub struct_name: String,
    pub field_def: String, // e.g., "field_name: NewType" (field name is parsed from this)
    #[serde(default)]
    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoveStructFieldOp {
    pub struct_name: String,
    pub field_name: String, // Name of the field to remove
    #[serde(default)]
    pub literal_only: bool, // If true, only remove from struct literals, not the definition
    #[serde(default)]
    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddStructLiteralFieldOp {
    pub struct_name: String,
    pub field_def: String, // e.g., "return_type: None"
    pub position: InsertPosition,
    #[serde(default)]
    pub struct_path: Option<String>,  // Optional canonical path (e.g., "crate::types::Rectangle")
}

/// Add or set the base expression (..expr) on struct literals
/// e.g., adds `..Default::default()` to `Foo { a: 1 }` → `Foo { a: 1, ..Default::default() }`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SetStructLiteralBaseOp {
    pub struct_name: String,
    /// The base expression (e.g., "Default::default()" or just "default")
    /// If "default", expands to "Default::default()"
    pub base_expr: String,
    #[serde(default)]
    pub struct_path: Option<String>,  // Optional canonical path (e.g., "crate::types::Rectangle")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddEnumVariantOp {
    pub enum_name: String,
    pub variant_def: String, // e.g., "NewVariant" or "NewVariant { x: i32 }"
    pub position: InsertPosition,
    #[serde(default)]
    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateEnumVariantOp {
    pub enum_name: String,
    pub variant_def: String, // e.g., "UpdatedVariant { new_field: Type }" (variant name parsed from this)
    #[serde(default)]
    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoveEnumVariantOp {
    pub enum_name: String,
    pub variant_name: String, // Name of the variant to remove
    #[serde(default)]
    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddMatchArmOp {
    pub pattern: String, // e.g., "MyEnum::NewVariant"
    pub body: String,    // e.g., "todo!()"
    pub function_name: Option<String>, // Optional: specific function containing match
    #[serde(default)]
    pub auto_detect: bool, // Auto-detect missing enum variants
    pub enum_name: Option<String>, // Enum name for auto-detection
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateMatchArmOp {
    pub pattern: String, // Pattern to find (e.g., "MyEnum::Variant")
    pub new_body: String, // New body for the arm
    pub function_name: Option<String>, // Optional: specific function containing match
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoveMatchArmOp {
    pub pattern: String, // Pattern to remove (e.g., "MyEnum::Variant")
    pub function_name: Option<String>, // Optional: specific function containing match
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddImplMethodOp {
    pub target: String, // e.g., "MyStruct" or "impl MyTrait for MyStruct"
    pub method_def: String, // Full method definition
    pub position: InsertPosition,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddUseStatementOp {
    pub use_path: String, // e.g., "std::collections::HashMap"
    pub position: InsertPosition,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddDeriveOp {
    pub target_name: String, // Name of struct or enum
    pub target_type: String, // "struct" or "enum"
    pub derives: Vec<String>, // e.g., ["Clone", "Debug", "Serialize"]
    #[serde(default)]
    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InsertPosition {
    First,
    Last,
    After(String),  // After named item
    Before(String), // Before named item
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BatchSpec {
    pub base_path: PathBuf,
    pub operations: Vec<Operation>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NodeLocation {
    pub line: usize,
    pub column: usize,
    pub end_line: usize,
    pub end_column: usize,
}

/// Backup of a single AST node before modification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupNode {
    pub node_type: String,        // "ItemStruct", "ItemEnum", "ItemImpl", "ExprStruct", "ExprMatch"
    pub identifier: String,        // "User", "Status::Draft", "process_event", etc.
    pub original_content: String,  // Original AST node as formatted code
    pub location: NodeLocation,
}

/// Result of applying an operation
#[derive(Debug)]
pub struct ModificationResult {
    pub changed: bool,
    pub modified_nodes: Vec<BackupNode>,
    /// Unmatched qualified paths (only populated for struct literal operations with simple names)
    /// Maps fully qualified path to count of instances found but not matched
    pub unmatched_qualified_paths: Option<std::collections::HashMap<String, usize>>,
}

/// Result of inspecting/listing AST nodes
#[derive(Debug, Serialize, Deserialize)]
pub struct InspectResult {
    pub file_path: String,
    pub node_type: String,      // "ExprStruct", "ExprMatch", etc.
    pub identifier: String,      // "Shadow", "Config", etc.
    pub location: NodeLocation,
    pub snippet: String,         // Formatted code snippet
    #[serde(skip_serializing_if = "Option::is_none")]
    pub preceding_comment: Option<String>,  // Doc comments + regular comments before the node
}

/// Generic transformation operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransformOp {
    pub node_type: String,           // "macro-call", "method-call", etc.
    pub name_filter: Option<String>, // Filter by name (e.g., "eprintln")
    pub content_filter: Option<String>, // Filter by content (e.g., "[SHADOW RENDER]")
    pub action: TransformAction,     // What to do with matching nodes
}

/// Actions that can be performed on AST nodes
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum TransformAction {
    Comment,                    // Wrap in // comment
    Remove,                     // Delete the node entirely
    Replace { with: String },   // Replace with provided code
}

/// Rename an enum variant across the codebase
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RenameEnumVariantOp {
    pub enum_name: String,      // Name of the enum (e.g., "IRValue")
    pub old_variant: String,    // Current variant name (e.g., "HashMapV2")
    pub new_variant: String,    // New variant name (e.g., "HashMap")
    #[serde(default)]
    pub enum_path: Option<String>,  // Optional canonical path (e.g., "crate::compiler::types::IRValue")
    #[serde(default)]
    pub edit_mode: EditMode,    // How to apply changes (surgical vs reformat)
}

/// Rename a function across the codebase
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RenameFunctionOp {
    pub old_name: String,       // Current function name (e.g., "process_v2")
    pub new_name: String,       // New function name (e.g., "process")
    #[serde(default)]
    pub function_path: Option<String>,  // Optional canonical path (e.g., "crate::utils::process_v2")
    #[serde(default)]
    pub edit_mode: EditMode,    // How to apply changes (surgical vs reformat)
}

/// Add documentation comment to an item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddDocCommentOp {
    pub target_type: String,    // "struct", "enum", "function", "field", "variant"
    pub name: String,           // Name of the target (e.g., "User", "Status::Draft")
    pub doc_comment: String,    // Documentation text (without /// prefix)
    #[serde(default)]
    pub style: DocCommentStyle, // Line (///) or Block (/** */)
}

/// Update existing documentation comment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateDocCommentOp {
    pub target_type: String,    // "struct", "enum", "function", "field", "variant"
    pub name: String,           // Name of the target
    pub doc_comment: String,    // New documentation text
}

/// Remove documentation comment from an item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoveDocCommentOp {
    pub target_type: String,    // "struct", "enum", "function", "field", "variant"
    pub name: String,           // Name of the target
}

/// Documentation comment style
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DocCommentStyle {
    Line,   // /// or //!
    Block,  // /** */ or /*! */
}

impl Default for DocCommentStyle {
    fn default() -> Self {
        DocCommentStyle::Line
    }
}

impl std::str::FromStr for DocCommentStyle {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "line" => Ok(DocCommentStyle::Line),
            "block" => Ok(DocCommentStyle::Block),
            _ => Err(format!("Invalid doc comment style: {}. Valid values are 'line' or 'block'", s)),
        }
    }
}

/// Location of a field in the codebase
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldLocation {
    pub file_path: String,
    pub line: usize,
    pub context: FieldContext,
}

/// Insert position for call arguments (numeric since args are positional)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ArgPosition {
    /// Insert as first argument
    First,
    /// Insert as last argument
    Last,
    /// Insert at specific index (0-based, shifts existing args right)
    Index(usize),
}

impl Default for ArgPosition {
    fn default() -> Self {
        ArgPosition::Last
    }
}

impl std::fmt::Display for ArgPosition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArgPosition::First => write!(f, "first"),
            ArgPosition::Last => write!(f, "last"),
            ArgPosition::Index(i) => write!(f, "index:{}", i),
        }
    }
}

impl std::str::FromStr for ArgPosition {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "first" => Ok(ArgPosition::First),
            "last" => Ok(ArgPosition::Last),
            s if s.starts_with("index:") => {
                let idx = s[6..].parse::<usize>()
                    .map_err(|_| format!("Invalid index in position: {}", s))?;
                Ok(ArgPosition::Index(idx))
            }
            s => {
                // Try parsing as plain number
                if let Ok(idx) = s.parse::<usize>() {
                    Ok(ArgPosition::Index(idx))
                } else {
                    Err(format!("Invalid arg position: {}. Valid values are 'first', 'last', or 'index:N'", s))
                }
            }
        }
    }
}

/// Add an argument to function or method calls
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddCallArgOp {
    /// Name of the function or method to target
    pub call_name: String,
    /// Expression to add as argument (e.g., "None", "ctx.clone()", "Default::default()")
    pub arg_expr: String,
    /// Where to insert the argument
    #[serde(default)]
    pub position: ArgPosition,
    /// Filter to "function" or "method" calls only (None = both)
    #[serde(default)]
    pub call_type: Option<String>,
    /// Filter call sites by content substring
    #[serde(default)]
    pub content_filter: Option<String>,
}

/// Update an argument at a specific index in function or method calls
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateCallArgOp {
    /// Name of the function or method to target
    pub call_name: String,
    /// Index of the argument to update (0-based)
    pub arg_index: usize,
    /// New expression for the argument
    pub new_expr: String,
    /// Filter to "function" or "method" calls only (None = both)
    #[serde(default)]
    pub call_type: Option<String>,
    /// Filter call sites by content substring
    #[serde(default)]
    pub content_filter: Option<String>,
}

/// Remove an argument at a specific index from function or method calls
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoveCallArgOp {
    /// Name of the function or method to target
    pub call_name: String,
    /// Index of the argument to remove (0-based)
    pub arg_index: usize,
    /// Filter to "function" or "method" calls only (None = both)
    #[serde(default)]
    pub call_type: Option<String>,
    /// Filter call sites by content substring
    #[serde(default)]
    pub content_filter: Option<String>,
}

/// Context in which a field appears
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum FieldContext {
    StructDefinition {
        struct_name: String,
        field_type: String,
    },
    EnumVariantDefinition {
        enum_name: String,
        variant_name: String,
        field_type: String,
    },
    StructLiteral {
        struct_name: String,
    },
}