rs_hack/
operations.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(tag = "type")]
6pub enum Operation {
7    AddStructField(AddStructFieldOp),
8    UpdateStructField(UpdateStructFieldOp),
9    RemoveStructField(RemoveStructFieldOp),
10    AddStructLiteralField(AddStructLiteralFieldOp),
11    AddEnumVariant(AddEnumVariantOp),
12    UpdateEnumVariant(UpdateEnumVariantOp),
13    RemoveEnumVariant(RemoveEnumVariantOp),
14    AddMatchArm(AddMatchArmOp),
15    UpdateMatchArm(UpdateMatchArmOp),
16    RemoveMatchArm(RemoveMatchArmOp),
17    AddImplMethod(AddImplMethodOp),
18    AddUseStatement(AddUseStatementOp),
19    AddDerive(AddDeriveOp),
20    Transform(TransformOp),
21    RenameEnumVariant(RenameEnumVariantOp),
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct AddStructFieldOp {
26    pub struct_name: String,
27    pub field_def: String, // e.g., "new_field: Option<String>" or just "new_field" if literal_default is provided
28    pub position: InsertPosition,
29    #[serde(default)]
30    pub literal_default: Option<String>, // If provided: tries to add to definition (idempotent), always updates literals
31    #[serde(default)]
32    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct UpdateStructFieldOp {
37    pub struct_name: String,
38    pub field_def: String, // e.g., "field_name: NewType" (field name is parsed from this)
39    #[serde(default)]
40    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct RemoveStructFieldOp {
45    pub struct_name: String,
46    pub field_name: String, // Name of the field to remove
47    #[serde(default)]
48    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct AddStructLiteralFieldOp {
53    pub struct_name: String,
54    pub field_def: String, // e.g., "return_type: None"
55    pub position: InsertPosition,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct AddEnumVariantOp {
60    pub enum_name: String,
61    pub variant_def: String, // e.g., "NewVariant" or "NewVariant { x: i32 }"
62    pub position: InsertPosition,
63    #[serde(default)]
64    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct UpdateEnumVariantOp {
69    pub enum_name: String,
70    pub variant_def: String, // e.g., "UpdatedVariant { new_field: Type }" (variant name parsed from this)
71    #[serde(default)]
72    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct RemoveEnumVariantOp {
77    pub enum_name: String,
78    pub variant_name: String, // Name of the variant to remove
79    #[serde(default)]
80    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
81}
82
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct AddMatchArmOp {
85    pub pattern: String, // e.g., "MyEnum::NewVariant"
86    pub body: String,    // e.g., "todo!()"
87    pub function_name: Option<String>, // Optional: specific function containing match
88    #[serde(default)]
89    pub auto_detect: bool, // Auto-detect missing enum variants
90    pub enum_name: Option<String>, // Enum name for auto-detection
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct UpdateMatchArmOp {
95    pub pattern: String, // Pattern to find (e.g., "MyEnum::Variant")
96    pub new_body: String, // New body for the arm
97    pub function_name: Option<String>, // Optional: specific function containing match
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct RemoveMatchArmOp {
102    pub pattern: String, // Pattern to remove (e.g., "MyEnum::Variant")
103    pub function_name: Option<String>, // Optional: specific function containing match
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct AddImplMethodOp {
108    pub target: String, // e.g., "MyStruct" or "impl MyTrait for MyStruct"
109    pub method_def: String, // Full method definition
110    pub position: InsertPosition,
111}
112
113#[derive(Debug, Clone, Serialize, Deserialize)]
114pub struct AddUseStatementOp {
115    pub use_path: String, // e.g., "std::collections::HashMap"
116    pub position: InsertPosition,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct AddDeriveOp {
121    pub target_name: String, // Name of struct or enum
122    pub target_type: String, // "struct" or "enum"
123    pub derives: Vec<String>, // e.g., ["Clone", "Debug", "Serialize"]
124    #[serde(default)]
125    pub where_filter: Option<String>, // Optional: filter targets (e.g., "derives_trait:Clone")
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub enum InsertPosition {
130    First,
131    Last,
132    After(String),  // After named item
133    Before(String), // Before named item
134}
135
136#[derive(Debug, Serialize, Deserialize)]
137pub struct BatchSpec {
138    pub base_path: PathBuf,
139    pub operations: Vec<Operation>,
140}
141
142#[derive(Debug, Serialize, Deserialize, Clone)]
143pub struct NodeLocation {
144    pub line: usize,
145    pub column: usize,
146    pub end_line: usize,
147    pub end_column: usize,
148}
149
150/// Backup of a single AST node before modification
151#[derive(Debug, Clone, Serialize, Deserialize)]
152pub struct BackupNode {
153    pub node_type: String,        // "ItemStruct", "ItemEnum", "ItemImpl", "ExprStruct", "ExprMatch"
154    pub identifier: String,        // "User", "Status::Draft", "process_event", etc.
155    pub original_content: String,  // Original AST node as formatted code
156    pub location: NodeLocation,
157}
158
159/// Result of applying an operation
160#[derive(Debug)]
161pub struct ModificationResult {
162    pub changed: bool,
163    pub modified_nodes: Vec<BackupNode>,
164}
165
166/// Result of inspecting/listing AST nodes
167#[derive(Debug, Serialize, Deserialize)]
168pub struct InspectResult {
169    pub file_path: String,
170    pub node_type: String,      // "ExprStruct", "ExprMatch", etc.
171    pub identifier: String,      // "Shadow", "Config", etc.
172    pub location: NodeLocation,
173    pub snippet: String,         // Formatted code snippet
174}
175
176/// Generic transformation operation
177#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct TransformOp {
179    pub node_type: String,           // "macro-call", "method-call", etc.
180    pub name_filter: Option<String>, // Filter by name (e.g., "eprintln")
181    pub content_filter: Option<String>, // Filter by content (e.g., "[SHADOW RENDER]")
182    pub action: TransformAction,     // What to do with matching nodes
183}
184
185/// Actions that can be performed on AST nodes
186#[derive(Debug, Clone, Serialize, Deserialize)]
187#[serde(tag = "type")]
188pub enum TransformAction {
189    Comment,                    // Wrap in // comment
190    Remove,                     // Delete the node entirely
191    Replace { with: String },   // Replace with provided code
192}
193
194/// Rename an enum variant across the codebase
195#[derive(Debug, Clone, Serialize, Deserialize)]
196pub struct RenameEnumVariantOp {
197    pub enum_name: String,      // Name of the enum (e.g., "IRValue")
198    pub old_variant: String,    // Current variant name (e.g., "HashMapV2")
199    pub new_variant: String,    // New variant name (e.g., "HashMap")
200}