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