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 pub unmatched_qualified_paths: Option<std::collections::HashMap<String, usize>>,
214}
215
216#[derive(Debug, Serialize, Deserialize)]
218pub struct InspectResult {
219 pub file_path: String,
220 pub node_type: String, pub identifier: String, pub location: NodeLocation,
223 pub snippet: String, #[serde(skip_serializing_if = "Option::is_none")]
225 pub preceding_comment: Option<String>, }
227
228#[derive(Debug, Clone, Serialize, Deserialize)]
230pub struct TransformOp {
231 pub node_type: String, pub name_filter: Option<String>, pub content_filter: Option<String>, pub action: TransformAction, }
236
237#[derive(Debug, Clone, Serialize, Deserialize)]
239#[serde(tag = "type")]
240pub enum TransformAction {
241 Comment, Remove, Replace { with: String }, }
245
246#[derive(Debug, Clone, Serialize, Deserialize)]
248pub struct RenameEnumVariantOp {
249 pub enum_name: String, pub old_variant: String, pub new_variant: String, #[serde(default)]
253 pub enum_path: Option<String>, #[serde(default)]
255 pub edit_mode: EditMode, }
257
258#[derive(Debug, Clone, Serialize, Deserialize)]
260pub struct RenameFunctionOp {
261 pub old_name: String, pub new_name: String, #[serde(default)]
264 pub function_path: Option<String>, #[serde(default)]
266 pub edit_mode: EditMode, }
268
269#[derive(Debug, Clone, Serialize, Deserialize)]
271pub struct AddDocCommentOp {
272 pub target_type: String, pub name: String, pub doc_comment: String, #[serde(default)]
276 pub style: DocCommentStyle, }
278
279#[derive(Debug, Clone, Serialize, Deserialize)]
281pub struct UpdateDocCommentOp {
282 pub target_type: String, pub name: String, pub doc_comment: String, }
286
287#[derive(Debug, Clone, Serialize, Deserialize)]
289pub struct RemoveDocCommentOp {
290 pub target_type: String, pub name: String, }
293
294#[derive(Debug, Clone, Serialize, Deserialize)]
296#[serde(rename_all = "lowercase")]
297pub enum DocCommentStyle {
298 Line, Block, }
301
302impl Default for DocCommentStyle {
303 fn default() -> Self {
304 DocCommentStyle::Line
305 }
306}
307
308impl std::str::FromStr for DocCommentStyle {
309 type Err = String;
310
311 fn from_str(s: &str) -> Result<Self, Self::Err> {
312 match s.to_lowercase().as_str() {
313 "line" => Ok(DocCommentStyle::Line),
314 "block" => Ok(DocCommentStyle::Block),
315 _ => Err(format!("Invalid doc comment style: {}. Valid values are 'line' or 'block'", s)),
316 }
317 }
318}
319
320#[derive(Debug, Clone, Serialize, Deserialize)]
322pub struct FieldLocation {
323 pub file_path: String,
324 pub line: usize,
325 pub context: FieldContext,
326}
327
328#[derive(Debug, Clone, Serialize, Deserialize)]
330#[serde(tag = "type")]
331pub enum FieldContext {
332 StructDefinition {
333 struct_name: String,
334 field_type: String,
335 },
336 EnumVariantDefinition {
337 enum_name: String,
338 variant_name: String,
339 field_type: String,
340 },
341 StructLiteral {
342 struct_name: String,
343 },
344}