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