1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum EditMode {
8 Surgical,
11 Reformat,
14}
15
16impl Default for EditMode {
17 fn default() -> Self {
18 EditMode::Surgical
19 }
20}
21
22impl std::fmt::Display for EditMode {
23 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24 match self {
25 EditMode::Surgical => write!(f, "surgical"),
26 EditMode::Reformat => write!(f, "reformat"),
27 }
28 }
29}
30
31impl std::str::FromStr for EditMode {
32 type Err = String;
33
34 fn from_str(s: &str) -> Result<Self, Self::Err> {
35 match s.to_lowercase().as_str() {
36 "surgical" => Ok(EditMode::Surgical),
37 "reformat" => Ok(EditMode::Reformat),
38 _ => Err(format!("Invalid edit mode: {}. Valid values are 'surgical' or 'reformat'", s)),
39 }
40 }
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(tag = "type")]
45pub enum Operation {
46 AddStructField(AddStructFieldOp),
47 UpdateStructField(UpdateStructFieldOp),
48 RemoveStructField(RemoveStructFieldOp),
49 AddStructLiteralField(AddStructLiteralFieldOp),
50 AddEnumVariant(AddEnumVariantOp),
51 UpdateEnumVariant(UpdateEnumVariantOp),
52 RemoveEnumVariant(RemoveEnumVariantOp),
53 AddMatchArm(AddMatchArmOp),
54 UpdateMatchArm(UpdateMatchArmOp),
55 RemoveMatchArm(RemoveMatchArmOp),
56 AddImplMethod(AddImplMethodOp),
57 AddUseStatement(AddUseStatementOp),
58 AddDerive(AddDeriveOp),
59 Transform(TransformOp),
60 RenameEnumVariant(RenameEnumVariantOp),
61 RenameFunction(RenameFunctionOp),
62 AddDocComment(AddDocCommentOp),
63 UpdateDocComment(UpdateDocCommentOp),
64 RemoveDocComment(RemoveDocCommentOp),
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub struct AddStructFieldOp {
69 pub struct_name: String,
70 pub field_def: String, pub position: InsertPosition,
72 #[serde(default)]
73 pub literal_default: Option<String>, #[serde(default)]
75 pub where_filter: Option<String>, }
77
78#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct UpdateStructFieldOp {
80 pub struct_name: String,
81 pub field_def: String, #[serde(default)]
83 pub where_filter: Option<String>, }
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct RemoveStructFieldOp {
88 pub struct_name: String,
89 pub field_name: String, #[serde(default)]
91 pub literal_only: bool, #[serde(default)]
93 pub where_filter: Option<String>, }
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub struct AddStructLiteralFieldOp {
98 pub struct_name: String,
99 pub field_def: String, pub position: InsertPosition,
101 #[serde(default)]
102 pub struct_path: Option<String>, }
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct AddEnumVariantOp {
107 pub enum_name: String,
108 pub variant_def: String, pub position: InsertPosition,
110 #[serde(default)]
111 pub where_filter: Option<String>, }
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct UpdateEnumVariantOp {
116 pub enum_name: String,
117 pub variant_def: String, #[serde(default)]
119 pub where_filter: Option<String>, }
121
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct RemoveEnumVariantOp {
124 pub enum_name: String,
125 pub variant_name: String, #[serde(default)]
127 pub where_filter: Option<String>, }
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct AddMatchArmOp {
132 pub pattern: String, pub body: String, pub function_name: Option<String>, #[serde(default)]
136 pub auto_detect: bool, pub enum_name: Option<String>, }
139
140#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct UpdateMatchArmOp {
142 pub pattern: String, pub new_body: String, pub function_name: Option<String>, }
146
147#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct RemoveMatchArmOp {
149 pub pattern: String, pub function_name: Option<String>, }
152
153#[derive(Debug, Clone, Serialize, Deserialize)]
154pub struct AddImplMethodOp {
155 pub target: String, pub method_def: String, pub position: InsertPosition,
158}
159
160#[derive(Debug, Clone, Serialize, Deserialize)]
161pub struct AddUseStatementOp {
162 pub use_path: String, pub position: InsertPosition,
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub struct AddDeriveOp {
168 pub target_name: String, pub target_type: String, pub derives: Vec<String>, #[serde(default)]
172 pub where_filter: Option<String>, }
174
175#[derive(Debug, Clone, Serialize, Deserialize)]
176pub enum InsertPosition {
177 First,
178 Last,
179 After(String), Before(String), }
182
183#[derive(Debug, Serialize, Deserialize)]
184pub struct BatchSpec {
185 pub base_path: PathBuf,
186 pub operations: Vec<Operation>,
187}
188
189#[derive(Debug, Serialize, Deserialize, Clone)]
190pub struct NodeLocation {
191 pub line: usize,
192 pub column: usize,
193 pub end_line: usize,
194 pub end_column: usize,
195}
196
197#[derive(Debug, Clone, Serialize, Deserialize)]
199pub struct BackupNode {
200 pub node_type: String, pub identifier: String, pub original_content: String, pub location: NodeLocation,
204}
205
206#[derive(Debug)]
208pub struct ModificationResult {
209 pub changed: bool,
210 pub modified_nodes: Vec<BackupNode>,
211}
212
213#[derive(Debug, Serialize, Deserialize)]
215pub struct InspectResult {
216 pub file_path: String,
217 pub node_type: String, pub identifier: String, pub location: NodeLocation,
220 pub snippet: String, #[serde(skip_serializing_if = "Option::is_none")]
222 pub preceding_comment: Option<String>, }
224
225#[derive(Debug, Clone, Serialize, Deserialize)]
227pub struct TransformOp {
228 pub node_type: String, pub name_filter: Option<String>, pub content_filter: Option<String>, pub action: TransformAction, }
233
234#[derive(Debug, Clone, Serialize, Deserialize)]
236#[serde(tag = "type")]
237pub enum TransformAction {
238 Comment, Remove, Replace { with: String }, }
242
243#[derive(Debug, Clone, Serialize, Deserialize)]
245pub struct RenameEnumVariantOp {
246 pub enum_name: String, pub old_variant: String, pub new_variant: String, #[serde(default)]
250 pub enum_path: Option<String>, #[serde(default)]
252 pub edit_mode: EditMode, }
254
255#[derive(Debug, Clone, Serialize, Deserialize)]
257pub struct RenameFunctionOp {
258 pub old_name: String, pub new_name: String, #[serde(default)]
261 pub function_path: Option<String>, #[serde(default)]
263 pub edit_mode: EditMode, }
265
266#[derive(Debug, Clone, Serialize, Deserialize)]
268pub struct AddDocCommentOp {
269 pub target_type: String, pub name: String, pub doc_comment: String, #[serde(default)]
273 pub style: DocCommentStyle, }
275
276#[derive(Debug, Clone, Serialize, Deserialize)]
278pub struct UpdateDocCommentOp {
279 pub target_type: String, pub name: String, pub doc_comment: String, }
283
284#[derive(Debug, Clone, Serialize, Deserialize)]
286pub struct RemoveDocCommentOp {
287 pub target_type: String, pub name: String, }
290
291#[derive(Debug, Clone, Serialize, Deserialize)]
293#[serde(rename_all = "lowercase")]
294pub enum DocCommentStyle {
295 Line, Block, }
298
299impl Default for DocCommentStyle {
300 fn default() -> Self {
301 DocCommentStyle::Line
302 }
303}
304
305impl std::str::FromStr for DocCommentStyle {
306 type Err = String;
307
308 fn from_str(s: &str) -> Result<Self, Self::Err> {
309 match s.to_lowercase().as_str() {
310 "line" => Ok(DocCommentStyle::Line),
311 "block" => Ok(DocCommentStyle::Block),
312 _ => Err(format!("Invalid doc comment style: {}. Valid values are 'line' or 'block'", s)),
313 }
314 }
315}
316
317#[derive(Debug, Clone, Serialize, Deserialize)]
319pub struct FieldLocation {
320 pub file_path: String,
321 pub line: usize,
322 pub context: FieldContext,
323}
324
325#[derive(Debug, Clone, Serialize, Deserialize)]
327#[serde(tag = "type")]
328pub enum FieldContext {
329 StructDefinition {
330 struct_name: String,
331 field_type: String,
332 },
333 EnumVariantDefinition {
334 enum_name: String,
335 variant_name: String,
336 field_type: String,
337 },
338 StructLiteral {
339 struct_name: String,
340 },
341}